Red-black tree-insert operation

Source: Internet
Author: User
Tags getcolor

Five properties of red and black trees:
1) Each node is either red or black.
2) The root node is black.
3) Each leaf node, or null node (NIL), is black.
4) If a junction is red, then both of its sons are black.
5) For each node, the same number of black nodes are included on all paths from the node to its descendants node.

Several cases of red-black tree insertion:

1, the tree is empty, directly set the node as the root node, the color is black;

public void Case1 (Rbnode t,rbnode newNode) {
if (Newnode.getparent () ==null) {
Newnode.setcolor (0);
T = NewNode;
}else{
Case2 (T,newnode);
}
}

2, the parent node is black, directly inserted under the parent node

Public Rbnode case2 (Rbnode t,rbnode newNode) {
if (Newnode.getparent (). GetColor () ==0) {
return T;
}else{
Return Case3 (T,newnode);
}
}

3, Uncle node is present and the node's uncle node is red

The parent and Uncle nodes are set to black, the grandfather node is set to red, the current node is set to the grandfather node, because the grandfather node is red, 1, may violate the root node is black requirements, 2, because the grandfather node is red, and grandfather's father node may also be red; synthesis 1, 2 starting from Case1, Recursive with the grandfather node for the current node.


Public Rbnode case3 (Rbnode t,rbnode newNode) {
Rbnode y = newnode.getparent (). GetParent (). GetRight ();//Uncle Node
if (Y!=null&&y.getcolor () ==1) {
Newnode.getparent (). SetColor (0);//Current node parent node and Uncle Node blacked out, grandfather node painted red
Y.setcolor (0);
Newnode.getparent (). GetParent (). SetColor (1);
Case1 (T,newnode);
return T;
}else{
Return Case4 (T,newnode.getparent (). GetParent ());
}

}

4, Uncle node is empty or Uncle node is black, Father node is red situation

It can be divided into two different situations here.

(1), the current node is the right subtree of the parent node, the Father node is the Zuozi of the grandfather node

(2), the current node is the left child tree of the parent node, the Father node is the right subtree of the grandparent node

is the first case

//Case 4: Parent node P is red, Uncle node U is black or nil; Insert node n is the right child of its parent node p, and parent node p is the left child of its parent node. (Rotate parent node)
    public rbnode case4 (rbnode t,rbnode newNode) {
         if (NewNode = = Newnode.getparent (). GetRight () &&newnode.getparent () ==newnode.getparent (). GetParent (). GetLeft ()) {
            leftrotate (T,newnode.getparent ()). GetParent ());
            newnode = Newnode.getleft ();
        }else if (NewNode = = Newnode.getparent (). GetLeft () && Newnode.getparent () ==newnode.getparent (). GetParent (). GetRight ()) {
             rightrotate (T,newnode.getparent (). GetParent ());
            newnode = Newnode.getright ();
        }
        return case5 (T,newnode);
    }

Continue through situation 5 for further processing

5, parent node p is red, Uncle node U is black or nil; (rotate grandfather node)

It can be divided into two different situations here.

(1), the current node is the left subtree of the parent node, the Father node is the Zuozi of the grandfather node

(2), the current node is the right subtree of the parent node, the Father node is the right subtree of the grandparent node

Public Rbnode case5 (Rbnode t,rbnode newNode) {
Newnode.getparent (). SetColor (0);
Newnode.getparent (). GetParent (). SetColor (1);
if (NewNode = = Newnode.getparent (). GetLeft () &&newnode.getparent () ==newnode.getparent (). GetParent (). GetLeft ()) {
Rightrotate (T,newnode.getparent (). GetParent ());
}else{
/* Counter-case, N is the right child of its parent node, and parent p is the right child of its parent G */
Leftrotate (T,newnode.getparent (). GetParent ());
}
return T;
}

Delete operation: http://blog.csdn.net/chenhuajie123/article/details/11951777

Red and black trees can keep O (logn) speed to find and delete

Red-black tree-insert operation

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.