1 / 18

CSE 780: Design and Analysis of Algorithms

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:

tomai
Télécharger la présentation

CSE 780: Design and Analysis of Algorithms

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. CSE 780: Design and Analysis of Algorithms Lecture 12: Augmenting Data Structures CSE 2331/5331

  2. Balanced Binary Search Tree • Maximum • Extract-Max • Insert • Increase-key • Search • Delete • Successor • Predecessor Also support Select operation ? CSE 2331/5331

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

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

  5. Example M 9 C 5 A 1 F 3 D 1 H 1 P 3 T 2 Q 1 CSE 2331/5331

  6. How to Setup Size Information? • procedure AugmentSize( ) If () then = AugmentSize( ); = AugmentSize( ); ; Return( ); end Return (); Postorder traversal of the tree ! CSE 2331/5331

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

  8. Correctness? • Running time? • O(h) CSE 2331/5331

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

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

  11. Correctness ? • Time complexity? • O(h) CSE 2331/5331

  12. Need to maintain augmented information under dynamic operations • Insert / delete • Extract-Max can be implemented with delete Are we done ? CSE 2331/5331

  13. Example M 9 C 5 A 1 F 3 D 1 H 1 P 3 T 2 Q 1 Insert(J) ? CSE 2331/5331

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

  15. Left-Rotate • y.size = x.size • x.size = x.left.size + x.right.size + 1 • O(1) time per rotation CSE 2331/5331

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

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

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

More Related