CS 332: Algorithms Dynamic Order Statistics David Luebke 1 7/1/2016 OS-Select Example ● Example: show OS-Select(root, 5): OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } David Luebke 2 M 8 C 5 A 1 P 2 F 3 D 1 Q 1 H 1 7/1/2016 OS-Select Example ● Example: show OS-Select(root, 5): OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } David Luebke 3 M 8 i=5 r=6 C 5 A 1 P 2 F 3 D 1 Q 1 H 1 7/1/2016 OS-Select Example ● Example: show OS-Select(root, 5): OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } David Luebke 4 M 8 C 5 A 1 i=5 r=6 i=5 r=2 P 2 F 3 D 1 Q 1 H 1 7/1/2016 OS-Select Example ● Example: show OS-Select(root, 5): OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } David Luebke 5 M 8 C 5 A 1 i=5 r=2 F 3 D 1 i=5 r=6 P 2 i=3 r=2 Q 1 H 1 7/1/2016 OS-Select Example ● Example: show OS-Select(root, 5): OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } David Luebke 6 M 8 C 5 A 1 P 2 i=5 r=2 F 3 D 1 i=5 r=6 i=3 r=2 H 1 Q 1 i=1 r=1 7/1/2016 OS-Select: A Subtlety OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } Oops… ● What happens at the leaves? ● How can we deal elegantly with this? David Luebke 7 7/1/2016 OS-Select OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } ● What will be the running time? David Luebke 8 7/1/2016 Determining The Rank Of An Element M 8 C 5 P 2 A 1 F 3 Q 1 D 1 H 1 What is the rank of this element? David Luebke 9 7/1/2016 Determining The Rank Of An Element M 8 C 5 P 2 A 1 F 3 Q 1 D 1 H 1 Of this one? Why? David Luebke 10 7/1/2016 Determining The Rank Of An Element M 8 C 5 P 2 A 1 F 3 Q 1 D 1 H 1 Of the root? What’s the pattern here? David Luebke 11 7/1/2016 Determining The Rank Of An Element M 8 C 5 P 2 A 1 F 3 Q 1 D 1 H 1 What about the rank of this element? David Luebke 12 7/1/2016 Determining The Rank Of An Element M 8 C 5 P 2 A 1 F 3 Q 1 D 1 H 1 This one? What’s the pattern here? David Luebke 13 7/1/2016 OS-Rank OS-Rank(T, x) { r = x->left->size + 1; y = x; while (y != T->root) if (y == y->p->right) r = r + y->p->left->size + 1; y = y->p; return r; } ● What will be the running time? David Luebke 14 7/1/2016 OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? David Luebke 15 7/1/2016 OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? ■ A: increment sizes of nodes traversed during search David Luebke 16 7/1/2016 OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? ■ A: increment sizes of nodes traversed during search ■ Why won’t this work on red-black trees? David Luebke 17 7/1/2016 Maintaining Size Through Rotation y 19 x 11 6 x 19 rightRotate(y) 7 leftRotate(x) 4 y 12 6 4 7 ● Salient point: rotation invalidates only x and y ● Can recalculate their sizes in constant time ■ Why? David Luebke 18 7/1/2016 Augmenting Data Structures: Methodology ● Choose underlying data structure ■ ● Determine additional information to maintain ■ ● E.g., subtree sizes Verify that information can be maintained for operations that modify the structure ■ ● E.g., red-black trees E.g., Insert(), Delete() (don’t forget rotations!) Develop new operations ■ David Luebke E.g., OS-Rank(), OS-Select() 19 7/1/2016