200 likes | 340 Views
Order statistics a dynamic version. rank and select. The dictionary ADT. Insert ( x,D ) Delete ( x,D ) Find ( x,D ): Returns a pointer to x if x ∊ D, and a pointer to the successor or predecessor of x if x is not in D. Suppose we want to add to the dictionary ADT.
E N D
Order statistics a dynamic version rank and select
The dictionary ADT • Insert(x,D) • Delete(x,D) • Find(x,D): Returns a pointer to x if x ∊ D, and a pointer to the successor or predecessor of x if x is not in D
Suppose we want to add to the dictionary ADT • Select(k,D): Returns the kthlargestelement in the dictionary: An element x such that k-1 elements are smaller than x
Select(5,D) 89 90 19 20 21 4 26 34 67 70 73 77
Select(5,D) 89 90 19 20 21 4 26 34 67 70 73 77
Can we still use a red-black tree ? 4 19 20 21 26 34 67 70 73 77 89 90
For each node v store # of leaves in the subtree of v 12 4 8 4 2 2 4 2 2 2 2 4 19 20 21 26 34 67 70 73 77 89 90
Select(7,T) 12 4 8 4 2 2 4 2 2 2 2 4 19 20 21 26 34 67 70 73 77 89 90
Select(7,T) 12 Select(3, ) 4 8 4 2 2 4 2 2 2 2 4 19 20 21 26 34 67 70 73 77 89 90
Select(7,T) 12 4 8 Select(3, ) 4 2 2 4 2 2 2 2 4 19 20 21 26 34 67 70 73 77 89 90
Select(7,T) 12 4 8 4 2 2 4 Select(1,) 2 2 2 2 4 19 20 21 26 34 67 70 73 77 89 90
Select(i,T) Select(i,T): Select(i,root(T)) Select(k,v): if v is a leaf thenifk = 1then return velse error if k ≤ (v.left).sizethen return Select(k,v.left)else return Select(k – (v.left).size),v.right) O(logn) worst case time
Rank(x,T) • Return the index of x in T
Rank(x,T) x Need to return 9
12 4 8 4 2 2 4 2 2 2 2 4 19 20 21 26 34 67 70 73 77 89 90 x Sum up the sizes of the subtrees to the left of the path
Insertion and deletions • Consider insertion, deletion is similar
Insert 12 8 4 2
Insert (cont) 13 9 5 3 2
Easy to maintain through rotations x y <===> y A C x A B C B size(x) ← size(B) + size(C) size(y) ← size(A) + size(x)
Summary • Insertion and deletion and other dictionary operations still take O(log n) time