Day2 Balance Tree notes, day2 Balance Tree
Operations not supported by the line segment tree: delete, insert
Common Balance Tree
Treap slow | well-written sbt (size-balanced tree) Fast and easy-to-write | incomplete function rbt red/black tree extremely fast |Very hard to writeThe preceding Operations Support inserting and deleting O (NlogN)
Splay is particularly slow .. ≈ O (sqrt (N) is not easy to write and has powerful functions.
Persistent Treap
The Balance Tree must be a binary tree.
The left son must have fewer elements than him.
The right son must be larger than the current node
The middle-order traversal must be sorted
Each recursive query
Small -- "left
Big -- "right
Disadvantages: the depth may be very deep --> the cost is very high
Treap = Tree + heap
Treap: stores two values: [key, val]
Val: The value inserted each time to satisfy the nature of the Balance Tree.
Key: meets the heap nature. rand must be at the logN level in depth.
Merge (p1, p2): merges the Treap with p1 as the root and the Treap with P2 as the root into a Treap. The maximum value of p1 should be <= the minimum value of P2
Split (p, k): split the Treap with p as the root into two treaps, one with k numbers and the other with n-k numbers. k is the first k smaller.
Insert: divide the tree into two parts: x and y. Then, regard the new node a as a tree. Merge the tree with x first. After the merge, merge the entire tree with y.
Delete:
Merge implementation
First find the largest key, compare p1, p2
P1 is the root, p2 must be on the Right of p1,
P1.L = p1.L
P1.r = merge (p2, p1.r)
p2.r=p2.r
p2.L=merge(p2.L,p1)
Merge returns the root node.
Split implementation
Size: number of nodes in the subtree
When k <= p. L. size-> split (p. L, k)->If p1 is set to a useful subtree, merge (p2, p. r) is used directly, and p2 is used as the left child of p.
Returns p. L + p, p. r when k = p. L. size + 1.
When k> p. l. size + 1-> split (p. r, k-p.L.size-1)-> set p2 as a useful subtree, direct merge (p, p1), take p1 as the right child of p