Presentation is loading. Please wait.

Presentation is loading. Please wait.

2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.

Similar presentations


Presentation on theme: "2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas."— Presentation transcript:

1 2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW COMP 103 Marcus Frean Binary Search Trees IV

2 2 RECAP-TODAY  RECAP  Binary search trees: binary tree with ordering constraint  pseudocode for add and contains on a BST  TODAY  remove from a BST

3 3 BST: Remove  how would you remove... 1.a leaf node? 2.a node with one child? 3.a node with two children? O O G G C C K K S S X X U U E E count root A A M M I I T T Q Q V V

4 4 BST: Remove Two Easy Cases: 1.Node has no children ⇒ just remove it 2.Node has one child ⇒ pull up the child into the node’s place

5 5 BST: Remove – 0 or 1 child Remove: Anton Lili Des Eve Fred Berta Anton Eve Kim Martha Des Lili Otto Pete Nina Gert Celia Joe Hans count root

6 6 …OR that it contains the minimum value in the right subtree BST: Remove A good way to think of the “leftmost node“ in the right subtree is that it is the successor in an in-order traversal… O O G G C C K K S S X X U U E E A A M M T T Q Q V V Third, more interesting case: 3.Node has two children: ⇒ Put leftmost node of the right subtree in place of the node simple case: right child has no left child ⇒ the right child is the leftmost! less simple case:right child has a left subtree…

7 7 BST: Remove – 2 children  a) Remove Martha  In-order successor is Nina  no need for further work since Nina was a leaf  b) Remove Berta  In-order successor is Celia  Celia is easy to replace by “pulling up” her only child Fred Berta Kim Martha Des Lili Otto Pete Nina Gert Celia Joe Hans count root

8 8 BST: Remove – 2 children  c) Remove Fred  in-order successor is Gert  Gert had a right child…  …but is easy to replace by “pulling up” his only child  Fred happened to be the root  need to update “root”?  definitely if Fred had been the last node Fred Berta Anton Eve Kim Martha Des Lili Otto Pete Nina Gert Celia Joe Hans count root the buck always stops here, i.e., this is the hardest case! Demo: www.cs.usfca.edu/~galles/visualization/BST.html (ie. google “BST visualization galles”) - note they go with the “other side” (they pull up right-most of the left child)

9 9 Remove: Recursive Function in BSTNode public BSTNode remove (E value) { if value equals this.node return replacementSubtreeFromChildren (this.left, this.right) // could not solve easily, have to defer to children if value smaller than this.item set this.left to result of this.left.remove(value) else // value is greater than this.item set this.right to result of this.right.remove(value) return this } assuming value is in tree, i.e., will be found. (“null” is not enough to encode both “could not be removed” and “result subtree is empty”) only children changed update the respective child returns a reference to a tree that has the element removed

10 10 helper function for recursive remove (in BSTNode) public BSTNode replacementSubtreeFromChildren(left, right) { if left and right are not both present return the one present (or null) set leftmostNode to the leftmost node within this.right set this.value to be leftmostNode.value // now that leftmostNode’s value is copied, leftmostNode can be removed set this.right to be the result of right.remove(value) return this } easy cases node remains, just value and right child changed copy value returns a reference to a subtree that contains all children from ‘left’ and ‘right’

11 11 Remove: Iterative Version in BSTSet public boolean remove (E value) { if tree is empty or value is invalidreturn false if value is in root set root to removeRootOf(root), decrement count, return true set currentNode to root; // look for parent of node containing value forever if value is less than currentNode.item if there is no left child return false if value is in left child set left child of currentNode to removeRootOf(left child) decrement count,return true else set currentNode to left child else // value is greater than item in currentNode if right child is nullreturn false if value is in right child set right child of currentNode to removeRootOf(right child) decrement count,return true else set currentNode to right child special case of root continue search

12 12 the helper for iterative form of remove (in BSTSet) Given a subtree, this returns a new subtree with the root node of the subtree removed: private BSTNode removeRootOf( BSTNode subroot) { if subroot has no children, return null if subroot has only one child, return that child set rightChild to be right child of subroot if rightChild has no left subtree set left child of rightChild to be left child of subroot return rightChild // find parent of leftmost node in right subtree set parent to rightChild while parent has a left-left grandchild { set parent to its left child } // parent’s left child is now the leftmost node in right subtree set leftmostNode to left child of parent set parent’s left node to right child of leftmostNode set right child of leftmostNode to rightChild set left child of leftmostNode to left child of subroot; return leftmostNode; simple case easy cases tricky case


Download ppt "2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas."

Similar presentations


Ads by Google