Complexity Of Dictionary Operations Binary Search Trees get(), put() and remove() • Dictionary Operations: Data Structure Worst Case Expected � get(key) Hash Table O(n) O(1) � put(key, value) � remove(key) Binary Search O(n) O(log n) • Additional operations: Tree � ascend() Balanced O(log n) O(log n) � get(index) (indexed binary search tree) Binary Search � remove(index) (indexed binary search tree) Tree n is number of elements in dictionary Complexity Of Other Operations Definition Of Binary Search Tree ascend(), get(index), remove(index) • A binary tree. Data Structure ascend get and remove • Each node has a (key, value) pair. Hash Table O(D + n log n) O(D + n log n) • For every node x, all keys in the left subtree of x are smaller than that in x. Indexed BST O(n) O(n) • For every node x, all keys in the right subtree of x are greater than that in x. Indexed O(n) O(log n) Balanced BST D is number of buckets
Example Binary Search Tree The Operation ascend() 20 20 10 40 10 40 6 15 30 6 15 30 25 25 2 8 2 8 Only keys are shown. Do an inorder traversal. O(n) time. The Operation get() The Operation put() 20 20 10 40 10 40 6 15 30 6 15 30 25 25 35 2 8 2 8 Complexity is O(height) = O(n), where n is number of nodes/elements. Put a pair whose key is 35.
The Operation put() The Operation put() 20 20 10 40 10 40 6 15 30 6 15 30 18 25 35 25 35 2 8 2 8 7 7 Put a pair whose key is 7. Put a pair whose key is 18. The Operation put() The Operation remove() 20 Three cases: 10 40 � Element is in a leaf. 6 15 30 � Element is in a degree 1 node. � Element is in a degree 2 node. 18 25 35 2 8 7 Complexity of put() is O(height).
Remove From A Leaf Remove From A Leaf (contd.) 20 20 10 40 10 40 6 15 30 6 15 30 18 18 25 35 25 35 2 8 2 8 7 7 Remove a leaf element. key = 7 Remove a leaf element. key = 35 Remove From A Degree 1 Node Remove From A Degree 1 Node (contd.) 20 20 10 40 10 40 6 15 30 6 15 30 18 18 25 35 25 35 2 8 2 8 7 7 Remove from a degree 1 node. key = 40 Remove from a degree 1 node. key = 15
Remove From A Degree 2 Node Remove From A Degree 2 Node 20 20 10 40 10 40 6 15 30 6 15 30 18 18 25 35 25 35 2 8 2 8 7 7 Replace with largest key in left subtree (or Remove from a degree 2 node. key = 10 smallest in right subtree). Remove From A Degree 2 Node Remove From A Degree 2 Node 20 20 10 40 8 40 6 15 30 6 15 30 18 18 25 35 25 35 2 8 2 8 7 7 Replace with largest key in left subtree (or Replace with largest key in left subtree (or smallest in right subtree). smallest in right subtree).
Remove From A Degree 2 Node Another Remove From A Degree 2 Node 20 20 8 40 10 40 6 15 30 6 15 30 18 18 25 35 25 35 2 8 2 8 7 7 Largest key must be in a leaf or degree 1 node. Remove from a degree 2 node. key = 20 Remove From A Degree 2 Node Remove From A Degree 2 Node 20 20 10 40 10 40 6 15 30 6 15 30 18 18 25 35 25 35 2 8 2 8 7 7 Replace with largest in left subtree. Replace with largest in left subtree.
Remove From A Degree 2 Node Remove From A Degree 2 Node 18 18 10 40 10 40 6 15 30 6 15 30 18 25 35 25 35 2 8 2 8 7 7 Replace with largest in left subtree. Complexity is O(height). Indexed Binary Search Tree Example Indexed Binary Search Tree 7 20 • Binary search tree. 4 3 10 40 • Each node has an additional field. 1 0 1 � leftSize = number of nodes in its left subtree 6 15 30 0 0 0 0 1 18 25 35 2 8 0 7 leftSize values are in red
leftSize And Rank leftSize And Rank 7 20 Rank of an element is its position in inorder 4 3 (inorder = ascending key order). 10 40 1 0 [2,6,7,8,10,15,18,20,25,30,35,40] 1 6 15 30 rank(2) = 0 0 0 0 0 1 18 25 35 2 8 rank(15) = 5 0 rank(20) = 7 7 leftSize(x) = rank(x) with respect to elements in sorted list = [2,6,7,8,10,15,18,20,25,30,35,40] subtree rooted at x get(index) And remove(index) get(index) And remove(index) 7 20 4 3 • if index = x.leftSize desired element is 10 40 x.element 1 0 1 • if index < x.leftSize desired element is 6 15 30 index’th element in left subtree of x 0 0 0 0 1 • if index > x.leftSize desired element is 18 25 35 2 8 (index - x.leftSize-1)’th element in right 0 subtree of x 7 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
Applications Linear List As Indexed Binary Tree (Complexities Are For Balanced Trees) 7 h 4 3 Best-fit bin packing in O(n log n) time. e l Representing a linear list so that get(index), 1 0 1 add(index, element), and remove(index) b f j run in O(log(list size)) time (uses an 0 0 0 0 1 indexed binary tree, not indexed binary g i k a d search tree). 0 c Can’t use hash tables for either of these applications. list = [a,b,c,d,e,f,g,h,i,j,k,l] add(5,’m’) add(5,’m’) 7 7 h h 4 3 4 3 e l e l 1 0 1 0 1 1 b f j b f j 0 0 0 0 0 0 0 0 1 1 g g i k i k a d a d 0 0 c c list = [a,b,c,d,e,f,g,h,i,j,k,l] list = [a,b,c,d,e, m,f,g,h,i,j,k,l] find node with element 4 (e)
add(5,’m’) add(5,’m’) 7 7 h h 4 3 4 3 e l e l 1 0 1 m 0 1 1 b f j b f j 0 0 0 0 0 0 0 0 1 1 g g i k i k a d a d 0 0 c c list = [a,b,c,d,e, m,f,g,h,i,j,k,l] add m as right child of e; former right find node with element 4 (e) subtree of e becomes right subtree of m add(5,’m’) add(5,’m’) 7 h 4 3 • Other possibilities exist. e l • Must update some leftSize values on path 1 0 1 b f j from root to new node. 0 0 0 0 • Complexity is O(height). 1 g m i k a d 0 c add m as leftmost node in right subtree of e
Recommend
More recommend