Red-black tree search tree binary sort of understand binary tree

https://blog.csdn.net/chudelong1/article/details/82698010

Thanks, remember yourself a note

Binary search tree (BST) have what characteristics do?

1. The left node of the root node is less than or equal to.

2. the right node of the root node is greater than or equal to the value.

3. The left and right subtrees are also binary sort tree.

 

Find 10

 

Very easy to find, equivalent to a binary search.

Disadvantages:

If the nodes are sequentially inserted into the following five: 7,6,5,4,3.

 

Although also a binary sort tree, so performance will be very bad.

Here began the red-black tree

1. The node is red or black.

2. The root is black.

3. Each leaf nodes are black empty node (NIL node).

Two children 4 each red node are black. (Red can not have two consecutive nodes on all paths from the root to each leaf)

5. from any node to all leaves of each path contains the same number of black nodes.

Under the tree diagram, it is a typical red-black tree:

As for how to insert data, be sure to pay attention to the data to be inserted after the finish line with all the rules above.

If it does not comply with the rules? So do operations include color change (color adjustment), rotation (left rotation, right rotation)

Good understanding of color, red is black, black reddening.

Rotate it? This is also an important operating from a balanced red-black tree.

Left Rotation:

Two red-black tree node counter-clockwise, so that the parent be replaced by his right children, and their children become their left. That it is very weird, it looks:

The figure, as the right child of the place of X, Y, and X becomes his left children. This rotation is left.

Right rotation:

Two red-black tree nodes rotating clockwise, so that the parent node is replaced left their children, and their children become their own right. We look:

The figure, as a child left the place of X, Y, and X into its own right children. This is the right rotation.

 

Why not consider, is to keep the balance! It is to solve the problem of binary tree sort of lame.

 

Guess you like

Origin www.cnblogs.com/duoba/p/11409791.html