1 / 65

O(lg n) Search Tree

O(lg n) Search Tree. Tree T is a search tree made up of n elements: x 0 x 1 x 2 x 3 … x n-1 No function (except transverse) takes more than O(lg n) in the worst case.   Functions: createEmptyTree() returns a newly created empty binary tree

Télécharger la présentation

O(lg n) Search Tree

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. O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the worst case.   Functions: createEmptyTree() returns a newly created empty binary tree delete(T, p) removes the node pointed to by p from the tree T insert(T, x) returns T with x added in the proper location search(T, key) returns a pointer to the node in T that has a key that matches key returns null if x is not found traverse(T) prints the contents of T in order isEmptyTree(T) returns true if T is empty and false if it is not

  2. Homework 5 • Describe how to implement a search tree that has a worst time search, insert, and delete time of no more than O(lg n). This tree should have no number of element limit. • Do the six Search Tree functions. • Discuss how you would implement this if there was a maximum to the number of elements.

  3. AVL Tree 0 54 0 +1 21 72 +1 -1 0 5 30 60 84 10 25 79 86

  4. AVL Tree • The five functions are the same. • Except that the tree needs to be rebalanced after insertion or deletion. • Keep track of the path used to insert/delete. • Balance starting at the parent of the leaf inserted or deleted. • Work up to the root.

  5. Insertion Case 1 0

  6. Insertion Case 1 -1

  7. Insertion Case 2 +1

  8. Insertion Case 2 0

  9. Insertion Case 3 +1

  10. Insertion Case 3 +2

  11. Deletion Case 1 -1

  12. Deletion Case 1 0

  13. Deletion Case 2 0

  14. Deletion Case 2 +1

  15. Deletion Case 3 +1

  16. Deletion Case 3 +2

  17. Single Rotation +2 +1

  18. Double Rotation +2 -1

  19. Single Rotation +2 +1

  20. Single Rotation A +2 B +1

  21. Single Rotation B A

  22. Single Rotation B A +2 +1

  23. Single Rotation B A 0 0

  24. Single Rotation +2 +1

  25. Single Rotation 0 +1

  26. Single Rotation +1 0

  27. Single Rotation +1 0

  28. Single Rotation +1 0

  29. Single Rotation 0 0

  30. Double Rotation +2 -1

  31. Double Rotation +1 0 0

  32. Double Rotation1 A +2 C -1 B -1

  33. Double Rotation1 B C A +2 -1 -1

  34. Double Rotation1 A +2 C -1 B -1

  35. Double Rotation1 A +2 C -1 B -1

  36. Double Rotation1 A +1 C +1 B -1

  37. Double Rotation1 A +1 B C +1 -1

  38. Double Rotation1 A +1 B C +1 -1

  39. Double Rotation1 A +1 B -1 C +1

  40. Double Rotation1 A +2 B +1 C +1

  41. Double Rotation1 A 0 B +1 C +1

  42. Double Rotation1 A B 0 +1 C +1

  43. Double Rotation1 B +1 A 0 C +1

  44. Double Rotation1 B 0 A 0 C +1

  45. Double Rotation1 B 0 A C 0 +1

  46. Double Rotation2 A +2 C -1 B +1

  47. Double Rotation2 A +2 C -1 B +1

  48. Double Rotation2 A +2 C 0 B +1

  49. Double Rotation2 A +2 B C 0 +1

  50. Double Rotation2 A +2 B +2 C 0

More Related