190 likes | 286 Views
CSE 780: Design and Analysis of Algorithms. Lecture 12: Augmenting Data Structures. Balanced Binary Search Tree. Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor. Also support Select o peration ? . Augment Tree Structure. Select ( T, k ) Goal:
E N D
CSE 780: Design and Analysis of Algorithms Lecture 12: Augmenting Data Structures CSE 2331/5331
Balanced Binary Search Tree • Maximum • Extract-Max • Insert • Increase-key • Search • Delete • Successor • Predecessor Also support Select operation ? CSE 2331/5331
Augment Tree Structure • Select ( T, k ) • Goal: • Augment the binary search tree data structure so as to support Select ( T, k ) efficiently • Ordinary binary search tree • O(h) time for Select(T, k) • Red-black tree (balanced search tree) • O(lg n) time for Select(T, k) CSE 2331/5331
How To Augment Tree Structure? • At each node x of the tree T • store x.size = # nodes in the subtree rooted at x • Include x itself • If a node (leaf) is NIL, its size is 0. • Space of an augmented tree: • Basic property: CSE 2331/5331
Example M 9 C 5 A 1 F 3 D 1 H 1 P 3 T 2 Q 1 CSE 2331/5331
How to Setup Size Information? • procedure AugmentSize( ) If () then = AugmentSize( ); = AugmentSize( ); ; Return( ); end Return (); Postorder traversal of the tree ! CSE 2331/5331
Augmented Binary Search Tree • Let T be an augmented binary search tree • OS-Select(x, k): • Return the k-th smallest element in the subtree rooted at x • OS-Select(T.root, k) returns the k-th smallest elements in the entire tree. OS-Select(T.root, 5) ? CSE 2331/5331
Correctness? • Running time? • O(h) CSE 2331/5331
OS-Rank(T, x) • Return the rank of the element x in the linear order determined by an inorder walk of T CSE 2331/5331
Example M 9 C 5 A 1 F 3 D 1 H 1 P 3 T 2 Q 1 OS-Rank(T, M) ? OS-Rank(T, D) ? CSE 2331/5331
Correctness ? • Time complexity? • O(h) CSE 2331/5331
Need to maintain augmented information under dynamic operations • Insert / delete • Extract-Max can be implemented with delete Are we done ? CSE 2331/5331
Example M 9 C 5 A 1 F 3 D 1 H 1 P 3 T 2 Q 1 Insert(J) ? CSE 2331/5331
During the downward search, increase the size attribute of each node visited along the path from root to the final insert location. • Time complexity: • O(h) • However, if we have to maintain balanced binary search tree, say Red-black tree • Also need to adjust size attribute after rotation CSE 2331/5331
Left-Rotate • y.size = x.size • x.size = x.left.size + x.right.size + 1 • O(1) time per rotation CSE 2331/5331
Right-rotate can be done similarly. • Overall: Two phases: • Update size for all nodes along the path from root to insertion location • O(h) = O(lg n) time • Update size for the Fixup stage involving O(1) rotations • O(1) + O(lg n) = O(lg n) time • O(h) = O(lg n) time to insert in a Red-Black tree • Same asymptotic time complexity as the non-augmented version CSE 2331/5331
Delete • Two phases: • Decrement size in each node on the path from the root to the node to be deleted • O(h) = O(lg n) time • During Fixup (to maintain balanced binary search tree property), update size for O(1) rotations • O(1) + O(lg n) time • Overall: • O(h) = O(lg n) time CSE 2331/5331
Summary • Simple example of augmenting data structures • In general, the augmented information can be quite complicated • Can be a separate data structure! • Need to consider how to maintain such information under dynamic changes CSE 2331/5331