Download presentation
Presentation is loading. Please wait.
Published byVictoria Fowler Modified over 9 years ago
1
2014-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 III
2
2 RECAP-TODAY RECAP motivation and introduction to Binary Search Trees TODAY: Binary Search Trees basic operations: add, contains Reading: Chapter 17.2 ANNOUNCEMENTS: Mandatory Requirements
3
3 BSTs for Set (or Bag or Map) Implement a Set as an object maintaining a count and a reference to the root of a binary tree of BSTNodes: public class BSTSet extends AbstractSet { private int count; private BSTNode root; … private class BSTNode { public E item; public BSTNode left, right; … } count root BSTSet Fred Celia Anton Des Hans Kim Joe Zoe Berta BSTNodes why not simply count nodes?
4
4 Cost of BST operations If a BST contains n nodes, what is the cost of contains: find an item in the tree _______ add: find the place to put it and insert _______ remove: find the item and splice it out _______ 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 Z Z V V.. … n nodes worst case, and average case?
5
5 Adding items to a BSTSet Add new item: Fred Celia Des Joe Anton Berta Hans Kim Zoe Fred Celia Anton Des Hans Kim Joe Zoe Berta count root
6
6 Cost of adding Add a node at the place we would find it recurse until future parent has been found add as a new leaf of the tree special case: the root Cost: balanced, average___________ unbalanced, worst case___________ 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 Z Z V V
7
7 BSTSet.add Pseudocode (+ recursive) public boolean add(E value) { if set is empty create new root with item value increment count return true else ask root node to add a node with item value if addition was successful increment count return true return false } special case
8
8 BSTNode.add Pseudocode (recursive) Follow path as in contains to find insertion point public boolean add (E value) { if value equals item return false // item already present if value < item // belongs on left if there is no left child insert as left child, and return true else add value to left child else // belongs on right if there is no right child insert as right child, and return true else add value to right child }
9
9 BSTSet.add Pseudocode (iterative) Follow path as in contains to find insertion point public boolean add (E value) { if set is empty create new root with value, increase count and return true set parentNode to root; // Find the parent node to insert new node forever if value equals item in parentNode// value already in set return false; if value < item in parentNode// belongs on left if parentNode has no left child insert as left child, increase count, and return true else set parentNode to left child of parentNode else // belongs on right if parentNode has no right child insert as right child, increase count, and return true else set parentNode to right child of parentNode
10
10 BSTNode.contains Pseudocode (recursive) Follow path from root to place where it would be if it were there. public boolean contains (E value) { if value equals item return true; if value < item if there is a left child return whether left child contains value else if there is a right child return whether right child contains value return false } To be most flexible, use a Comparator, cf. sorting algorithms
11
11 BSTSet.contains Pseudocode (iterative) Follow path from root to place where it would be if it were there. public boolean contains(BSTNode node, E value) { while node points to a node if node.item equals value return true; if value < node.item set node to left child of node else set node to right child of node return false } modifying parameter variable…
12
12 Tree Sort – Building & Traversing a BST To sort a list: Insert each item into a BST In-order traverse the BST What is the complexity? average case_______________ worst case_______________ avoid by balancing!
13
13 Definitions: Balanced trees & Complete trees 1. if all the leaves are at the same level? (“full”) 2. if all the leaves are approximately the same depth 3. if all the leaves are in the bottom two levels Complete tree: balanced (defn. 3), and all leaves on bottom row are to the left O G C K S X U E A M I Q O G C K S X U E A M I T Q Z V
14
14 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.