Take the basic data structure and add new operations on it.
Dynamic order statistics:
OS-Select(i)--returns ith smallest item in dynamic set.
OS-Rank(x)--returns rank of x in sorted order.
Idea: keep sub-tree sizes in nodes of RB-tree.
Size[x]=size[left[x]]+size[right[x]]+1
Trick: use sentinel (dummy record) for nil. Size[nil]=0
Ex. OS-Select(root,5) --> ptr to “H”
Analysis: O(logn)
OS-Rank in CLRS
Q: Why not keep ranks in nodes instead of sub-tree sizes?
A: Hard to maintain when RB-tree is modified.
Modifying ops: Insert Delete
Strategy: Update subtree sizes when inserting or deleting.
Ex. Insert “K”. But must also handle rebalancing.
---RB color changes: no effect
---rotations: look at children and fix up in O(1) time
Ex.
Insert,Delete still O(logn) time.
Data-structure augmentation
Methodology.(Ex. OStrees)
数据结构 1.Choose underlying data structure(red-black tree);
附加信息 2.Determine additional info(subtree sizes);
保持信息 3.Verify that info can be maintained for modify operations(insert and delete---rotations);
新的操作 4.Develop new operations that use info(OS-Select, OS-Rank)
Usually, must play with interactions between steps.
Interval trees
Maintain a set of intervals e.g. Time intervals
Query: Find an interval in the set that overlaps a given query interval.
Methodology:
1. red-black tree keyed on low endpoint;
2. Store in node the largest value m in the sub-tree rooted at that node;
3. Modifying ops
Insert: Fix m's on way down.
But, also need to handle rotations. Fixing up m's during rotation takes O(1) time.
=> Insert time=O(logn), Delete similar.
4. New operations.
Ex. [14,16] -> [15,18]
[12,14]-> nil
Time=O(logn)
List all overlaps: O(klogn) “output sensitive”: the running time depends upon #outputs.
Best to date=O(k+logn)
Correctness: