2020-05-18Original-language archivelegacy assets may be incomplete
Dynamic sets
operations
Search(S, k)
Insert(S, x)
Delete(S, x)
Min/Max(S)
Successor(S, x)
Precessor(S, x)
data structure augmentation
choose an underlying data structure
determine addtional information to maintain
verify it can be maintained
develop new operations
优先队列
operation
Insert(S, x)
Max(S)
extract_max(S)
increase_key(S, x, k)
not good for Search
线性数据结构
Stack
Queue
Linked list
堆
property
max-heap property: A[Parent(i)]≥A[i]
procedure
max-heapify O(lgn)
build-max-heap O(n)
max-heap-insert/extract/increase O(lgn)
def max_heapify(A, i): # l, r are already max-heaps l = 2 * i r = 2 * i + 1 if l <= A.size and A[l] > A[i]: largest = l else: largest = i if r <= A.size and A[r] > A[largest]: largest = r if largest != i: swap(A[i], A[largest]) mex_heapify(A, largest)
哈希表
m slots store n elements
hash function: simple uniform hashing
h(k)=⌊km⌋
h(k)=kmodm
h(k)=⌊m(kAmod1)⌉
universal hashing: P(h(k)=h(l))≤m∣H∣
hab(k)=((ak+b)modp)modm,H={hab:a∈Zp∗,b∈Zp}
collision resolution
chaining O(1+mn) (worst case Θ(n))
open addressing
linear probing
quadratic probing: h(k,i)=(h′(k)+c1i+c2i2)modm
double hashing: h(k,i)=(h1(k)+ih2(k))modm
perfect hashing: two level hashing, O(1)
二叉搜索树
property: left < root < right
search, min/max, succ/pre: O(h)
void bst_insert(T, z){ y = NULL x = T.root while (x){ y = x; if z.key < x.key x = x.left else x = x.right } z.p = y; if (!y) T.root = z; else if (z.key < y.key) T.left = z; else y.right = z;}void bst_delete(T, z){ if (!z.left) transplant(T, z, z.right); else if (!z.right) transplant(T, z, z.left); else { y = bst_min(z.right); if (y.parent != z){ transplant(T, y, y.right); y.right = z.right; y.right.parent = y; } transplant(T, z, y); y.left = z.left; y.left.parent = y; }}
randomly built BST has expected h=O(lgn)
红黑树
h≤2log2(n+1)
red-black properties
node is red(R) or black(B)
root is black
leaf(NIL) is black
red node has black children
all simple paths from the node to descendant leaves contain the same number of black nodes
rotation
void left_rotate(T, x){ y = x.right; x.right = y.left; if y.left != T.nil y.left.p = x y.p = x.p if x.p == T.nil T.root = y else if x == x.p.left x.p.left = y else x.p.right = y y.left = x x.p = y}
Insertion: insert red + fixup(以下父亲为祖父左结点)
case1: uncle is red
case2: uncle is black and self is left
case3: uncle is black and self is right
Delete: delete + (black) fixup
case1: sibling is red
case2: sibling black and has black children
case3: sibling black and has left red, right black
case4: sibling black and has right red
order-static tree: maintain size
retrieve with a given rank O(lgn)
determine rank O(lgn)
B 树
h≤logt2n+1 with minimum degree t≥2 and n keys
same examining times for keys
lgt less examining times for nodes
properties
k1≤x.k1≤k2≤⋯≤x.kx.n≤kx.n
All leaves have the same depth
Every node other than root has at least t−1 keys(t children). Every node may have at most 2t children. Root has at least 1 key.
split: O(t)
search/insert/delete: O(th)
B+ tree: 索引仅出现在 leaf,容纳更多索引项
Fibonacci 堆
Mergeable heaps (amortized)
Insert Θ(1)
Decrease Θ(1)
Union Θ(1)
Other same to heap
Fibonacci heap: min-heap ordered (k-ary)
Φ(H)=t(H)+2m(H)
t(H): number of trees
m(H): number of marked nodes (lost a child since the last time it was made the child of another node)
consolidate: O(D(n))
find two roots of same degree, link the more one to another, until every root has a distinct degree value
auxiliary array A[0..D(H.n)]
D(H)=⌊logϕn⌋=O(lgn)
node with root degree k has size ≥Fk+2
van Emde Boas Tree (vEB tree)
dynamic set with values from universe Zu
direct addressing
insert, delete, member: O(1)
min/max, pre/succ: Θ(u)
superimposing a tree of constrant height: O(u)
shrink with u gets O(lglgn) (shrink with 2 gets O(lgn))
T(n)=T(n)+O(1) yields T(n)=O(lglgn)
T(n)=2T(n)+O(1) yields T(n)=O(lgnlglgn)
vEB node
u: number of elements
min, max
summary: point to a vEB(u) node keeping bit vector