Understanding red and black trees with Java.util.TreeMap source

Source: Internet
Author: User
Tags comparable

Objective

This article will combine JDK1.6 's TreeMap source code, to explore the mystery of red-Haishi together. The red-black tree is an unbalanced problem that solves the two-fork search tree.

When inserting (or deleting) a new node, in order to keep the tree balanced, certain rules must be followed, which is the red-black 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 black (or not necessarily true)
4) The same number of black nodes must be included from each path that is followed by a leaf or a null node

Insert a new node

The red-Haishi insertion process is basically the same as the normal two-fork search tree: From the point of intersection, the relative size of the keywords at each node determines whether to go left or right.

PublicVPut(KKey,VValue){Entry<K,V>T=Root;IntCmp;Entry<K,V>Parent;Comparable<?SuperK>K=(Comparable<?SuperK>)Key;Do{Parent=T;Cmp=K.CompareTo(T.Key);If(Cmp<0){T=T.Left;}ElseIf(Cmp>0){T=T.Right;}Else{Note that the return exit methodReturnT.SetValue(Value);}}While(T!=Null);Entry<K,V>E=NewEntry<K,V> (Key,Value,parentif  (cmp < 0) Span class= "o" >{parent. Left = e} else {parent. Right = e} fixafterinsertion (esize++; modcount++; return null;        

However, in red-black tree species, finding the insertion point is more complex because there are color transformations and rotations. The fixAfterInsertion() method is to handle color transformations and rotations, and you need to focus on how it maintains the balance of the tree (use rotations and the color rules to maintain the tree's balance).

In the following discussion, use X, P, and g to represent the associated node. x represents a special node, p is the parent of X, and G is the parent of P.

X is a node and has caused a rule violation. (sometimes X refers to a newly inserted node, and sometimes to the child node when a parent and child has a redred Confli Ct.)

On the the-the-the-the-tree to find-the insertion point, you perform a color-flip whenever you find a black node with the red Children (a violation of Rule 2). Sometimes the flip causes a red-red conflict (a violation of Rule 3). Call the red Child X and the red parent P. The conflict can be fixed with a single rotation or a double rotation, depending on whether X are an outside or inside gran Dchild of G. Following color flips and rotations, you continue down to the insertion point and insert the new node.

After you ' ve inserted the new node X, if P was black, you simply attach the new red node. If P is red, the there is and possibilities:x can be a outside or inside grandchild of G. If X is a outside grandchild, you perform one rotation, and if it's an inside grandchild, you perform. This restores the tree to a balanced state.

According to the above explanation, the discussion can be divided into 3 parts, arranged according to the complexity, respectively:
1) Color transform on the downstream path (color flips on the way down)
2) rotation after inserting the node (rotations after that node is inserted)
3) on the downward path of the rotation (rotations on the road down)

Color transform in the down path (color flips on the way down)

Here's the rule:every time the insertion routine encounters a black node that has both red children, it must change the CH Ildren to Black and the parent to red (unless the parent was the root, which always remains black)

The flip leaves unchanged the number of black nodes on the path from the root in the through P to the leaf or null nodes.

Although the color transform does not violate rule 4, it may violate rule 3. If the parent of P is black, there is no problem when p turns from black to red, but if the parent of P is red, then two red nodes are connected after the color of P changes. This problem needs to be resolved before continuing to insert a new node down the path, which can be corrected by rotation and will be seen later.

Rotation after inserting nodes (rotations after the node is inserted)

Before inserting the new node, the tree conforms to the red-black rule, and after inserting the new node, the tree is unbalanced, and the tree's balance needs to be rotated to re-match the red-black rule.

The possibility of 1:p is black, and there is nothing to do about it. Insert.

The likelihood 2:p is red, X is an outer descendant node of G, then one rotation and some color changes are required.
To insert 50,25,75,12,6 as an example, note that node 6 is an outer descendant node, and its parent node is red.

In this example, X is an outer descendant node and is a left dial hand node, and X is the outer descendant node and is the right child node, which is a symmetric case. By creating a tree with 50,25,75,87,93, and then drawing a drawing again, this is omitted.

The likelihood 3:p is red, X is an inner descendant node of G, it takes two rotations and some color changes.
To insert 50,25,75,12,18 as an example, note that node 18 is an inner descendant node, and its parent node is red.

The rotation on the downward path (rotations on the way down)

Before inserting a new node, the tree has actually violated the red-black rule, so it needs to be adjusted before inserting a new node. So the topic we're discussing is "when you're ready to insert a new node down the road, adjust it to make it a standard red-black tree, then insert the new node."

Outer descendant nodes

To insert 50,25,75,12,37,6,18,3 as an example, the node that violates the rule in the example is an outer descendant node.

medial descendant nodes

To insert 50,25,75,12,37,31,43, for example, the node that violates the rule is an inner descendant node.

The efficiency of red-Haishi

Similar to the typical two-fork search tree, red-Haishi has an O (log2n) time complexity for finding, inserting, and deleting.

The search time for red-Haishi should be almost identical to that of a normal two-fork searching tree. Because the red-black feature is not used during the lookup process. The extra overhead is just a little bit more storage space per node to store the red-black color (a Boolean variable).

FinalEntry<K,V>Getentry(ObjectKey){Comparable<?SuperK>K=(Comparable<?SuperK>)Key;Entry<K,V>P=Root;While(P!=Null){IntCmp=K.CompareTo(P.Key);If(cmp < 0) {p Span class= "o" >= p. Left} else if  (cmp  > 0) {p = p< Span class= "O". right} else {return p} } return null;    

The time to insert and delete adds a constant factor because you have to perform color transformations and rotations on the downstream path and at the insertion point. On average one insertion takes approximately one rotation.

Because in most applications, the number of lookups is more than the number of insertions and deletions, applying a red-black tree instead of a normal two-fork search tree generally does not add much time overhead.

Resources
    1. How Eclipse debugs debug JDK source code
    2. A brief discussion on the algorithm and data structure: Nine balance search tree of red and black trees

Understanding red and black trees with Java.util.TreeMap source

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.