2019-04-22 Augmenting BST

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

  1. Count(x)
    • search for x
    • if found, return the mul
  2. search - stays the same
  3. insert
p = search(T,x) O(logn) 
if p.key = x
    p.mul += 1
else
    create n with mult 1 and insert as usual
  1. delete
    • mul = 1 -> delete
    • mul > 1 -> mul--

Selection

Select
IN Tree t, positive integer k
OUT A node with the k-th smallest key

  1. How to do in O(n)
    From the recitation, we can get a sorted array of key in O(n)
  2. 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

微信图片_20190506111639.jpg
微信图片_201905061116391.jpg
微信图片_201905061116392.jpg
微信图片_201905061116393.jpg

你可能感兴趣的:(2019-04-22 Augmenting BST)