Day2 Balance Tree notes, day2 Balance Tree

Source: Internet
Author: User

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.


Merge implementation

First find the largest key, compare p1, p2

  • If p1 is large

P1 is the root, p2 must be on the Right of p1,

P1.L = p1.L

P1.r = merge (p2, p1.r)

  • If p2



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.