Java Practice Red-black tree-small trial sledgehammer

Source: Internet
Author: User
Tags comparable


The first feeling is that the difference between the AVL tree is not particularly large, not very intuitive to feel the huge increase in efficiency, as a small obsessive-compulsive patients, or AVL tree more beautiful.

But the truth is, the balance is destroyed in the same situation, the red and black trees are really less some of the rotation ...

Because the balance is broken when the node is inserted, the red and black tree will choose to rotate or discolor.
The AVL tree is rotated only.

In addition I found other great God writing, with my little gap ... A caring mind can help me to glance, I was not where the wrong, first thanked ~ ~

Another reference website, blog, PDF:
Red and Black tree visualization, insert, delete nodes have animated effect Oh ~
Magical handouts, slightly more, but in a nutshell
A great God's blog, clear-minded, detailed, very worthy of reference

Implementation features

There's a pseudo-deletion ... You can ignore it and add a Boolean variable.

Why don't you call it a little sledgehammer (escape


First glance at the nature of a jealous black tree

Let's see what it looks like.

About the things that were inserted.

Summarize again

Code implementation
Public class rbtree<t extends comparable<t>> {public static void Main (string[] args) {///*int[] Testnum =Newint[]{ the,1,3,6,8, -, A, +, the}; rbtree<integer> fuck =NewRbtree<integer> (); for(int i =0; i < testnum.length;        i++) {Fuck.insertnode (testnum[i]); } System.out.println (;//*/        /* rbtree<character> fuck = new rbtree<character> ();        Fuck.insertnode (' F ');        Fuck.insertnode (' G ');        Fuck.insertnode (' D ');        Fuck.insertnode (' B ');        Fuck.insertnode (' C ');        System.out.println (; */}PrivateStaticFinalBoolean RED =true;PrivateStaticFinalBoolean BLACK =false;PrivateTreenode<t> Root;/** * Insert a new node that matches the red-black tree character after insertion * * @param data needs to be inserted */public void Insertnode (T data) {treenode<t> curr; treenode<t> parent = totargetparent (data);if(Parent = =NULL) {Curr = root =Newtreenode<t> (data); }Else{if(Data.compareto ( <0) {Curr = Parent.left =Newtreenode<t> (data);            Curr.parent = parent; }Else{Curr = Parent.right =Newtreenode<t> (data);            Curr.parent = parent;    }} fixuptree (Curr); }/** * Fix red black tree imbalance * * @param node New */    Privatevoid Fixuptree (TreeNode node) {TreeNode parent =NULL, grandparent =NULL; Boolean parentcolor =false, Unclecolor =false;if(Node! = root)            {parent = node.parent;            grandparent = parent.parent;            Parentcolor = Parent.color;        Unclecolor = getunclecolor (node); }The //parent node is empty to indicate that it is the root node        if(Parent = =NULL&& Node.color = = RED) {node.color = BLACK; }Else if(Parentcolor = = Red && Unclecolor = = red) {ChangeColor (grandparent);//Determine if the root node meets the requirements againFixuptree (grandparent); }Else if(Parentcolor = = RED && Unclecolor = = BLACK)        {Dispatchrotation (grandparent, parent, node); }    }/** * Determine which of the four cases, LL, LR, RR, RL, use the correct rotation * * @param grandparent grandfather node * @param Parent Parent node * @param Child new node */    Privatevoid Dispatchrotation (TreeNode grandparent, TreeNode parent, TreeNode child) {if(Grandparent.left = = parent) {if(Parent.left = = Child)            {rightrotation (grandparent); }Else{leftrotation (parent);            Rightrotation (grandparent); }        }Else{if(Parent.left = = Child)                {rightrotation (parent);            Leftrotation (grandparent); }Else{leftrotation (grandparent); }        }    }/** * Change the color of the grandfather node and two child nodes * * @param grandparent incoming new node grandfather */    Privatevoid ChangeColor (TreeNode grandparent) {grandparent.color = RED;if(Grandparent.left! =NULL) {grandParent.left.color = BLACK; }if(Grandparent.right! =NULL) {grandParent.right.color = BLACK; }    }/** * Returns the color of your uncle's node * * @param node of nodes * @return The color of their uncle's node * *    PrivateBoolean Getunclecolor (TreeNode node) {TreeNode parent = node.parent;//If the grandfather of the current node is empty, the description is that its parent node is the root node        returnGetbrothercolor (Parent.parent = =NULL?    Node:parent); }/** * Returns the color of the sibling node * * @param Child incoming node * @return Returns the color of the sibling node */    PrivateBoolean Getbrothercolor (TreeNode child) {TreeNode parent = child.parent;if(Parent.left = = Child && parent.right! =NULL) {returnParent.right.color; }Else if(Parent.right = = Child && Parent.left! =NULL) {returnParent.left.color; }Else{returnBLACK; }    }/** * Promote the right child node of the incoming node to a new parent node, the incoming node drops to its right child node * Note the color, the parent node needs to be processed, be sure to clear the right child node of the incoming node, because it has been promoted the parent node. * @param Curr of a node */    Privatevoid Leftrotation (TreeNode curr) {TreeNode tparent = curr.right;        Tparent.parent = curr.parent; Tparent.color = BLACK;//The left child node of the new parent node, placed on the right side of the incoming nodeCurr.right = Tparent.left;if(Tparent.left! =NULL) {tParent.left.parent = Curr; }data collation before//down to child nodesCurr.color = RED;        Curr.parent = tparent;        Tparent.left = Curr;    SetChild (Curr, tparent); }/** * Promote the left child node of the incoming node to a new parent node, the incoming node to its right child node * Note the color, the parent node needs to be processed, it is important to clear the left child node of the incoming node, because it has been promoted the parent node. * @param Curr of a node */    Privatevoid Rightrotation (TreeNode curr) {//New parent nodeTreeNode tparent = Curr.left;        Tparent.parent = curr.parent; Tparent.color = BLACK;//The right child node of the new parent node, placed on the left side of the incoming nodeCurr.left = Tparent.right;if(Tparent.right! =NULL) {tParent.right.parent = Curr; }data collation before the incoming node is reduced to a child nodeCurr.color = RED;        Curr.parent = tparent;        Tparent.right = Curr;    SetChild (Curr, tparent); }/** * Causes rotation to take effect in the tree * * @param ronode the node being rotated * @param The parent node after the newparent is rotated */    Privatevoid SetChild (TreeNode ronode, TreeNode newparent) {TreeNode ronodeparent = newparent.parent;if(Ronodeparent = =NULL) {root = Newparent; }Else if(Ronodeparent.left = = Ronode)        {ronodeparent.left = newparent; }Else{ronodeparent.right = newparent; }    }/** * To the parent node of the data storage location * @param data used for comparison * @return The parent node of data storage * /    PrivateTreenode<t> totargetparent (T data) {treenode<t> Curr = root; treenode<t> parent = root; while(Curr! =NULL) {parent = Curr;if(Data.compareto ( <0) {Curr = Curr.left; }Else{Curr = Curr.right; }        }returnParent }/** * Internal node * /Static class TreeNode<t extends comparable<t>> { T data; Boolean color;//pseudo-deleteBoolean isDeleted;        Treenode<t> left;        Treenode<t> right;        Treenode<t> parent; TreeNode (T data, Boolean color) { This. data = data; This. color = color; } TreeNode (T data) { This. data = data; This. color = RED; }    }}

Proven, long like this!


Java Practice Red-black tree-small trial sledgehammer

Related Article

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: 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.