Red-Haishi of Java data structure

Source: Internet
Author: User

This blog I will focus on the red-Haishi understanding, highlighting the red-Haishi lookup, here we will discuss the algorithm called top-down insertion, that is, to look down the tree down the insertion point

Ⅰ, balance trees and non-equilibrium trees

Balance tree and non-balanced tree: When inserting a set of data keywords is inserted in ascending or descending order, this is the most extreme of the unbalanced tree, at this time can also be seen as a linked list when the search for this tree is O (N), so the time complexity of searching for an unbalanced tree is between the time complexity of the balance tree O (logn) The time between and O (N) depends on the degree of imbalance of the tree.

Ⅱ, red-black tree rules

Next we will discuss the red-black tree with the following characteristics (red-black tree rule):

    • 1. Each node is either red or black
    • 2. The root is always black
    • 3. If the node is red, its child nodes must be All black (which in turn is not necessarily true)
    • 4. Each path from the root to the leaf node or the empty node must contain the same number of black nodes

The "Empty node" in fourth refers to the position of the node with the right child node that may be connected to the left Dial hand node, or the node that has the left dial hand node may be connected to the right child node. Is that the non-leaf node may have, but actually does not have the child node (if the node has only the right child node, then the empty left child node is the sub-node, and vice versa) in the Right child node (50→25→25) Black height of 1, from the root to 6 and to 75 of the number of black nodes are 2, This violates rule 4, which has the same number of black nodes as the leaf node, but has a single empty node with a number of black nodes of 1 so this tree is not a red-black tree.

The number of black nodes from the root node to the leaf node path is called the black height , so another way of stating the above rule four is that all the black heights from the root to the leaf node path must be the same.

In order to satisfy the rules of red and black trees, we may use the following two ways to make corrections, so that a tree satisfies the rules of the red and black trees,

    1. Change node color
    2. Perform rotation operations

Why guarantee that the above four red-black tree rules will ensure that the red-Haishi is the balance tree? From the red and black tree rule analysis we can get if a tree over two layers of imbalance how this tree is not able to meet the red-black tree rules, more than two layers of imbalance is that the number of nodes with a path than the number of nodes in the other path more than one, more out of either have more black nodes, if this violates the red-black tree rule Or at least two contiguous red nodes, which violates the Red node's child nodes must all be black nodes.

Ⅲ, the rotation of the tree:

