1 / 72

# Binary Search Trees

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).

Télécharger la présentation

## Binary Search Trees

E N D

### Presentation Transcript

1. Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio

2. Topics • Applications • Binary Search Trees • Retrieve • Insert • Delete Binary Trees

3. Ordered Dictionaries • Keys are assumed to come from a total order. • New operations: • closestBefore(k) • closestAfter(k) Binary Trees

4. 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

5. Binary Trees

6. 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

7. Binary Trees

8. Binary Trees

9. Binary Trees

10. Binary Search Trees Binary Trees

11. Binary Trees

12. Binary Trees

13. Binary Trees

14. Binary Trees

15. Binary Trees

16. Binary Trees

17. Binary Trees

18. Binary Trees

19. Binary Trees

20. Binary Trees

21. Binary Trees

22. Binary Trees

23. Binary Trees

24. Binary Trees

25. summer spring winter maybe fall always *** Binary Trees

26. Binary Trees

27. Binary Tree Nodes Binary Trees

28. 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

29. 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

30. 80 40 90 60 50 75 WHAT ARE THE STEPS IF THE CALL IS find (60)? Binary Trees

31. 80 40 90 60 50 75 WHAT ARE THE STEPS IF THE CALL IS find (70)? Binary Trees

32. Binary Trees

33. 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

34. 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

35. 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

36. 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

37. insert (73) 80 40 90 60 50 75 Binary Trees

38. 80 40 90 60 50 75 73 WILL THE INSERTED ITEM ALWAYS BE A LEAF? Binary Trees

39. 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

40. Binary Trees

41. Binary Trees

42. 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

43. Binary Trees

44. SUPPOSE link IS POINTING TO THE NODE WITH 50. 80 40 90 60 50 75 73 Binary Trees

45. 80 40 90 60 75 73 Binary Trees

46. Removing an Item From a Binary Tree Binary Trees

47. WHAT IF link IS POINTING TO THE NODE WITH 40? 80 40 90 60 75 73 Binary Trees

48. 80 60 90 75 73 Binary Trees

49. Removing an Item From a Binary Tree Binary Trees

More Related