1 / 60

Lecture 26

Lecture 26. Welcome back! Binary search trees 15.1-5. Binary Search Trees. Dictionary Operations: get(key) put(key, value) remove(key) Additional operations: ascend() get(index) (indexed binary search tree) remove(index) (indexed binary search tree).

mia-paul
Télécharger la présentation

Lecture 26

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 26 • Welcome back! • Binary search trees • 15.1-5

  2. Binary Search Trees • Dictionary Operations: • get(key) • put(key, value) • remove(key) • Additional operations: • ascend() • get(index) (indexed binary search tree) • remove(index) (indexed binary search tree)

  3. n is number of elements in dictionary Complexity Of Dictionary Operationsget(), put() and remove()

  4. D is number of buckets Complexity Of Other Operationsascend(), get(index), remove(index)

  5. Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree of x are smaller than that in x. For every node x, all keys in the right subtree of x are greater than that in x.

  6. Example Binary Search Tree 20 10 40 6 15 30 25 2 8 Only keys are shown.

  7. 20 10 40 6 15 30 25 2 8 The Operation ascend() Do an inorder traversal. O(n) time.

  8. 20 10 40 6 15 30 25 2 8 The Operation get() 8 Get pair whose key is 8

  9. The Operation get() 20 8 10 40 6 15 30 25 2 8 Get pair whose key is 8

  10. The Operation get() 20 10 40 8 6 15 30 25 2 8 Get pair whose key is 8

  11. The Operation get() 20 10 40 6 15 30 8 25 2 8 Get pair whose key is 8

  12. The Operation get() 20 10 40 6 15 30 25 2 8 8! Get pair whose key is 8

  13. The Operation get() 20 10 40 6 15 30 25 2 8 Complexity is O(height) = O(n), where n is number of nodes/elements.

  14. The Operation put() 35 20 10 40 6 15 30 25 2 8 Put a pair whose key is 35.

  15. The Operation put() 20 10 40 35 6 15 30 25 2 8 Put a pair whose key is 35.

  16. The Operation put() 20 10 40 6 15 30 35 25 2 8 Put a pair whose key is 35.

  17. The Operation put() 20 10 40 6 15 30 25 35 2 8 Put a pair whose key is 35.

  18. The Operation put() 20 7 10 40 6 15 30 25 35 2 8 Put a pair whose key is 7.

  19. The Operation put() 20 10 40 7 6 15 30 25 35 2 8 Put a pair whose key is 7.

  20. The Operation put() 20 10 40 6 15 30 7 25 35 2 8 Put a pair whose key is 7.

  21. The Operation put() 20 10 40 6 15 30 25 35 2 8 7 Put a pair whose key is 7.

  22. The Operation put() 20 10 40 6 15 30 25 35 2 8 7 Put a pair whose key is 7.

  23. The Operation put() 20 10 40 6 15 30 18 25 35 2 8 7 Put a pair whose key is 18.

  24. The Operation put() 20 10 40 6 15 30 18 25 35 2 8 7 Complexity of put() is O(height).

  25. The Operation remove() • Three cases: • Element is in a leaf. • Element is in a degree 1 node. • Element is in a degree 2 node.

  26. Remove From A Leaf 20 10 40 6 15 30 18 25 35 2 8 7 Remove a leaf element. key = 7

  27. Remove From A Leaf 20 10 40 6 15 30 18 25 35 2 8 Remove a leaf element. key = 7

  28. Remove From A Leaf (contd.) 20 10 40 6 15 30 18 25 35 2 8 7 Remove a leaf element. key = 35

  29. Remove From A Leaf (contd.) 20 10 40 6 15 30 18 25 2 8 7 Remove a leaf element. key = 35

  30. Remove From A Degree 1 Node 20 10 40 6 15 30 18 25 35 2 8 7 Remove from a degree 1 node. key = 40

  31. Remove From A Degree 1 Node 20 10 6 15 30 18 25 35 2 8 7 Remove from a degree 1 node. key = 40

  32. Remove From A Degree 1 Node 20 10 6 15 30 18 25 35 2 8 7 Remove from a degree 1 node. key = 40

  33. Remove From A Degree 1 Node (contd.) 20 10 40 6 15 30 18 25 35 2 8 7 Remove from a degree 1 node. key = 15

  34. Remove From A Degree 1 Node (contd.) 20 10 40 6 30 18 25 35 2 8 7 Remove from a degree 1 node. key = 15

  35. Remove From A Degree 1 Node (contd.) 20 10 40 6 30 18 25 35 2 8 7 Remove from a degree 1 node. key = 15

  36. Remove From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Remove from a degree 2 node. key = 10

  37. Remove From A Degree 2 Node 20 40 6 15 30 18 25 35 2 8 7 How to patch tree back together?

  38. Remove From A Degree 2 Node 20 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

  39. Remove From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Largest (smallest) key in left (right) subtree must be in a leaf or degree 1 node.

  40. Remove From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 7 Take the 1 (possibly empty) subtree of the replacement node and promote it.

  41. Remove From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 7 Done

  42. Another Remove From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Remove from a degree 2 node. key = 20

  43. Remove From A Degree 2 Node 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

  44. Remove From A Degree 2 Node 18 10 40 6 15 30 25 35 2 8 7 No left child to promote this time.

  45. Remove From A Degree 2 Node 18 10 40 6 15 30 25 35 2 8 7 Complexity is O(height).

  46. Indexed Binary Search Tree • Binary search tree • Each node has an additional field • leftSize = number of nodes in its left subtree

  47. Example Indexed Binary Search Tree 7 20 4 3 10 40 1 0 1 6 15 30 0 0 0 0 1 18 25 35 2 8 0 7 leftSize values are in red

  48. leftSize And Rank Rank of an element is its position in inorder (inorder = ascending key order). [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7 leftSize(x) = rank(x)with respect to elements in subtree rooted at x

  49. leftSize And Rank 7 20 4 3 10 40 1 0 1 6 15 30 0 0 0 0 1 18 25 35 2 8 0 7 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

  50. 7 20 4 3 10 40 1 0 1 6 15 30 0 0 0 0 1 18 25 35 2 8 0 7 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40] get(index) And remove(index)

More Related