1 / 33

Binary Search Trees

Binary Search Trees. Speed of List Search. O(n) when implemented as a linked-list O(lg n) possible if implemented as an array. Binary Search Tree. 54. 21. 72. 5. 30. 60. 84. 10. 25. 35. 79. 86. 44. 93. Binary Search Tree.

Download Presentation

Binary Search Trees

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. Binary Search Trees

  2. Speed of List Search • O(n) when implemented as a linked-list • O(lg n) possible if implemented as an array

  3. Binary Search Tree 54 21 72 5 30 60 84 10 25 35 79 86 44 93

  4. Binary Search Tree Tree T is a binary search tree made up of n elements: x0 x1 x2 x3 … xn-1 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, p) returns T with the node pointed to by p 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 the node 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

  5. Homework 4 • Describe how to implement a binary search tree using a set of nodes (this tree will have no number of element limit). • Do the six BST functions. • Can you determine how to implement a binary search tree using an array (assume it will never have more than 100 elements)? • Consider the six BST functions.

  6. Binary Search Tree 54 21 72 5 30 60 84 10 25 35 79 86 44 93

  7. Binary Search Tree – Linked List null

  8. createEmptyTree() t null

  9. isEmptyTree(t) return t == null

  10. search(t, key) search(t, key) if t == null return null else if key == t.key return t else if key < t.key return search(t.left, key) else return search(t.right, key)

  11. search(t, key) t 52 35 80 20 48 65 null 58 70 40 30 10 45 75 15 60

  12. insert(t, p) insert(t, p) if t == null t = p else insert2(t, p) return t

  13. insert2(t, p) insert2(t, p) if p.key < t.key if t.left == null t.left = p else insert2(t.left, p) else if t.right == null t.right = p else insert2(t.right, p)

  14. traverse(t) traverse(t) if t != null traverse(t.left) print t.data traverse(t.right)

  15. delete(t, p) -- leaf t p 52 35 null null

  16. delete(t, p) -- leaf t p 52 35 t.left = null null null null

  17. delete(t, p) – single parent t p 52 35 20 null

  18. delete(t, p) – single parent t p 52 35 t.left = p.left 20 null

  19. delete(t, p) t p 52 35 20 41 46

  20. delete(t, p) t p 52 35 successor 20 41 46

  21. successor(t, p) t 50 36 80 20 48 65 null 58 70 40 30 10 45 75 15 60 39

  22. delete(t, p) t p 52 35 successor 20 41 46

  23. delete(t, p) t p 52 delete(p, successor) 35 successor 20 41 46

  24. delete(t, p) t p 52 t.left = successor 35 successor 20 41 46

  25. delete(t, p) t successor.left = p.left successor.right = p.right p 52 35 successor 20 41 46

  26. delete(t, p) t 50 36 80 20 48 65 null 58 70 40 30 10 45 75 15 60 39

  27. Binary Search Tree -- Array 0 1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child

  28. Binary Search Tree -- Array 0 1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child

  29. Binary Search Tree -- Array 0 1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child

  30. Binary Search Tree -- Array • Advantages • fast • can access the parent from a child • Disadvantages • fixed size

  31. Speed of Binary Search Trees In the worst case a BST is just a linked list. t 20 35 48 52 65 80 O(n) How can we guarantee O(lg n) null

  32. 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, p) returns T with the node pointed to by p 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 the node 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

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

More Related