View on GitHub

csc263

Notes for CSC263 - Data Structures and Analysis

Back to index

Dynamic Order Statistics

Operations

A set that supports dynamic order statistics has the operations

Note that select and rank are inverses!

Naive Implementation

“Nobody wants the naive implementation”

​ - Danny Heap


At each node $x$, store the rank of $x$

Better implementation

At each node $x$, store the size of the subtree rooted at $x$

select

def select(S, k):
    if k == RR(x):
        return x
    elif k < RR(x):
        return select(left(x), k)
    else:
        return select(right(x), k - RR(x))

Efficiency

rank

def rank(S, x):
    r = RR(x)
    y = x
    while y != root(T)
    	if y == right(parent(y))
        	r += RR(parent(y))
        y = parent(y)
    return r

Efficiency

insert, delete - Maintaining Size

After insertion/deletion, must go upwards and:

Efficiency