Recap
BST: Search, Insert, Delete in
RB-Tree:
Duplicates
Want to store a set of numbers maybe with duplicates
S,I,D,Count in O(logn)
{1,2,3,5,2} count(2)=2
Idea
In BST, a node has right, left, key, p
At each node, to store the multiplicity of the value
- Count(x)
- search for x
- if found, return the mul
- search - stays the same
- insert
p = search(T,x) O(logn)
if p.key = x
p.mul += 1
else
create n with mult 1 and insert as usual
- delete
- mul = 1 -> delete
- mul > 1 -> mul--
Selection
Select
IN Tree t, positive integer k
OUT A node with the k-th smallest key
- How to do in O(n)
From the recitation, we can get a sorted array of key in O(n) - How to do in O(klogn)
For each node N, we will have N.size = the size of the subtree rooted at N
Select(k,T)
if T.left.size == k-1:
return T
if T.left.size >= k:
return Select(k, T.left)
else:
return Select(k-T.left.size-1, T.right)
Inseart
n.p.size ++
delete
n.p.size--
balancing
pic to be fangshang
Create a RB-tree st Node T stores an interval [l,r]
T.key = l, T.r = r, T.mr = max of r in the subtree




网友评论