Advanced Data Structures and Implementation
Total Page:16
File Type:pdf, Size:1020Kb
www.getmyuni.com Advanced Data Structures and Implementation • Top-Down Splay Trees • Red-Black Trees • Top-Down Red Black Trees • Top-Down Deletion • Deterministic Skip Lists • AA-Trees • Treaps • k-d Trees • Pairing Heaps www.getmyuni.com Top-Down Splay Tree • Direct strategy requires traversal from the root down the tree, and then bottom-up traversal to implement the splaying tree. • Can implement by storing parent links, or by storing the access path on a stack. • Both methods require large amount of overhead and must handle many special cases. • Initial rotations on the initial access path uses only O(1) extra space, but retains the O(log N) amortized time bound. www.getmyuni.com www.getmyuni.com Case 1: Zig X L R L R Y X Y XR YL Yr XR YL Yr If Y should become root, then X and its right sub tree are made left children of the smallest value in R, and Y is made root of “center” tree. Y does not have to be a leaf for the Zig case to apply. www.getmyuni.com www.getmyuni.com Case 2: Zig-Zig L X R L R Z Y XR Y X Z ZL Zr YR YR XR ZL Zr The value to be splayed is in the tree rooted at Z. Rotate Y about X and attach as left child of smallest value in R www.getmyuni.com www.getmyuni.com Case 3: Zig-Zag (Simplified) L X R L R Y Y XR X YL YL Z Z XR ZL Zr ZL Zr The value to be splayed is in the tree rooted at Z. To make code simpler, the Zig-Zag rotation is reduced to a single Zig. This results in more iterations in the splay process. www.getmyuni.comReassembling the Splay Tree L X R X L R XL XR XL XR When the value to be splayed to the root is at the root of the “center” tree, we have reached the point where we are ready to reassemble the tree. This is accomplished by a) making XL the right child of the maximum element in L, b) making XR the left child of the minimum element in R, and then making L and R the left and right children of X www.getmyuni.com www.getmyuni.com Operation 1: Zig-Zig L A R L R C B B Ar D Cr A C Br E Dl Br Ar Cr Er D F E Dl G Fl Er F H Gl G X Fl Hl H Xl Xr Gl X Hl Rotate B Xl Xr around A L is still empty, and and make L R is now the tree child of rooted at B. Note minimum that R contains nodes element in > X but not in the R (which is right subtree of X. now empty) www.getmyuni.com Operation 2: Zig-Zag L C R L R E B D B D Cr Er A F E Dl C A Dl G Fl Er Br Ar F H Cr Br Ar Gl G X Fl Hl H Xl Xr Gl X Hl Xl Xr Just perform Zig L was previously (simplified empty and it now Zig-Zag) consists of node D and D’s left subtree After X reaches root: www.getmyuni.com R L X This D configuration Xl Xr B was achieved Dl G C A by doing Zig H Zig (of F, G) F Cr Br Ar E followed by a Hl Fl Gl Er Zig (node H) Reassemble – XL becomes right sub tree of H, XR becomes left sub tree of X E, and then L, R reattached to X B D C A Dl G Note that this is not the Cr Br Ar same tree as was obtained H E F by doing BU splaying. Hl Xl Er Fl Gl www.getmyuni.com www.getmyuni.com Red-Black Tree • Popular alternative to the AVL tree. • Operations take O(log N) time in worst case. • Height is at most 2log(N+1). • A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. • The root is black. • If node is red, its children must be black. • Every path from a node to a null reference must contain the same number of black nodes. • Basic operations to conform with rules are color changes and tree rotations. www.getmyuni.com Theorem 1 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node. www.getmyuni.com Theorem 2 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem1, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path. www.getmyuni.com Theorem 3 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 1, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h www.getmyuni.com Bottom-Up Insertion • Cases: • 0: X is the root – color it black • 1: Both parent and uncle are red – color parent and uncle black, color grandparent red, point X to grandparent, check new situation • 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children – color grandparent red, color X black, rotate left on parent, rotate right on grandparent • 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children – color parent black, color grandparent red, rotate right on grandparent www.getmyuni.com Top-Down Red-Black Trees • In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. • When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. • So, T-Down insertion can be done iteratively which is generally faster. • Insertion is always done as a leaf (as in ordinary BST insertion). www.getmyuni.com Process • On the way down, when we see a node X that has two red children, we make X red and its two children black. • If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. • X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case. www.getmyuni.com Example: Insert 45 30 15 70 60 85 10 20 Two red 80 90 50 65 children 5 40 55 www.getmyuni.com Example (Cont.) 30 15 70 85 10 20 60 flip colors - 80 90 50 65 two red nodes 5 40 55 www.getmyuni.com Example (Cont.): Do a single rotation 30 15 60 20 70 10 50 85 40 55 65 5 80 90 www.getmyuni.com Example (Cont.): Now Insert 45 30 15 60 20 70 10 50 85 40 55 65 5 80 90 45 www.getmyuni.com Note • Since the parent of the newly inserted node was black, we are done. • Had the parent of the inserted node been red, one more rotation would have had to be performed. • Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees. www.getmyuni.comImplementation of Top-Down Red- Black Trees • See pages 464-467 www.getmyuni.com Top-Down Deletions • Recall that in deleting from a binary search tree, the only nodes which are actually removed are leaves or nodes with exactly one child. • Nodes with two children are never removed. Their contents are just replaced. • If the node to be deleted is red, there is no problem - just delete the node. • If the node to be deleted is black, its removal will violate property. • The solution is to ensure that any node to be deleted is red. www.getmyuni.com Deterministic Skip Lists • A probabilistically balanced linked list. • Invented in 1986 by William Pugh. • Definition: Two elements are linked if there exists at least one link going from one to another. • Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them. www.getmyuni.com Skip List d) xtra pointers every eighth item - full structure 9 21 NIL 3 6 7 12 17 19 25 26 e) skip list - same link distribution, random choice 6 25 NIL 3 7 9 12 17 19 21 26 www.getmyuni.com Search time • In the deterministic version (a-d): • in a, we need to check at most n nodes • in b, at most n/2+1 nodes • in c, at most n/4+2 nodes • in general, at most log N nodes • Efficient search, but impractical insertion and deletion.