Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Binary Search Trees.

Similar presentations


Presentation on theme: "COMP 103 Binary Search Trees."— Presentation transcript:

1 COMP 103 Binary Search Trees

2 RECAP-TODAY RECAP Tree traversals TODAY: Binary Search Trees
Using tree data structures for implementing Sets, Bags and Maps Contains, add Another sort! Remove READING Chapter 17, sections 1 to 3

3 Recap – traversals on trees
Depth-first vs. breadth-first Breadth-first: an iterative algorithm, that uses a queue! Depth-first: pre-order vs. post-order naturally recursive algorithm but you can also do it iteratively, using a stack. if it’s a binary tree, can have in-order traversals it’s not so obvious how to do this iteratively though... with otherwise identical code!

4 Implementing Sets, Bags and Maps
We’ve seen several ways to implement these collections: Add on set has to check whether it’s already there: O(log n) for sorted array and O(n) otherwise. Also finds place to insert for sorted array or list. Actual insertion is O(n) for sorted array and O(1) otherwise Data Structure Add Find Remove Unsorted Array Bag: O(1) Set: O(n) O(n) O(n) +O(1) Sorted Array O(log n) O(log n) + O(n) Unsorted Linked List O(n)+O(1) Sorted Linked List

5 Implementing Sets, Bags and Maps
The problem: Finding in sorted arrays is fast – use binary search. Finding in linked lists is slow. Inserting/removing in sorted arrays is slow. Inserting/removing in linked lists is fast once you’ve found the right place, and done member check!! Can we get lookup speed of binary search and insert/remove speed of linked list ?  Note that binary search imposes a tree structure on top of the array.  Binary search tree a binary tree that mimics the binary search structure a linked tree structure that makes insertion/removal fast

6 Binary Search in a sorted array
Imposes a tree structure on the array: Same steps as if we searched in this tree structure: Tree structure allows fast add and remove of linked structures Jack Jacob Jade Jas Jeremy John Joshua Julia Justin 1 2 3 4 5 6 7 8 Jeremy Jacob Jack Jade John Julia Joshua Jas Jill Justin

7 Binary Search Trees A binary tree with an ordering property:
At every node: item in node > all items in left subtree (≥ if duplicates allowed) item in node < all items in right subtree The order in the tree makes searching efficient just as in sorted arrays. Note: in-order traversal lists items in ascending order! Jeremy Jade Joshua Jack Jasmine John Julia Justin Jacob

8 BSTs for Set (or Bag or Map)
Implement a Set as an object containing a count and a reference to the root node of a binary tree of BSTNodes: public class BSTSet <E> extends AbstractSet { private int count; private BSTNode root; private class BSTNode { public E item; public BSTNode left, right; } Cost of contains, add, remove? How to implement the operations? Optional – makes size() constant, but need to adjust as elements are added/removed. Omit if size() is seldom used. count root BSTSet Jeremy Jade Jack Jasmine John Julia Joshua Justin Jacob BSTNodes

9 worst case, and average case
Cost of BST operations worst case, and average case 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. count root O G U C K S X n nodes A E I M Q T V Z .. .. .. .. .. .. .. .. .. .. .. ..

10 Algorithm / Pseudocode for contains, in BSTSet
Follow path from root to place where it be if it was there. public boolean contains (E value) { set currentNode to root while currentNode is not null if value == item in currentNode return true; if value < item in currentNode set currentNode to left child else set currentNode to right child return false } Ex: Write a recursive version Ex: Adapt to return the node containing value. you found it... To be really general, use compareTo – see text book.

11 Algorithm for add, in BSTSet
Follow path as in contains to find insertion point. Always a leaf! public boolean add (E value) { if root is null insert at root, and return true set parentNode to root; // Find the parent node to insert new node while true if value equals item in parentNode // value already in set return false; if value is less than item in parentNode // belongs on left if left child of parentNode is null insert as left child, and return true else set parentNode to left child of parentNode else // belongs on right if right child is null insert as right child, and return true else set parentNode to right child of parentNode special case

12 Algorithm for add, in BSTSet
insert at root: set root to new node containing value, increment count insert as left child: set left child of parentNode to new node with value, insert as right child: set right child of parentNode to new node with value,

13 Adding items to a BSTSet
Add new item: Jeremy Jade Jasmine Joshua Jack Jacob John Julia Justin count root Jeremy Jade Joshua Jack Jasmine John Julia Justin Jacob

14 Tree Sort To sort a list: What is the best, average, and worst cost?
Insert each item into a BST. In-order traverse the BST, copying items back into list. What is the best, average, and worst cost? What if the tree is automatically kept balanced at all times? Sorted list is the worst for it! While insertion sort is the best for sorted list.

15 Balanced and Unbalanced BSTs
O G C K S X U E M I T Q Z V A O M C K S X U E A I G T Q Z V A V U T X G C M K S O Z E I Q see the discussion of “rotations”, in the text book

16 Definitions: balanced trees, complete trees
if all the leaves are at the same level? (“full”) if all the leaves are approximately the same depth? 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 T Z V

17 Balancing a Binary Search Tree
One way: Balance the tree periodically by writing the elements to a list and rebuilding the tree. Write elements to list: What algorithm? Cost? Rebuild tree: When/how often should we do this? Sorted list is the worst for it! While insertion sort is the best for sorted list.

18 Balancing a Binary Search Tree
Another idea: record the “size” of each subtree. When deleting, promote from the side with bigger size. And another: after inserting, check to see if size of one subtree exceeds the other by more than a specified amount, and if so move one of more elements from that side to the other. What should we use as the “size” of a subtree? Number of elements? Height? How much difference should we allow before moving elements? See discussion of rotations in text book. AVL trees: height balanced – heights of subtrees differ by at most one. Sorted list is the worst for it! While insertion sort is the best for sorted list.

19 Cost of Contains: Worst case cost: Average case cost: Balanced tree:
what is the worst case? Average case cost: what is the average case? Balanced tree: worst case cost average case cost Unbalanced tree:

20 Cost of adding Add a node at the place we would find it
as a new leaf of the tree special case: the root Cost: balanced, average unbalanced, worst case count root O G U C K S X A E I M Q T V Z


Download ppt "COMP 103 Binary Search Trees."

Similar presentations


Ads by Google