Download presentation
Presentation is loading. Please wait.
Published byJordan Little Modified over 9 years ago
1
Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)
2
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Tree Traversal Process of visiting nodes in a tree systematically Some algorithms need to visit all nodes in a tree. Example: printing, counting nodes, etc. Implementation Can be done easily by recursion Order of visits does matter. 2 Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada
3
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Preorder Traversal A preorder traversal of the subtree rooted at node n: Visit node n (process the node's data). Recursively perform a preorder traversal of the left child. Recursively perform a preorder traversal of the right child. A preorder traversal starts at the root. 3 public void preOrderTraversal(Node n) { if (n == null) return; System.out.print(n.value+" "); preOrderTraversal(n.left); preOrderTraversal(n.right); }
4
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Preorder Traversal 4 1 2 34 56 7 8 9 public void preOrderTraversal(Node n) { if (n == null) return; System.out.print(n.value+" "); preOrderTraversal(n.left); preOrderTraversal(n.right); }
5
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Inorder Traversal A preorder traversal of the subtree rooted at node n: Recursively perform an inorder traversal of the left child. Visit node n (process the node's data). Recursively perform an inorder traversal of the right child. An iorder traversal starts at the root. 5 public void inOrderTraversal(Node n) { if (n == null) return; inOrderTraversal(n.left); System.out.print(n.value+" "); inOrderTraversal(n.right); } Defined only in binary tree.
6
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Inorder Traversal 6 6 2 14 35 7 9 8 public void inOrderTraversal(Node n) { if (n == null) return; inOrderTraversal(n.left); System.out.print(n.value+" "); inOrderTraversal(n.right); }
7
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print “(“ before traversing left subtree print “)“ after traversing right subtree 7 Algorithm printExpression(v) if hasLeft (v) print(“(’’) printExpression (left(v)) print(v.element ()) if hasRight (v) printExpression (right(v)) print (“)’’) + - 2 a1 3b 3 1 2 5 6 79 8 4 ( ( ( ) ) () )
8
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Postorder Traversal A preorder traversal of the subtree rooted at node n: Recursively perform a postorder traversal of the left child. Recursively perform a postorder traversal of the right child. Visit node n (process the node's data). A postorder traversal starts at the root. 8 public void postOrderTraversal(Node n) { if (n == null) return; postOrderTraversal(n.left); postOrderTraversal(n.right); System.out.print(n.value+" "); }
9
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Postorder Traversal 9 9 5 14 23 8 7 6 public void postOrderTraversal(Node n) { if (n == null) return; postOrderTraversal(n.left); postOrderTraversal(n.right); System.out.print(n.value+" "); }
10
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Evaluate Arithmetic Expressions Specialization of a postorder traversal Recursive method returning the value of a subtree When visiting an internal node, combine the values of the subtrees 10 Algorithm evalExpr(v) if isExternal (v) return v.element () else x evalExpr(leftChild (v)) y evalExpr(rightChild (v)) operator stored at v return x y + - 2 51 32 3 1 2 5 67 9 8 4 2 5 1 ‐ x 3 2 x +
11
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Binary Search Trees A binary search tree is a binary tree storing keys at its nodes and satisfying the following property: Let u, v, and w be three nodes such that u is the left child v and w is the right child of v. Then, we have key(u) key(v) key(w). The key value in v is larger than all keys in its left subtree and smaller than all keys in its right subtree. An inorder traversal of a binary search trees visits the keys in an increasing order. 11 6 92 418 v u w
12
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Search Searching a key To search for a key k, we trace a downward path starting at the root The next node visited depends on the comparison of k with the key of the current node Search until we reach a leaf. Example: get(4) Call TreeSearch(4, root) The algorithms for floorEntry and ceilingEntry are similar. 12 Algorithm TreeSearch(k, v) if T.isExternal (v) return v if k < key(v) return TreeSearch(k, T.left(v)) else if k = key(v) return v else return TreeSearch(k, T.right(v)) 6 9 2 4 1 8
13
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Inserting a node To perform operation insert(k), we search for key k using TreeSearch Assume k is not already in the tree, and let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 13 6 9 2 4 18 w 6 92 418 5 w
14
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion 14 public void insert(int i) { root = recursiveInsert(root, i); } private Node recursiveInsert(Node n, int i) { if (n == null) return new Node(i); if (i < n.value) { // insert in the left subtree n.left = recursiveInsert(n.left,i); return n; } else { // insert in the right subtree n.right = recursiveInsert(n.right,i); return n; }
15
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Deletion Deleting a node with a child To perform operation remove(k), we search for key k Assume key k is in the tree, and let v be the node storing k Simply connect parent and child of v. Example: remove 4 15 6 9 2 4 18 5 v w 6 9 2 5 18
16
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Deletion (cont.) Deleting a node with two children We consider the case where the key k to be removed is stored at a node v whose children are both internal. We find the internal node w that follows v in an inorder traversal. we copy key(w) into node v. we remove node w. Example: remove 3 16 3 1 8 6 9 5 v w z 2 5 1 8 6 9 v 2
17
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Implementation of Deletion 17 public void delete(int i) { root = recursiveRemove(root,i); } private Node recursiveRemove(Node n, int i) { if (n == null) // end of tree (node not found) return null; if (i < n.value) { // recurse left n.left = recursiveRemove(n.left,i); return n; } else if (i > n.value) { // recurse right n.right = recursiveRemove(n.right,i); return n; }
18
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Implementation of Deletion 18 else { // match if (n.left == null && n.right == null) { // no child return null; } else if (n.left != null && n.right == null) { // left child only return n.left; } else if (n.left == null && n.right != null) { // right child only return n.right; } else { // two children Node maxLeft = findMax(n.left); // find node to replace recursiveRemove(n.left, maxLeft.value); // remove the node maxLeft.left = n.left; // set children of replacement node maxLeft.right = n.right; return maxLeft; // return replacement node (adds it to tree) }
19
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Performance 19
20
20
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.