Using the rotation of the tree to rearrange some nodes to balance a tree, there are two things that must be done to rotate:

    1. Make some nodes rise, some nodes fall, help the tree balance
    2. Guaranteed not to break the character of the binary search tree (because the search algorithm relies on this feature, it doesn't make sense)

The color rules of the red and black trees above and the color changes of the nodes only help to decide when to perform the rotation. The light changes the color does not allow the branch tree to achieve the balance, the rotation is the key operation which lets the tree balance, therefore we must understand the tree the rotation operation carefully, should be careful to do the rotation the right vertex must have a left child node, the left-hand top must have the right child node, otherwise rotates the vertex to have no node, Next we look at a simple rotation:

In A→b we can see that the tree has been a right-handed, a figure in node 37 is called the inner descendants of the top node 50 (node 12 is the outer descendants point), for the inner descendants, if he is the child node of the node moved up, it must be disconnected from the parent node and reconnect to its previous grandfather node, It was as if I had become my own uncle. Is the rotation of the single-node position transformation, the above we say the inner descendants can also be a subtrees tree, we show that if it is a sub-tree node of a moving situation, the approximate principle is almost interlinked.

The tree in a right-handed, doing the following several things:

    •  The top node (50) moves to the position of his right child node
    • The Left child node (25) of the top node moves to the top position
    • An entire subtrees tree with node 12 as its root moves up
    • The whole subtrees tree with the node 37 as the root moves laterally, becoming the left child node of node 50
    • The whole subtrees tree with node 75 as its root moves down

Above we see how to rotate a tree to do what kind of rotation, next we discuss how to insert a new node (using rotation and color rules to maintain the balance of the tree)

Ⅳ, insertion of nodes

When we look down at the insertion point, we'll focus on three things.

    1. Color changes along the way
    2. The rotation down the road
    3. Rotation after inserting a node
Color changes on the way down the ①

First of all, we discuss the way the color changes on the road and the normal two-fork search tree is the same, but on the way down will involve color changes, the rule is as follows: whenever you encounter a black node with two red child nodes, it must turn the node into black, the parent node to red (unless the parent node is the root node), If a color transformation is performed

The color change does not change the number of black nodes from the root down through p to the leaf node or the empty node path so the color transform does not violate rule 4, and the benefit of this change is that it is easier to connect a new red node without violating rule 3. Although the above color changes will not violate the rule 4 but it is possible to violate rule 3, if the parent node of the P node is red, then the transformation will violate rule 3, then the tree will be rotated operation,

② the rotation on the downward path

There are two possibilities for rotation on the downward path, when the violated node is the outer descendant node "node violating the rules" refers to the child node in the parent-child node pair that caused the red-red conflict, we start from 50 to create a new tree insert the following node: 25, 75,12,37,6 and 18 require a color transform when inserting 12 and 6. Next we want to insert node 3, we must do a color transformation of node 12 and its child nodes 6 and 18, when the resulting tree is a tree in, when nodes 25 and 12 violate the red and black tree Rule 3, this tree needs to be rotated operations,

Here we have just the color transformation of the triangle vertex is x,x parent node is p, grandfather node is G, at this time we are in the following color change and rotation:

    1. Change the color of the X's grandfather node p (the color of the root must also be changed after rotation in this example)
    2. Change the color of the X parent node P
    3. Rotate the top of X's grandfather node G to the direction of the X ascending

This satisfies the red-Haishi color rules, the tree becomes a balanced tree, it is easy to insert node 3, after inserting as shown in the B figure.

If you insert a descendant node on the inside, at this point we have a root of 50, insert 25, 75,12,37,31 and 43. Before inserting 12 and 31 requires a color transformation, and then a new insertion node 28, looking down at the insertion point, when the 37 node needs to perform a color transformation, after the transformation as shown in the A-tree, after the transformation of 25 and 37 are red need to rotate operation, the color transform triangle vertex x is 37,p 2 5,g is 50, perform the following four steps to make the tree New Balance

    1. Change the color of G
    2. Change the color of X
    3. Rotates in the direction of the top rotation of p to the X rise, as shown in B
    4. Rotate in G for top, x upward to rotate, and then easily insert node 28

③ rotation after inserting a node

Next we discuss the rotation after inserting the node, where we discuss inserting the node as X, whose parent node is P, and the grandfather node is G. 1. If node x is the same as p on the side of P on the side of G, then X is a descendant node (a,d) on the outer side and, conversely, if it is not the same, X is an inner descendant node (b,c). Shows the diversity of "pointing" to the left and right changes

The action taken to restore the red-black rule is determined by the X and its relatives, and the nodes are arranged in only three of the time (the point transformation above does not count)

    1. P is black.
    2. P is red, X is an outer descendant node of G
    3. P is the red X is a descendant node of the inside of G, as shown in

possibility 1: If P is black: do not need to do anything because the inserted node is always red. If the parent node is black, there is no red node connection and the Red node conflict does not increase the number of black nodes, so inserting does not need to do anything at this point.

possibility 2: P is red, X is an outer descendant node of G: This is an insert that requires a rotation and some color changes, at which point we can create a tree with a root of 50 and then insert 25,75 and 12. You need to do a color transform before inserting 12, and then insert 6 into the tree to get a tree in, at this time against the red and black tree rules, need to do a three-step transformation,

    • Change the color of X's grandfather node G.
    • Change the color of the X parent node p.
    • With X's grandfather node G as the top rotation, the direction of x ascending direction, the transformed tree as B, again become a red black tree

possibility 3: P is the red X is a descendant node of the inner side of G, at which time two rotations and some color changes are required, first we create a tree with a node of 50,25,75,12,18 (one color transform before inserting node 12) Insert 12 tree as shown in a, Insert Node 18 is an inner descendant node, and he and his parent nodes are red, first rotation of the descendant nodes into the outer descendant nodes, the b->c then can take advantage of the same situation 1 the same rotation, color conversion and rotation steps are as follows:

    • Change the color of X's grandfather node 25
    • Change the color of x node 18
    • Use X's parent node p as the top rotation, direction x ascending direction
    • Again with the grandfather node of x 25 for the top rotation, the direction for the X rising direction

Next we discuss whether the above three scenarios contain all of the following:

    • First we assume that X has a sibling node s, he is another child node of P, if p is a black insert x no problem (corresponds to Case 1), if p is red then his two child nodes must be black, so at this time there can be no separate black child node s, otherwise the path on the black height is not the same, When P is red, X cannot have a sibling node.
    • Another possibility is that P's parent node G has a child node U,u is the sibling node of P and the tree node of x, and if p is black, inserting x does not need to be rotated. If P is red, then you must also be red, otherwise the black height will be different, this situation when we go down the path to find the insertion point when the color will be changed, the P and U will be converted to black nodes, so at this time there is a transformation back to the case 1.

So the three possibilities discussed above are all possible situations (in probabilities 2 and 3 x may be right or left child nodes, and g may be right or left child nodes)

Deletion of Ⅴ, red-Haishi

Before we discussed the two-fork search tree Writing deleted code is much more difficult than writing the code of the inserted operation, red-Haishi is also the case, after deleting the node to restore the red and black rules become very complex, generally we will use other methods to avoid, for example, only the node to delete the tag and not really delete

Efficiency of Ⅵ, red-Haishi

Similar to the general two-fork search tree, the red-Haishi lookup, the insertion deletion time complexity is O (log2n), the red-Haishi lookup time and the normal two-fork search tree lookup time almost exactly the same, the additional overhead is just storage space to increase the storage of a red-black color space.

The time of insertion and deletion adds a constant factor because you have to perform color transformations and rotations on the downstream path and at the insertion point. The average insertion is about one rotation, so the time complexity of the insertion is still O (log2n) but slower than the normal two-fork search tree (no color transformations and rotations)

Most applications find more times than insertions and deletions, so applying a red-black tree instead of a binary search tree does not add much time overhead, and the biggest advantage of the red-black tree is that the operation of the ordered data does not slow down to the time complexity of O (N).

This is the basic understanding of the red and black tree structure, the shortcomings of the welcome point, do not know where to welcome questions.

Red-Haishi of Java data structure

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: info-contact@alibabacloud.com 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.