struktur data algoritme data structures algorithms
play

Struktur Data & Algoritme ( Data Structures & Algorithms ) - PDF document

Struktur Data & Algoritme ( Data Structures & Algorithms ) 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 -


  1. Struktur Data & Algoritme ( Data Structures & Algorithms ) 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 Objectives � understands the definition and terminology of a general tree � know applications of the tree � know how to traverse of a tree SDA/ TREE/ V2.0/ 2 1

  2. Outline � Tree � example � terminology/definition � binary tree � traversal of trees � iterator SDA/ TREE/ V2.0/ 3 Examples � a tree represents a hierarchy � organization structure of a corporation SDA/ TREE/ V2.0/ 4 2

  3. Examples � table of contents of a book SDA/ TREE/ V2.0/ 5 Examples � Unix or DOS/Windows file system SDA/ TREE/ V2.0/ 6 3

  4. Terminology � 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 degree of node B is 2 � Property: ( # edges ) = ( #nodes ) - 1 SDA/ TREE/ V2.0/ 7 Tree Viewed Recursively � The sub-tree is also a tree!! SDA/ TREE/ V2.0/ 8 4

  5. Binary Tree � Binary tree: tree with all internal nodes of degree 2 � 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 Examples of Binary Tree � arithmetic expression SDA/ TREE/ V2.0/ 10 5

  6. Properties of Binary Trees � If we restrict that each parent can have two and only two children, then: � (# external nodes ) = (# internal nodes) + 1 � (# nodes at level i) ≤ 2 i � (# external nodes) ≤ 2 (height) � (height) ≥ log 2 (# external nodes) � (height) ≥ log 2 (# nodes) - 1 � (height) ≤ (# internal nodes) = ((# nodes) - 1)/2 SDA/ TREE/ V2.0/ 11 Traversing Trees � preorder traversal Algorithm preOrder(v) “visit” node v for each child w of v do recursively perform preOrder(w) � reading a document from beginning to end SDA/ TREE/ V2.0/ 12 6

  7. Traversing Trees � postorder traversal Algorithm postOrder(v) for each child w of v do recursively perform postOrder(w) “visit” node v � du (disk usage) command in Unix SDA/ TREE/ V2.0/ 13 Traversing Trees � Algorithm evaluateExpression(v) if v is an external node return the variable stored at v else let o be the operator stored at v x ← evaluateExpression(leftChild(v)) y ← evaluateExpression(rightChild(v)) return x o y SDA/ TREE/ V2.0/ 14 7

  8. Traversing Trees � inorder traversal of a binary tree Algorithm inOrder(v) recursively perform inOrder(leftChild(v)) “visit” node v recursively perform inOrder(rightChild(v)) � printing an arithmetic expression � print “(“ before traversing the left subtree � print “)” after traversing the right subtree SDA/ TREE/ V2.0/ 15 The BinaryNode in Java � The tree is a collection of nodes: class BinaryNode { Object element; /* Item stored in node */ BinaryNode left; BinaryNode right; } � The tree stores a reference to the root node , which is the starting point. public class BinaryTree { private BinaryNode root; public BinaryTree( ) { root = null; } } SDA/ TREE/ V2.0/ 16 8

  9. Think Recursively � Computing the height of a tree is complex without recursion. � The height of a tree is one more than the maximum of the heights of the subtrees. � H T = max (H L +1, H R +1) H R +1 H L +1 H R H L SDA/ TREE/ V2.0/ 17 Routine to Compute Height � Handle base case (empty tree) � Use previous observation for other case. public static int height (TreeNode t) { if (t == null) { return 0; } else { return 1 + max(height (t.left), height (t.right)); } } SDA/ TREE/ V2.0/ 18 9

  10. Running Time � This strategy is a postorder traversal: information for a tree node is computed after the information for its children is computed. � Postorder traversal running time is N times the cost of processing each node. � The running time is linear because we do constant work for each node in the tree. SDA/ TREE/ V2.0/ 19 Print Pre-Order class BinaryNode { void printPreOrder( ) { System.out.println( element ); // Node if( left != null ) left.printPreOrder( ); // Left if( right != null ) right.printPreOrder( ); // Right } } class BinaryTree { public void printPreOrder( ) { if( root != null ) root.printPreOrder( ); } } SDA/ TREE/ V2.0/ 20 10

  11. Print Post-Order class BinaryNode { void printPostOrder( ) { if( left != null ) left.printPostOrder( ); // Left if( right != null ) right.printPostOrder( ); // Right System.out.println( element ); // Node } } class BinaryTree { public void printPostOrder( ) { if( root != null ) root.printPostOrder( ); } } SDA/ TREE/ V2.0/ 21 Print InOrder class BinaryNode { void printInOrder( ) { if( left != null ) left.printInOrder( ); // Left System.out.println( element ); // Node if( right != null ) right.printInOrder( ); // Right } } class BinaryTree { public void printInOrder( ) { if( root != null ) root.printInOrder( ); } } SDA/ TREE/ V2.0/ 22 11

  12. Traversing Tree Pre-Order Post-Order InOrder SDA/ TREE/ V2.0/ 23 Exercise � A tree contains Integer objects. � Find the maximum value � Find the total value SDA/ TREE/ V2.0/ 24 12

  13. Iterator Class � Problems with recursive: � Can not process step-by-step (ie. using loop) � How to avoid recursive? How to implement non- recusive traversal? � 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 Tree Iterator: implementation � see online code http://telaga.cs.ui.ac.id/WebKuliah/IKI101 00/resources/code/DataStructures/TreeItera tor.java SDA/ TREE/ V2.0/ 26 13

  14. Post-Order Traversal using Stack � Use stack to store the current state (nodes we have traversed but not yet completed) � similar to PC (program counter) in the activation record � What are the states for post-order traversal? 0. about to make a recursive call to left subtree 1. about to make a recursive call to right subtree 2. about to process the current node SDA/ TREE/ V2.0/ 27 Post-Order Algorithm/ Pseudocode � init: push the root into the stack with state 0 � advance: � while (true) • node X = pop from the stack • switch (state X): • case 0: • push node X with state 1; • 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/ 28 14

  15. Post-Order traversal: stack states SDA/ TREE/ V2.0/ 29 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/ 30 15

  16. Exercise � Create an algorithm/psedo-code for in-order traversal using stack. � Create an algorithm/psedo-code for pre-order traversal using stack. SDA/ TREE/ V2.0/ 31 In-Order Traversal using Stack � What are the states for in-order traversal? 0. about to make a recursive call to left subtree 1. about to process the current node 2. about to make a recursive call to right subtree SDA/ TREE/ V2.0/ 32 16

  17. In-Order Algorithm/ Pseudocode � init: push the root into the stack with state 0 � advance: � while (true) • node X = pop from the stack • switch (state X): • case 0: • push node X with state 1; • push left child node X (if it exists) w/ state 0; • break; • case 1: • push node X with state 2; • “visit”/ ”set current to” the node X; return; • case 2: • push right child node X (if it exists) w/ state 0; • break; SDA/ TREE/ V2.0/ 33 In-Order Algorithm/ Pseudocode � init: push the root into the stack with state 0 � advance (optimize): � while (true) • node X = pop from the stack • switch (state X): • case 0: • push node X with state 1; • push left child node X (if it exists) w/ state 0; • break; • case 1: • “visit”/ ”set current to” the node X; • push right child node X (if it exists) w/ state 0; • return; SDA/ TREE/ V2.0/ 34 17

  18. In-Order: Implementation � see online code http://telaga.cs.ui.ac.id/WebKuliah/IKI101 00/resources/code/DataStructures/InOrder.j ava SDA/ TREE/ V2.0/ 35 Pre-Order Traversal using Stack � What are the states for in-order traversal? 0. about to process the current node 1. about to make a recursive call to left subtree 2. about to make a recursive call to right subtree SDA/ TREE/ V2.0/ 36 18

Recommend


More recommend