720 likes | 874 Views
Binary Search Trees. Briana B. Morrison Adapted from Alan Eugenio. Topics. Applications Binary Search Trees Retrieve Insert Delete. Ordered Dictionaries. Keys are assumed to come from a total order. New operations: closestBefore (k) closestAfter (k). Binary Search (§8.3.3).
E N D
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio
Topics • Applications • Binary Search Trees • Retrieve • Insert • Delete Binary Trees
Ordered Dictionaries • Keys are assumed to come from a total order. • New operations: • closestBefore(k) • closestAfter(k) Binary Trees
Binary Search (§8.3.3) • Binary search performs operation find(k) on a dictionary implemented by means of an array-based sequence, sorted by key • similar to the high-low game • at each step, the number of candidate items is halved • terminates after O(log n) steps • Example: find(7) 0 1 3 4 5 7 8 9 11 14 16 18 19 m h l 0 1 3 4 5 7 8 9 11 14 16 18 19 m h l 0 1 3 4 5 7 8 9 11 14 16 18 19 m h l 0 1 3 4 5 7 8 9 11 14 16 18 19 l=m =h Binary Trees
Overview of Binary Search Tree Binary search tree definition: T is a binary search tree if either of these is true • T is empty; or • Root has two subtrees: • Each is a binary search tree • Value in root > all values of the left subtree • Value in root < all values in the right subtree Binary Trees
Binary Search Trees Binary Trees
summer spring winter maybe fall always *** Binary Trees
Using Binary Search Trees Application: Removing Duplicates Binary Trees
Binary Tree Nodes Binary Trees
Searching a Binary Tree: Algorithm • if root is NULL • item not in tree: return NULL • compare target and root->data • if they are equal • target is found, return root->data • else if target < root->data • return search(left subtree) • else • return search(right subtree) • Note that in the STL, trees are not implemented with recursion but with loops Binary Trees
Current NodeAction-LOCATING DATA IN A TREE- Root = 50 Compare item = 37 and 50 37 < 50, move to the left subtree Node = 30 Compare item = 37 and 30 37 > 30, move to the right subtree Node = 35 Compare item = 37 and 35 37 > 35, move to the right subtree Node = 37 Compare item = 37 and 37. Item found. Binary Trees
80 40 90 60 50 75 WHAT ARE THE STEPS IF THE CALL IS find (60)? Binary Trees
80 40 90 60 50 75 WHAT ARE THE STEPS IF THE CALL IS find (70)? Binary Trees
Logic for Insert • Insertion is similar to find, but you must keep track of parent to be able to insert a new node • Find the spot in the tree that the value would be at if it were already in the tree, • When you reach a null link, insert the value there Binary Trees
Update Operations: 1st of 3 steps 1)- The function begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35. Binary Trees
Update Operations: 2nd of 3 steps 2)- Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35. Binary Trees
Update Operations: 3rd of 3 steps 3)- Create a leaf node with data value 32. Insert the new node as the left child of node 35. newNode = getSTNode(item,NULL,NULL,parent); parent->left = newNode; Binary Trees
insert (73) 80 40 90 60 50 75 Binary Trees
80 40 90 60 50 75 73 WILL THE INSERTED ITEM ALWAYS BE A LEAF? Binary Trees
FOR INSERTING AN ITEM, WHAT IS THE WORST CASE? WHAT IS THE WORST HEIGHT? • THE worstTime(n) IS LINEAR IN n. • WHAT IS THE AVERAGE HEIGHT? • THE averageTime(n) IS LOGARITHMIC IN n. Binary Trees
Deletion There are three possible cases to deletion: • Value to be deleted is a leaf node: just adjust parent link and delete node • Value to be deleted has one child: child takes parent’s place (value to be deleted’s parent now points to grandchild) and delete node • Value to be deleted has two children: swap and delete node Binary Trees
SUPPOSE link IS POINTING TO THE NODE WITH 50. 80 40 90 60 50 75 73 Binary Trees
80 40 90 60 75 73 Binary Trees
Removing an Item From a Binary Tree Binary Trees
WHAT IF link IS POINTING TO THE NODE WITH 40? 80 40 90 60 75 73 Binary Trees
80 60 90 75 73 Binary Trees
Removing an Item From a Binary Tree Binary Trees