tree data structure: design and implementation in

Upload: ahmedrajput

Post on 30-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    1/21

    9-1

    TreeTree Data Structure: DesignTree Data Structure: Design

    and Implementation in Javaand Implementation in Java

    resented byAhmad AbdulRehmanKhayam Shah

    resented toresented toir Naeem Aslamir Naeem Aslam

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    2/21

    9-2

    w hy Trees?w Treesw ree Terminologyw &ifference between Tree BinaryTreesw pplications of Treew uffman Coding Treew ne Recursive Tree Algorithm

    mplementation

    OutlineOutline

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    3/21

    9-3

    Storing Many ObjectsStoring Many Objects

    wWe have examined 2 major waysto store data in the mainmemory of the computer

    arrays

    use subscripts to immediatelyaccess elements

    fast access, but in the old

    days would consume morememory that you would likeit to (not true anymore)

    linked structures

    each node refers to the nextin the collection. To get to

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    4/21

    9-4

    Another Linked StructureAnother Linked Structure

    wWe now turn our attention to

    another major way of storing

    data: the Tree

    One implementation of a tree has

    nodes with a left and right

    link field (binary tree)

    w 1

    32

    root

    edges

    nodes

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    5/21

    9-5

    First Some DefinitionsFirst Some Definitions

    wA tree has a set of nodes and

    directed edges that connect

    them

    a directed edge connects a

    parent to its children

    wTree properties

    one node is distinguished as the

    root

    every node (except the root) is

    connected by an edge fromexactly one other node

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    6/21

    9-6

    General TreesGeneral Trees

    wTrees store data in a

    hierarchical manner

    w

    Root node is A A's children are B, C, and D E, F, and D are leaves Length of path from A to E is 2

    A

    DB C

    FE

    Trees have layers of nodes,where some are higher,

    others are lower.

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    7/21

    9-7

    Some tree terminologySome tree terminology

    Node An element in the tree referencesdata and other nodes

    Root The node at the top It is upsidedown!

    Parent The node directly above

    another node (except root)

    ChildThe node(s) below a given node

    Size The number of descendants plus

    one for the node itself

    Leaves Nodes with no children

    Levels The root A is at level 0, Eand F are at level 2

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    8/21

    9-8

    Differences Between A Tree & A Binary TreeDifferences Between A Tree & A Binary Tree

    w No node in a binary tree may have adegree more than 2, whereas there isno limit on the degree of a node in a

    tree.w A binary tree may be empty; a tree

    cannot be empty.

    w The subtrees of a binary tree are

    ordered; those of a tree are notordered.

    Morew Are different when viewed as binary

    trees.

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    9/21

    9-9

    Javas Classes ExampleJavas Classes Example

    Root

    children of root

    grand children of root

    great grand

    w

    Object

    Number Throwable OutputStream

    Integer Double Exception FileOutputStream

    RuntimeException

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    10/21

    9-10

    Applications of treesApplications of trees

    w File Systems Hierarchical files systems include

    Unix and DOS

    In DOS, each \ represents an edge(In Unix, it's /)

    Each directory is a file with a listof all its children

    wStore large volumes of data

    data can be quickly inserted,removed, and found

    w Data structure used in a variety ofsituations

    implement data vase management systems com ilers: ex ression tree s mbol tree

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    11/21

    9-11

    Huffman Coding TreeHuffman Coding Tree

    wBinary trees in a famous file

    compression algorithm HuffmanCoding Tree

    wEach character is stored in a

    leaf

    wThe code is found by following

    the path 0 go left, 1 go right

    a is 01

    e is 1

    'e'

    't' 'a'

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    12/21

    9-12

    Importing Packages Importing Packages

    import java.awt.*;\\ importing abstractwindowing toolkit

    import java.awt.event.*; \\ importing awt.event

    import javax.swing.*; \\ importing javax.swing

    import javax.swing.tree.*;\\ importingswing.tree package

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    13/21

    9-13

    Creating Tree1 classCreating Tree1 class

    public class Tree1 extends Jframe \\ extendingclass using Jframe

    {public static void main(String args[])

    Tree1 frame = new Tree1(" A Tree");\\ creataingtree

    frame.setSize(200,200); \\ set frame size

    frame.setVisible(true); \\ if false then novisibility

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    14/21

    9-14

    Creating NodesCreating Nodes

    }public Tree1(String title) \\ creating class

    setTitle(title); \\ setting title

    DefaultMutableTreeNode root=new \\ creating root

    DefaultMutableTreeNode ("Engineering");\\ root

    name

    DefaultMutableTreeNode style=new \\ declare newstyle of root node

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    15/21

    9-15

    Creating Style to Nodes.Creating Style to Nodes.

    DefaultMutableTreeNode ("InformationTechnology");

    root.add(style); \\ add sub root or leaf node.

    JTree jt=new JTree(root); \\

    Container contentPane=getContentPane();\\

    container can contain other awt component panel

    contentPane.add(new JScrollPane(jt));\\ includingscrollpane class for manage a viewprt

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    16/21

    9-16

    wEach Tree object has

    a reference to an object object so wecan store anything

    a link to the left subtree whichcould be an empty tree

    a link to the right subtree whichcould be an empty tree

    w3 Constructors

    two set some data fields to null

    (left and right)

    wThe data fields are private

    Like LinkNode, methods in the

    enclosing class can reference

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    17/21

    9-17

    Memory linked structure diagramMemory linked structure diagram

    Computer Science "

    00

    Engineering

    Root

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    18/21

    9-18

    import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;

    public class Tree1 extends JFrame{public static void main(String args[]){Tree1 frame = new Tree1(" A Tree");frame.setSize(200,200);frame.setVisible(true);}public Tree1(String title){setTitle(title);DefaultMutableTreeNode root=new

    DefaultMutableTreeNode ("Engineering");DefaultMutableTreeNode style=new

    DefaultMutableTreeNode ("InformationTechnology");

    root.add(style);

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    19/21

    9-19

    style=newDefaultMutableTreeNode("Electronics");

    root.add(style);style=new DefaultMutableTreeNode

    ("Computer Science");root.add(style);style=new DefaultMutableTreeNode

    ("Mechanical");root.add(style);style=new DefaultMutableTreeNode

    ("Electrical");root.add(style);style=new DefaultMutableTreeNode

    ("Sound");root.add(style);

    JTree jt=new JTree(root);Container contentPane=getContentPane();contentPane.add(new JScrollPane(jt));}}

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    20/21

    9-20

    ny Questions ?ny Questions ?

  • 8/14/2019 Tree Data Structure: Design and Implementation In

    21/21

    9-21

    hank youhank you