Lecture12: Trees II Tree Traversal Preorder Traversal Preorder Traversal

Lecture12: Trees II Tree Traversal Preorder Traversal Preorder Traversal

Tree Traversal • Process of visiting nodes in a tree systematically CSED233: Data Structures (2013F) . Some algorithms need to visit all nodes in a tree. Example: printing, counting nodes, etc. Lecture12: Trees II • Implementation . Can be done easily by recursion Computers”R”Us . Order of visits does matter. Bohyung Han Sales Manufacturing R&D CSE, POSTECH [email protected] US International Laptops Desktops Europe Asia Canada CSED233: Data Structures 2 by Prof. Bohyung Han, Fall 2013 Preorder Traversal Preorder Traversal • A preorder traversal of the subtree rooted at node n: . Visit node n (process the node's data). 1 . Recursively perform a preorder traversal of the left child. Recursively perform a preorder traversal of the right child. 2 7 • A preorder traversal starts at the root. public void preOrderTraversal(Node n) 3 6 8 12 { if (n == null) return; 4 5 9 System.out.print(n.value+" "); preOrderTraversal(n.left); preOrderTraversal(n.right); 10 11 } CSED233: Data Structures CSED233: Data Structures 3 by Prof. Bohyung Han, Fall 2013 4 by Prof. Bohyung Han, Fall 2013 Inorder Traversal Inorder Traversal • A preorder traversal of the subtree rooted at node n: . Recursively perform a preorder traversal of the left child. 6 . Visit node n (process the node's data). Recursively perform a preorder traversal of the right child. 4 11 • An iorder traversal starts at the root. public void inOrderTraversal(Node n) 2 5 7 12 { if (n == null) return; 1 3 9 inOrderTraversal(n.left); System.out.print(n.value+" "); inOrderTraversal(n.right); } 8 10 CSED233: Data Structures CSED233: Data Structures 5 by Prof. Bohyung Han, Fall 2013 6 by Prof. Bohyung Han, Fall 2013 Print Arithmetic Expressions Postorder Traversal • Specialization of an inorder traversal • A preorder traversal of the subtree rooted at node n: . print operand or operator when visiting node . Recursively perform a preorder traversal of the left child. print “(“ before traversing left subtree . Recursively perform a preorder traversal of the right child. print “)“ after traversing right subtree . Visit node n (process the node's data). 6 • A postorder traversal starts at the root. + Algorithm printExpression(v) 2 8 public void postOrderTraversal(Node n) if hasLeft (v) print(“(’’) { 1 4 79 if (n == null) printExpression (left(v)) ‐ return; print(v.element ()) 2 3 b 3 5 if hasRight (v) postOrderTraversal(n.left); printExpression (right(v)) a 1 postOrderTraversal(n.right); print (“)’’) System.out.print(n.value+" "); ((2 (a ‐ 1)) + (3 b)) } CSED233: Data Structures CSED233: Data Structures 7 by Prof. Bohyung Han, Fall 2013 8 by Prof. Bohyung Han, Fall 2013 Postorder Traversal Evaluate Arithmetic Expressions • Specialization of a postorder traversal 12 . Recursive method returning the value of a subtree . When visiting an internal node, combine the values of the subtrees 5 11 Algorithm evalExpr(v) 9 if isExternal (v) + 3 4 9 10 return v.element () 5 8 else 1 4 6 7 x evalExpr(leftChild (v)) 2 ‐ 3 2 1 2 8 y evalExpr(rightChild (v)) operator stored at v 2 3 5 1 return x y 6 7 2 5 1 ‐ x 3 2 x + CSED233: Data Structures CSED233: Data Structures 9 by Prof. Bohyung Han, Fall 2013 10 by Prof. Bohyung Han, Fall 2013 Binary Search Trees Search • A binary search tree is a binary tree storing keys at its nodes • Searching a key Algorithm TreeSearch(k, v) and satisfying the following property: . To search for a key k, we trace if T.isNull (v) . Let u, v, and w be three nodes such that u is the left child v and w is th a downward path starting at t return null e right child of v. Then, we have key(u) key(v) key(w). he root if k < key(v) return TreeSearch(k, T.left(v)) . The key value in v is larger than all keys in its left subtree and smaller . The next node visited depends than all keys in its right subtree. else if k = key(v) on the comparison of k with return v • An inorder traversal of a binary search trees visits the keys in the key of the current node increasing order. else { k > key(v) } . Search until we reach a leaf. return TreeSearch(k, T.right(v)) 6 • Example: get(4): . Call TreeSearch(4, root) 6 2 9 • The algorithms for floorEntry 2 9 1 4 8 and ceilingEntry are similar 1 4 8 CSED233: Data Structures CSED233: Data Structures 11 by Prof. Bohyung Han, Fall 2013 12 by Prof. Bohyung Han, Fall 2013 Insertion Insertion public void insert(int i) • Insertion at leaf 6 { . To perform operation 2 9 root = recursiveInsert(root,i); insert(k, o), we search for key } k using TreeSearch 1 4 8 . Assume k is not already in the private Node recursiveInsert(Node n, int i) tree, insert k by creating w as { one of the last visited node. w if (n == null) return new Node(i); • Example: insert 5 6 if (i<n.value) { // insert in the left subtree 2 9 n.left = recursiveInsert(n.left,i); return n; 1 4 8 } else { // insert in the right subtree w 5 n.right = recursiveInsert(n.right,i); return n; } } CSED233: Data Structures CSED233: Data Structures 13 by Prof. Bohyung Han, Fall 2013 14 by Prof. Bohyung Han, Fall 2013 Deletion Deletion (cont.) • Deleting a node with a child • Deleting a node with two 1 v . To perform operation remove 6 children 3 (k), we search for key k . 2 9 We consider the case where 2 8 . Assume key k is in the tree, the key k to be removed is and let let v be the node stori 1 4 v 8 stored at a node v whose 6 9 ng k . w children are both internal. w 5 5 . Simply connect parent and chi . we find the internal node w z ld of v. that follows v in an inorder traversal. • Example: remove 4 1 6 . we copy key(w) into node v. v 5 . we remove node w. 2 9 2 8 • Example: remove 3 1 5 8 6 9 CSED233: Data Structures CSED233: Data Structures 15 by Prof. Bohyung Han, Fall 2013 16 by Prof. Bohyung Han, Fall 2013 Implementation of Deletion Implementation of Deletion public void remove(int i) else { // match { if (n.left == null && n.right == null) { // no child root = recursiveRemove(root,i); return null; } } else if (n.left != null && n.right == null) { // left child only private Node recursiveRemove(Node n, int i) return n.left; { } if (n == null) // end of tree (node not found) else if (n.left == null && n.right != null) { // right child only return null; return n.right; } if (i < n.value) { // recurse left else { // two children n.left = recursiveRemove(n.left,i); Node maxLeft = findMax(n.left); // find node to replace return n; recursiveRemove(n.left, maxLeft.value); // remove the node } maxLeft.left = n.left; // set children of replacement node else if (i > n.value) { // recurse right maxLeft.right = n.right; n.right = recursiveRemove(n.right,i); return maxLeft; // return replacement node (adds it to tree) return n; } } } } CSED233: Data Structures CSED233: Data Structures 17 by Prof. Bohyung Han, Fall 2013 18 by Prof. Bohyung Han, Fall 2013 Performance • Consider ordered set items im plemented by means of a binary search tree of height . the space used is ܱ ݊ . methods get, put and remove take ܱ ݄ time. • The height is in the worst case and in the best cas e • We want a balanced binary tree! CSED233: Data Structures 20 19 by Prof. Bohyung Han, Fall 2013.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us