330 likes | 398 Views
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.
E N D
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 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
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.
Binary Search Tree 54 21 72 5 30 60 84 10 25 35 79 86 44 93
createEmptyTree() t null
isEmptyTree(t) return t == null
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)
search(t, key) t 52 35 80 20 48 65 null 58 70 40 30 10 45 75 15 60
insert(t, p) insert(t, p) if t == null t = p else insert2(t, p) return t
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)
traverse(t) traverse(t) if t != null traverse(t.left) print t.data traverse(t.right)
delete(t, p) -- leaf t p 52 35 null null
delete(t, p) -- leaf t p 52 35 t.left = null null null null
delete(t, p) – single parent t p 52 35 20 null
delete(t, p) – single parent t p 52 35 t.left = p.left 20 null
delete(t, p) t p 52 35 20 41 46
delete(t, p) t p 52 35 successor 20 41 46
successor(t, p) t 50 36 80 20 48 65 null 58 70 40 30 10 45 75 15 60 39
delete(t, p) t p 52 35 successor 20 41 46
delete(t, p) t p 52 delete(p, successor) 35 successor 20 41 46
delete(t, p) t p 52 t.left = successor 35 successor 20 41 46
delete(t, p) t successor.left = p.left successor.right = p.right p 52 35 successor 20 41 46
delete(t, p) t 50 36 80 20 48 65 null 58 70 40 30 10 45 75 15 60 39
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
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
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
Binary Search Tree -- Array • Advantages • fast • can access the parent from a child • Disadvantages • fixed size
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
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
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.