RBT Red black Tree-java version

Source: Internet
Author: User

This is a huge amount of code, imitating someone else to write a whole day ...

Java because there is no reference delivery, so the build tree is either the global setting root and then change, or the function returns the root ....

Reference: teach you a thorough understanding of red and black tree data Structures-red and black trees

The red and black trees ensure that no path is about twice times longer than other paths, so it is nearly balanced.

1. Red-black Tree Nature (restriction):

1) Each node is either red or black.
2) The root node is black.
3) Each leaf node (leaf node refers to a nil pointer or null node at the end of the tree) is black.
4) If a junction is red, then its two sons are black.
5) for any node, each path to the nil pointer at the end of the leaf node tree contains the same number of black nodes.

2. Performance analysis of red and black trees:

Dig a hole, write it tomorrow.


3. Preliminary knowledge:

Left (right) refers to the right (left) sub-tree of the point occupying its own position, the original point becomes its (right) sub-tree.


4. Core operation:

1) Insert

Like AVL, there's a process of adjusting the color.

/  * * If the parent node is black, it is not violated without adjustment. * instead .... * When inserting a node. The nature of the possible destruction is (4) If a node is red, then its child node is black   (2) The root node is black  * Insert Repair Case 1: If the parent node of the current node is red and the other child node of the grandparent node (Uncle node) is red         * Insert Fix Case 2: The parent node of the current node is red, the Uncle node is black, the current node is the right child of its parent node  * Insert Fix 3: The parent node of the current node is red, Uncle node is black, the current node is the left child of its parent node */


2) Delete

More complex, four kinds of situations ...

