objectives
play

Objectives understands the definition and terminology of a general - PowerPoint PPT Presentation

Objectives understands the definition and terminology of a general tree Struktur Data & Algoritme know applications of the tree ( Data Structures & Algorithms ) know how to traverse of a tree Tree Denny ( denny@cs.ui.ac.id )


  1. Objectives � understands the definition and terminology of a general tree Struktur Data & Algoritme � know applications of the tree ( Data Structures & Algorithms ) � know how to traverse of a tree Tree Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0 - I nternal Use Only SDA/ TREE/ V2.0/ 2 Outline Examples � Tree � a tree represents a hierarchy � example � organization structure of a corporation � terminology/definition � binary tree � traversal of trees � iterator SDA/ TREE/ V2.0/ 3 SDA/ TREE/ V2.0/ 4 1

  2. Examples Examples � table of contents of a book � Unix or DOS/Windows file system SDA/ TREE/ V2.0/ 5 SDA/ TREE/ V2.0/ 6 Terminology Tree Viewed Recursively � A is the root node � B is the parent of D and E � C is the sibling of B � D and E are the children of B � D, E, F, G, I are external nodes , or leaves � A, B, C, H are internal nodes � The depth / level / path length of E is 2 � The height of the tree is 3 � The sub-tree is also a tree!! � The degree of node B is 2 � Property: ( # edges ) = ( #nodes ) - 1 SDA/ TREE/ V2.0/ 7 SDA/ TREE/ V2.0/ 8 2

  3. Binary Tree Examples of Binary Tree � Binary tree: tree with all internal nodes of degree 2 � arithmetic expression � Recursive View: Binary tree is either � empty � an internal node (the root ) and two binary trees (left subtree and right subtree ) � Ordered/Search tree: the children of each node are ordered SDA/ TREE/ V2.0/ 9 SDA/ TREE/ V2.0/ 10 Properties of Binary Trees Traversing Trees � If we restrict that each parent can have two and only two � preorder traversal children, then: Algorithm preOrder(v) � (# external nodes ) = (# internal nodes) + 1 “visit” node v � (# nodes at level i) ≤ 2 i for each child w of v do � (# external nodes) ≤ 2 (height) � (height) ≥ log 2 (# external nodes) recursively perform preOrder(w) � (height) ≥ log 2 (# nodes) - 1 � reading a document from beginning to end � (height) ≤ (# internal nodes) = ((# nodes) - 1)/2 SDA/ TREE/ V2.0/ 11 SDA/ TREE/ V2.0/ 12 3

  4. Traversing Trees Traversing Trees � postorder traversal � Algorithm evaluateExpression(v) Algorithm postOrder(v) if v is an external node for each child w of v do return the variable stored at v recursively perform else let o be the operator stored at v postOrder(w) x ← evaluateExpression(leftChild(v)) “visit” node v y ← evaluateExpression(rightChild(v)) � du (disk usage) command in return x o y Unix SDA/ TREE/ V2.0/ 13 SDA/ TREE/ V2.0/ 14 Traversing Trees The BinaryNode in Java � inorder traversal of a � The tree is a collection of nodes: binary tree class BinaryNode Algorithm inOrder(v) { recursively perform Object element; /* Item stored in node */ BinaryNode left; inOrder(leftChild(v)) BinaryNode right; “visit” node v } recursively perform � The tree stores a reference to the root node , which is inOrder(rightChild(v)) the starting point. � printing an arithmetic public class BinaryTree expression { � print “(“ before private BinaryNode root; traversing the left subtree public BinaryTree( ) � print “)” after { traversing the right root = null; } subtree } SDA/ TREE/ V2.0/ 15 SDA/ TREE/ V2.0/ 16 4

  5. Think Recursively Routine to Compute Height � Computing the height of a tree is complex without � Handle base case (empty tree) recursion. � Use previous observation for other case. � The height of a tree is one more than the maximum of public static int height (TreeNode t) the heights of the subtrees. { � H T = max (H L +1, H R +1) if (t == null) { return 0; } else { return 1 + max(height (t.left), height (t.right)); } } H R +1 H L +1 H R H L SDA/ TREE/ V2.0/ 17 SDA/ TREE/ V2.0/ 18 Running Time Print Pre-Order class BinaryNode { � This strategy is a postorder traversal: information for void printPreOrder( ) a tree node is computed after the information for its { children is computed. System.out.println( element ); // Node � Postorder traversal running time is N times the cost if( left != null ) of processing each node. left.printPreOrder( ); // Left if( right != null ) � The running time is linear because we do constant right.printPreOrder( ); // Right work for each node in the tree. } } class BinaryTree { public void printPreOrder( ) { if( root != null ) root.printPreOrder( ); } } SDA/ TREE/ V2.0/ 19 SDA/ TREE/ V2.0/ 20 5

  6. Print Post-Order Print InOrder class BinaryNode { class BinaryNode { void printPostOrder( ) void printInOrder( ) { { if( left != null ) if( left != null ) left.printPostOrder( ); // Left left.printInOrder( ); // Left if( right != null ) System.out.println( element ); // Node right.printPostOrder( ); // Right if( right != null ) System.out.println( element ); // Node right.printInOrder( ); // Right } } } } class BinaryTree { class BinaryTree { public void printPostOrder( ) public void printInOrder( ) { { if( root != null ) if( root != null ) root.printPostOrder( ); root.printInOrder( ); } } } } SDA/ TREE/ V2.0/ 21 SDA/ TREE/ V2.0/ 22 Traversing Tree Exercise � A tree contains Integer objects. � Find the maximum value � Find the total value Pre-Order Post-Order InOrder SDA/ TREE/ V2.0/ 23 SDA/ TREE/ V2.0/ 24 6

  7. Iterator Class Tree Iterator: implementation � Problems with recursive: � see online code � Can not process step-by-step (ie. using loop) http://telaga.cs.ui.ac.id/WebKuliah/IKI101 � How to avoid recursive? How to implement non- 00/resources/code/DataStructures/TreeItera recusive traversal? tor.java � Recursion is implemented by using a stack. � We can traverse nonrecursively by maintaining the stack ourselves (emulate stack of activation records). � Is non-recursive approach faster than recursive approach? � Yes � Why? � We can place only the essentials, while the compiler place an entire activation record. SDA/ TREE/ V2.0/ 25 SDA/ TREE/ V2.0/ 26 Post-Order Traversal using Stack Post-Order Algorithm/ Pseudocode � Use stack to store the current state (nodes we have � init: push the root into the stack with state 0 traversed but not yet completed) � advance: � similar to PC (program counter) in the activation record � while (true) � What are the states for post-order traversal? • node X = pop from the stack 0. about to make a recursive call to left subtree • switch (state X): • case 0: 1. about to make a recursive call to right subtree • push node X with state 1; 2. about to process the current node • push left child node X (if it exists) w/ state 0; • break; • case 1: • push node X with state 2; • push right child node X (if it exists) w/ state 0; • break; • case 2: • “visit”/ ”set current to” the node X; return; SDA/ TREE/ V2.0/ 27 SDA/ TREE/ V2.0/ 28 7

  8. Post-Order traversal: stack states Post-Order: Implementation � see online code � http://telaga.cs.ui.ac.id/WebKuliah/IKI10100/res ources/code/DataStructures/PostOrder.java � http://telaga.cs.ui.ac.id/WebKuliah/IKI10100/res ources/code/DataStructures/StNode.java SDA/ TREE/ V2.0/ 29 SDA/ TREE/ V2.0/ 30 Exercise In-Order Traversal using Stack � Create an algorithm/psedo-code for in-order traversal � What are the states for in-order traversal? using stack. 0. about to make a recursive call to left subtree � Create an algorithm/psedo-code for pre-order 1. about to process the current node traversal using stack. 2. about to make a recursive call to right subtree SDA/ TREE/ V2.0/ 31 SDA/ TREE/ V2.0/ 32 8

  9. In-Order Algorithm/ Pseudocode In-Order Algorithm/ Pseudocode � init: push the root into the stack with state 0 � init: push the root into the stack with state 0 � advance: � advance (optimize): � while (true) � while (true) • node X = pop from the stack • node X = pop from the stack • switch (state X): • switch (state X): • case 0: • case 0: • push node X with state 1; • push node X with state 1; • push left child node X (if it exists) w/ state 0; • push left child node X (if it exists) w/ state 0; • break; • break; • case 1: • case 1: • push node X with state 2; • “visit”/ ”set current to” the node X; • “visit”/ ”set current to” the node X; return; • push right child node X (if it exists) w/ state 0; • case 2: • return; • push right child node X (if it exists) w/ state 0; • break; SDA/ TREE/ V2.0/ 33 SDA/ TREE/ V2.0/ 34 In-Order: Implementation Pre-Order Traversal using Stack � What are the states for in-order traversal? � see online code 0. about to process the current node http://telaga.cs.ui.ac.id/WebKuliah/IKI101 1. about to make a recursive call to left subtree 00/resources/code/DataStructures/InOrder.j 2. about to make a recursive call to right subtree ava SDA/ TREE/ V2.0/ 35 SDA/ TREE/ V2.0/ 36 9

Recommend


More recommend