Presentation is loading. Please wait.

Presentation is loading. Please wait.

Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.

Similar presentations


Presentation on theme: "Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6."— Presentation transcript:

1 Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6

2  Definition of Trees  Representing Rooted Tree  Tree Traversal  Preorder Traversal  Postorder Traversal  Level Order Traversal

3  Tree:  Set of nodes and edges that connect them  Exactly one path between any 2 nodes  Rooted Tree:  One distinguished node is called the root  Every node C, except root, has one parent P, the first node on path from c to the root. C is P’s child  Root has no parent  A node can have any number of children

4  Leaf  Node with no children  Siblings  Nodes with same parent  Ancestors  nodes on path from d to rott, including d, d’s parent, d’s grand parent, … root  Descendant  If A is B’s ancestor, then B is A’s Descendant

5  Length of path  Number of edges in path  Depth of node n  Length of path from n to root  Depth of root is zero  Height of node n  Length of path from n to its deepest descendant  Height of any leaf is zero  Height of a tree = Height of the root  Subtree rooted at n  The tree formed by n and its descendants

6  G & T  Each node has 3 references stored in a list ▪ Item ▪ Parent ▪ Children  Another Option: Sibling Tree  Siblings are directly linked

7 Next Sibling Parent Item First Child Class SibTreeNode { Object item ; SibTreeNode parent ; SibTreeNodefirstChild; SibTreeNodenextSibling; } Class SibTreeNode { Object item ; SibTreeNode parent ; SibTreeNodefirstChild; SibTreeNodenextSibling; }

8 Next Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First Child

9  Rooted Tree  Preorder Traversal  Postorder Traversal  Level Order Traversal  Binary Tree  Inorder Traveral

10  Visit each node before recursively visiting its children, left to right A BC DEFGH A A’s First Child B B’s First Child D D has no child, then sibling E E has no child, then sibling F F has no child, no sibling B has no child, no sibling A has no child, then sibling C C’s First Child G G has no child, then sibling H H has no child, no sibling

11 Class SibTreeNode { public void preorder() { this.visit(); if(firstChild!=null){ firstChild.preorder(); } if(nextSibling!=null){ nextSibling.preorder(); } Class SibTreeNode { public void preorder() { this.visit(); if(firstChild!=null){ firstChild.preorder(); } if(nextSibling!=null){ nextSibling.preorder(); }  Preorder Traversal Realization by Recursion

12  Preorder Traversal Realization by Stacks A BC DEFGH A B C A Stack: Visiting Sequence: B D E F DEFC G H GH

13  Visit each node’s children (left-to-right) before the node itself A BC DEFGH

14  Postorder Traversal Realization by Recursion Class SibTreeNode { public void postorder() { if(firstChild!=null) { firstChild.postorder(); } this.visit(); if(nextSibling!=null) { nextSibling.postorder(); } Class SibTreeNode { public void postorder() { if(firstChild!=null) { firstChild.postorder(); } this.visit(); if(nextSibling!=null) { nextSibling.postorder(); }

15  Visit root, then all (height-1) nodes, then all (height-2) nodes, … etc. A BC DEFGH

16  Level Order Traversal Realization by Queues Queue: A A BC DEFGH B C D E F G H A Visiting Sequence: BDEFCGH

17  A Binary Tree  No node has > 2 children  Every child is either left child or a right child, even if it is the only child

18  Binary Tree Node Right Child Parent Item Left Child Class BiTreeNode { Object item ; BiTreeNode parent ; BiTreeNode leftChild; BiTreeNode rightChild; } Class BiTreeNode { Object item ; BiTreeNode parent ; BiTreeNode leftChild; BiTreeNode rightChild; }

19 Right Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left Child

20  Visit left child, then node, then right child Class BiTreeNode { public void inorder() { if(leftChild!=null){ leftChild.inorder(); } this.visit(); if(rightChild!=null) { rightChild.inorder(); } Class BiTreeNode { public void inorder() { if(leftChild!=null){ leftChild.inorder(); } this.visit(); if(rightChild!=null) { rightChild.inorder(); }

21  Visualization of inorder traversal A BC DEF

22


Download ppt "Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6."

Similar presentations


Ads by Google