/* * If the deletion is a red dot, does not affect ... * If the deletion is a black point, in place of the red/black two cases, * 1. Red, change this point directly to black * 2.1 black and is the root node nothing to do * 2.2 Delete the repair condition 1, the current node color is black, the sibling node is red (at this time the parent node and the sibling node is divided into black) * 2.2 Delete the repair condition 2, the current node color is black, Brother is black and brother node two child nodes are all Black * 2.2 Delete repair case 3, the current node color is black, the brother node is black, the brother's left dial hand is red, the right child is black * 2.2 Delete the repair condition 4, the current node color is black, the sibling node is black, but the brother node is red, * *

Code:

Import java.util.arrays;import java.util.collection;import java.util.scanner;interface ANode{public void Rbinordertravel (node node);p ublic node Rbsearch (node node);p ublic node Rbminnode (node node);p ublic node Rbmaxnode (node Node);p ublic Boolean Rbinsert (node node,int data);p ublic Boolean rbdelete (node node,int data);p ublic void LL (node node);p ublic void RR (node node);} public class Main {public static void main (string[] args) {int [] a = new Int[20];for (int i = 0;i<20;i++) {A[i] = (int) (Math.random () *1000); System.out.print (A[i] + ""); Node.rbinsert (Node.root, A[i]);} System.out.println (); Node.rbinordertravel (Node.root); Node.rbdelete (Node.root, a[1]); Node.rbdelete (Node.root, a[9]); Node.rbdelete (Node.root, a[0]); Node.rbinordertravel (Node.root);}} Class Node{public static Node root = null;public static int RED = 1;public static int BLACK = 2; Node Lson,parent,rson;int high;int data;int color;public node () {super (); Lson = Rson = Null;high = 0;this.color = RED;} public Node (int data) {//TODO Auto-generated constructor Stubthis (); this.data = data;} public void Free () {This.lson = This.rson = Null;this.parent = null;} public static node Rbsearch (node Node,int data) {while (node! = null) {if (Data < node.data) node = Node.lson;else if (Data & Gt Node.data) node = node.rson;else return node;} return null;} public static node Rbsuccessor (node node) {Node pre = null;while (node! = null) {pre = Node;node = Node.lson;} return pre;} public static void Rbinordertravel (node node) {//TODO auto-generated method stubif (Null = = Node) return;rbinordertravel (n Ode.lson); System.out.print (node.data+ ""); Rbinordertravel (Node.rson); if (node = = root) System.out.println ();} public static Boolean Rbinsert (node node, int data) {//TODO auto-generated method Stubnode now = new Node (data); Node pre = Null;while (null! = node) {pre = Node;if (Data < node.data) node = node.lson;else node = Node.rson;} if (pre = = null) root = now;else{if (Data < Pre.data) Pre.lson = Now;else Pre.rson = Now;} Now.parent = Pre;rbtreeinsertfixUp (now); return true;} /* * If the parent node is black, it is not violated without adjustment. * instead .... * When inserting a node. The nature of the possible destruction is (4) If a node is red, then its child knot is black (2) the root node is black * Insert repair Case 1: If the parent node of the current node is red and the other child node of the grandparent node (Uncle node) is red * Insert fix 2: The parent of the current node is red, tert The tertiary node is black, the current node is the right child of its parent node * Insert Fix 3: The parent node of the current node is red, the Uncle node is black, the current node is the left child of its parent node */private static void Rbtreeinsertfixup (node node) {// TODO auto-generated Method Stubnode Uncle,gparent,p;while ((p=node.parent)! = null && p.color ==red) {gparent = P.P arent;//If the parent node is the left child of the grandfather node (because the parent node is the red node, so there must be a grandfather node) if (p = = Gparent.lson) {uncle = gparent.rson;if (Uncle! = null && Uncle.color = = RED) {//fix case 1gparent.color = Red;p.color = Black;uncle.color = Black;node = gparent;} else{//uncle does not exist or exists but the color is black, it must be changed by the search to match the color to maintain the nature of 2if (node = = P.rson) {//repair 2node = p; LL (node);p = Node.parent;} The situation 2:x for the right child of its parent node, by converting the left hand to case 3//Case three: X for the left child of its parent node, adjusting the parent node and the grandfather node color to correct the nature of 4, but destroying the property 5 P.color = Black;gparent.color = RED; RR (gparent);//At this time X->parent->color = BLACK, loop over}}else{uncle = gparent.lson;if (Uncle! = null && Uncle.color = = RED) {gparent.color = Red;p.color = Black;uncle.color = Black;node = gparent;} else{//uncle does not exist or exists but the color is black, it must be searched to match the change of color to maintain the nature of 2if (node = = P.lson) {node = p; RR (node);p = Node.parent;} The situation 2:x for the right child of its parent node, by converting the left hand to case 3//Case three: X for the left child of its parent node, adjusting the parent node and the grandfather node color to correct the nature of 4, but destroying the property 5 P.color = Black;gparent.color = RED; LL (gparent);//At this time X->parent->color = black, loop end}}}root.color = black;//Hold property 2, root is black}/* * If the deletion is red dot, no effect ... If the deletion is a black point, instead of the position of the red/Black case, * 1. Red, change this point directly to black * 2.1 black and is the root node nothing to do * 2.2 Delete the repair condition 1, the current node color is black, the sibling node is red (at this time the parent node and the sibling node is divided into black) * 2.2 Delete the repair condition 2, the current node color is black, Brother is black and brother node of two sub-nodes are all Black * 2.2 Delete repair case 3, the current node color is black, the brother node is black, the brother's left dial hand is red, the right son is black * 2.2 Delete repair condition 4, the current node color is black, the sibling node is black, but the brother node is red, * * Another explanation: * Deleting a black node causes the following three problems: * (1) If the deletion node y is the root node, and a red child of Y becomes the new root, then violates the Nature 2 * (2) How Y's parent node and its child nodes are red, then violates the Nature 4 * (3) Deleting y will result in one less black node tree on any path that previously contained Y, destroying the property 5. * The solution is: the deleted node black attribute is moved down to its child node x. At this time the nature of 5 is maintained, there are 2 situations: * (1) x is red, at this point the child node attribute is red and black, at this time the nature of Destruction (1), (4), if x or the root, the destruction of the Nature (2) * processing is: The x is re-colored to black (this operation also removes its extra black properties), After processing, the nature of the red and black tree is maintained * (2) x is originally black, at this time the properties of X is double black, destroying the nature (1), if x is the root, you can simply eliminate X extra Black properties *       Otherwise you need to do the necessary rotation and color modification */public static Boolean Rbdelete (node node, int data) {//TODO auto-generated method Stubnode Now = Rbsearch (Node,data); Node pre = now; Node son = null;if (now = null) return false;if (Now.lson! = NULL && Now.rson! = null) {now = Rbsuccessor (Now.rson);p Re.data = Now.data;} else if (Now.lson! = null) son = Now.lson; else if (Now.rson! = null) son = Now.rson;if (son! = null) Son.parent = now.parent;if (now.parent = = null) root = Son;else{if (no W.parent.lson = = Now) Now.parent.lson = Son;else Now.parent.rson = Son;} if (Now.color = = BLACK) rbtreedeletefixup (Root,now.parent,son); Now.free (); return true;} private static void Rbtreedeletefixup (node Root,node Parent, node node) {//TODO auto-generated method Stubnode brother = Null;while ((node==null | | node.color==black) && node! = root) {if (node = = Parent.lson) {brother = parent.rson;//sentiment Case 1: If the sibling node is red, then the parent color ratio is black, at this time adjust the color, and left, so that the brother and//parent position Exchange, this operation does not destroy other properties, and the situation 1 changes to the situation 2,3,4 if (Brother.color = = RED) { Parent.color = RED; brother.color = BLACK; LL (parent); brother = Parent.rson;} Situation 2, there is no add brother==black because after the situation 1 will satisfy//brother has two black nodes (null is also black node): The X and brother erase a heavy black//specific operation for, brother's color into red, The x node is moved up to its parent node if (Brother.lson = = NULL | | brother.lson.color = = BLACK) && (Brother.rson = = NULL | | brother.rson.color = = BLACK) {Brother.color = Red;node = Parent;parent = parent.parent;} else{//already know from above if two children are not all Black//Case 3:brother left child for Red node, right child for black node if (Brother.rson = = NULL | | brother.rson.color = = Black) { Brother.lson.color = Black;brother.color = RED; RR (brother);//Right-turn causes condition 3 to change to condition 4 brother = parent.rson;//because rotation, resetting}//condition 4:brother right child for Red node://Swap the color of brother and parent and Position, so that one of the 2 black properties of X is transferred to its parent//at this time to the brother's right child's black knot point less one, so the right node color black, red and black tree nature to maintain Brother.color = Parent.color;p Arent.color = Black;brother.rson.color = BLACK; LL (parent); node = root;}} Else{brother = parent.lson;//Case 1: If the sibling node is red, the parent color is darker than black, and the color is adjusted and left-handed so that the brother and the//parent position are swapped, this operation does not destroy other properties, and change the situation 1 to the case 2,3,4 if (Brother.color = = red) {Parent.color = red; brother.color = BLACK; RR (parent); brother = Parent.lson;} Situation 2, there is no add brother==black because after the situation 1 will satisfy//brother has two black nodes (null is also black node): The X and brother erase a heavy black//specific operation for, brother's color into red, The x node is moved up to its parent node if (Brother.lson = = NULL | | brother.lson.color = = BLACK) && (Brother.rson = = NULL | | brother.rson.color = = BLACK) {Brother.color = Red;node = Parent;parent = parent.parent;} else{//already know from above if two children are not all Black//Case 3:brother right child for red node, left child for black node if (Brother.lson = = NULL | | brother.lson.color = = Black) { Brother.rson.color = Black;brother.color = RED; LL (brother);//right-turn make case 3 change to condition 4 brother = parent.rson;//because rotation, reset}//condition 4:brother right child for Red node://Swap the color of brother and parent and Position, so that one of the 2 black properties of X is transferred to its parent//at this time to the brother's right child's black knot point less one, so the right node color black, red and black tree nature to maintain Brother.color = Parent.color;p Arent.color = Black;brother.lson.color = BLACK; RR (parent); node = root;}}} if (node! = null) Node.color = BLACK;} /** * Red and Black tree's turn left is different from AVL, LL is root.left.right up * @param node */public static void LL (node node) {//TODO auto-generated method StubnOde son = Node.rson;node.rson = Son.lson;if (Son.lson! = null) Son.lson.parent = Node;son.parent = Node.parent;//node is the root if ( Node.parent = = null) root = son;else{if (Node.parent.lson = = node) Node.parent.lson = Son;else Node.parent.rson = Son;} Son.lson = Node;node.parent = Son;} public static void RR (node node) {//TODO auto-generated method Stubnode son = Node.lson;node.lson = Son.rson;if (Son.rson ! = null) Son.rson.parent = Node;son.parent = Node.parent;//node is the root if (node.parent = = null) root = Son;else{if ( Node.parent.lson = = node) Node.parent.lson = Son;else Node.parent.rson = Son;} To modify the Parent,parent's left and right, the pointer itself Son.rson = Node;node.parent = Son;}}



RBT Red black Tree-java version

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.