Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees (I)

Similar presentations


Presentation on theme: "Binary Search Trees (I)"— Presentation transcript:

1 Binary Search Trees (I)
COMP 103 Binary Search Trees (I) Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW Lindsay Groves School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 17

2 RECAP-TODAY RECAP Part I: Collections, Linear structures, Efficiency, Trees, Binary Trees, Recursion TODAY Part II: Binary search trees, General trees, Partially ordered trees/heaps, Sorting, Hashing Introduction to Binary Search Trees Using hierarchical access to unstructured data Reading: Chapter 17.1

3 Remember: Efficiency Challenge
Unsorted Arrays access: O(n) modification: O(n) Sorted Arrays access: O(log(n)) Linear Linked Structures access: O(n) modification: O(1) [provided we know the position] Can we have the best of both worlds? yes, using trees!

4 Recall Divide & Conquer
Optimal strategy when playing “20 questions” we want to eliminate as many as possible in each step but don’t know the answer beforehand... so ask a “question” whose answer you’re maximally uncertain about Mammal Egg Laying Feline Canine Bird Reptile Toby Tiger Lea Lion Bully Bulldog Tanja Tui Kurt Kaka Tim Turtle Sally Snake

5 Divide & Conquer Strategy
ask a “question” whose answer you’re maximally uncertain about what does this remind you of? can we improve on the below? what numbers do we choose? <19 >19 <10 >10 <51 >51 1 5 13 20 50 52 99

6 Divide & Conquer Strategy use existing elements for decision nodes 1 5
13 20 50 52 99

7 Divide & Conquer Strategy use existing elements for decision nodes
now we need less nodes can get “lucky” on the way down 20 5 52 1 13 50 99

8 Binary Search Again Searching “50” 20 5 52 1 13 50 99 1 5 13 20 50 52
mid low hi

9 “≤” if duplicates are allowed
Binary Search Trees “≤” if duplicates are allowed Properties For every node: all items in left subtree < item item < all items in right subtree Ascending order obtained by ___________ traversal 20 5 52 1 13 50 99 51 8

10 BSTNode.contains Pseudocode (recursive)
Very similar to binary search! public boolean contains (E value) { if value is at root return true; if value < item Look in left subtree, if non-empty else Look in right subtree, if non-empty return false }

11 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 is at root return true; if value < item at root 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 more flexible, use a Comparator -- later

12 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…

13 BSTNode.add pseudocode (recursive)
Notice we follow path just as in “contains”, to find the 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 a new left child, and return true else call add(value) on left child else // belongs on right if there is no right child insert as a new right child, and return true else call add(value) on right child }

14 BSTNode.add pseudocode (iterative)
public boolean add (E value) { 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

15 What’s next? Code these algorithms in Java Consider their cost
How does the order of adding values affect the cost? How can we remove an item?


Download ppt "Binary Search Trees (I)"

Similar presentations


Ads by Google