Data structure - red-black trees

Red-black tree

A red-black tree is essentially balanced binary search tree, binary balanced search tree, there are many, including AVL trees, Splay Tree (splay trees), Treap (Treap), etc.

Red-black tree is a balanced binary search tree, is the most commonly used.

 

Binary search tree in frequent dynamic update process, the situation may appear much larger than the height of the tree log2n, resulting in decreased efficiency of each operation.

In extreme cases, the binary tree will degenerate into a linked list, the time complexity degenerate to O (n).

To solve the problem after dynamically updated frequently complexity degradation, we need a balanced binary search tree.

 

Balanced binary search tree

Strict definition of a balanced binary tree is: height of left and right subtrees of any node in the binary tree differ by no greater than 1

First, the full binary tree complete binary tree and are certainly balanced binary tree, but it may be a non-complete binary tree balanced binary tree.

 

Balanced binary search tree is first to be an AVL tree invention.

It is strictly in line with the definition of balanced binary search tree I have just mentioned, namely left and right subtrees of any node differ by no more than 1 highly, is a highly balanced binary search tree. 

 

But in fact many balanced binary search tree is actually not strictly conform to the strict definitions above.

To say red-black tree, it is the longest path from the root to each leaf node, there may be twice as large than the shortest path.

 

"balance"

Balanced binary search tree "balance" means, in fact, is to make the entire tree look around more "symmetrical" more "balanced."

Do not appear left sub-high tree, right subtree very short situation.

High efficiency so that we can make the whole tree height is relatively low, the corresponding insert, delete, search and other operations some.

 

So a balanced binary tree, as long as the height of the tree is much larger than log2n (such as the height of the tree is still an order of magnitude).

Although it does not meet the strict definition of the balanced binary search tree, but you can still say that this is a qualified balanced binary search tree.

 

Requires red-black tree

English red-black tree is a "Red-Black Tree", referred to as RB Tree. It is a not a strict balanced binary search tree.

The reason is called red-black tree, because the red-black tree node, a class is marked in black, one is marked in red.

In addition, there are a lot of requirements:

First, the root is black;

Second, each leaf node is a black node empty (NIL), that is, leaf nodes do not store data;

Third, any adjacent nodes can not be both red, that is to say, the red nodes are separated by a black node;

Fourth, each node, all paths which reach the leaf node reachable from that node, contain the same number of black nodes;

 

"Approximate balance"

Red-black tree is an approximately balanced binary search tree.

"Balance" can mean not equivalent to performance degradation. "Approximate balance" is equivalent to the performance degradation is not too serious.

Many binary search tree height is proportional to the performance of operations related to the tree. A measure of balance or not the concept can convert it.

An extremely balanced binary tree (or full binary tree complete binary tree) height is approximately log2n.

So if you want to prove that red-black tree is approximately balanced, we only need to analyze whether the height of the red-black tree is relatively stable approach log2n enough.

 

The basic idea is to achieve red-black tree

Cube recovery solution is a fixed algorithm: Which face encounter what it was like, how it corresponds to turn a few.

You just follow the recovery step, we will certainly be able to recover the Rubik's cube.

Balancing process of red-black tree with cube recover quite similar, the general process is this: encounter any kind of node arrangement, we will correspond to how to adjust.

Just follow a fixed procedure, the retention insert, delete process, the definition does not destroy the balance of the tree on the line.

 

L (rotate left), right-handed (rotate right)

L stands for is actually called a node around the left-hand, right hand around the right hand is called a node.

Left and right hand operation are substantially red-black tree balance adjustment.

In fact, left and right hand can be imagined as a fixed pulley.

L-around node X:
pull to the left of a sub-tree node X, to pull down, pull the node Y position node X, a sub-tree is still left sub-tree node X and node X becomes the left node Y child node, then it will be left subtree b original node Y off, the tree becomes the right child node X;

Similarly, the right-handed around the node X:
pull r sub-tree node X on the right, pull down the node Y pulled location node X, a sub-tree is still left sub-tree node Y, the node X has become the right child node Y, then it will be right subtree b original node Y off to become a node X left subtree;

 

At this time, the node of interest is X, during the red-black tree balance adjustment, be sure to identify the node of interest, do not get lost, wrong node of interest. 

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104652556