The red/black tree is a classic data structure that is used in Linux memory management, nginx, and many other places. The main operations include insert and delete. If there are 6 insertion cases and 8 deletion cases, we will not discuss the detailed ideas. If you do not understand this, refer to chapter 13 of Introduction to algorithms, when you look at it, you must draw out each insertion or deletion on the paper, which will save you a lot of time. The following code is implemented in Java:
Package COM. algorithm. rbtree; public class rbtree {private final node nil = new node (null, color. black,-1); private node root; Public rbtree () {root = nil;} public rbtree (node root) {This. root = root;} // Insert the public void rbinsert (node) {node previous = nil; node temp = root; while (temp! = Nil) {previous = temp; If (temp. getvalue () <node. getvalue () {temp = temp. getright ();} else {temp = temp. getleft () ;}} node. setparent (previous); If (previous = nil) {root = node; root. setparent (NiL);} else if (previous. getvalue ()> node. getvalue () {previous. setleft (node);} else {previous. setright (node);} node. setleft (NiL); node. setright (NiL); node. setcolor (color. red); rb_insert_fixup (node);} // adjust P after the node is inserted Rivate void rb_insert_fixup (node) {While (node. getparent (). getcolor () = color. red) {If (node. getparent () = node. getparent (). getparent (). getleft () {node rightnuncle = node. getparent (). getparent (). getright (); If (rightnuncle. getcolor () = color. red) {// case 1rightnuncle. setcolor (color. black); node. getparent (). setcolor (color. black); node. getparent (). getparent (). setcolor (color. red); node = node. g Etparent (). getparent ();} else if (node = node. getparent (). getright () {// case 2 node = node. getparent (); leftrotate (node);} else {// case 3node. getparent (). setcolor (color. black); node. getparent (). getparent (). setcolor (color. red); rightrotate (node. getparent (). getparent () ;}} else {node leftnuncle = node. getparent (). getparent (). getleft (); If (leftnuncle. getcolor () = color. red) {// case 4leftnuncle. SETC Olor (color. black); node. getparent (). setcolor (color. black); node. getparent (). getparent (). setcolor (color. red); node = node. getparent (). getparent ();} else if (node = node. getparent (). getleft () {// case 5 node = node. getparent (); rightrotate (node);} else {// case 6node. getparent (). setcolor (color. black); node. getparent (). getparent (). setcolor (color. red); leftrotate (node. getparent (). getparent () ;}} root. SETC Olor (color. black);} // Delete the public node rbdelete (INT data) {node = search (data); node temp = nil; node child = nil; if (node = NULL) {return NULL;} else {If (node. getleft () = nil | node. getright () = nil) {temp = node;} else {temp = successor (node);} If (temp. getleft ()! = Nil) {Child = temp. getleft ();} else {Child = temp. getright ();} child. setparent (temp. getparent (); If (temp. getparent () = nil) {root = Child;} else if (temp = temp. getparent (). getleft () {temp. getparent (). setleft (child);} else {temp. getparent (). setright (child);} If (temp! = Node) {node. setvalue (temp. getvalue ();} If (temp. getcolor () = color. black) {rb_delete_fixup (child);} return temp ;}// adjust private void rb_delete_fixup (node) {While (node! = Root & node. getcolor () = color. black) {If (node = node. getparent (). getleft () {node rightbrother = node. getparent (). getright (); If (rightbrother. getcolor () = color. red) {// case 1 node is the left child, And the node brother is redrightbrother. setcolor (color. black); node. getparent (). setcolor (color. red); leftrotate (node. getparent (); rightbrother = node. getparent (). getright ();} If (rightbrother. getleft (). getcolor () = color. Black & rightbrother. getright (). getcolor () = color. black) {rightbrother. setcolor (color. red); node = node. getparent ();} else if (rightbrother. getright (). getcolor () = color. black) {rightbrother. getleft (). setcolor (color. black); rightbrother. setcolor (color. red); rightrotate (rightbrother); rightbrother = node. getparent (). getright ();} else {rightbrother. setcolor (node. getparent (). getcolor (); node. getparen T (). setcolor (color. black); rightbrother. getright (). setcolor (color. black); leftrotate (node. getparent (); node = root;} else {node leftbrother = node. getparent (). getleft (); If (leftbrother. getcolor () = color. red) {leftbrother. setcolor (color. black); node. getparent (). setcolor (color. red); rightrotate (node. getparent (); leftbrother = node. getparent (). getleft ();} If (leftbrother. getleft (). getcolor () = color. Black & leftbrother. getright (). getcolor () = color. black) {leftbrother. setcolor (color. red); node = node. getparent ();} else if (leftbrother. getleft (). getcolor () = color. black) {leftbrother. setcolor (color. red); leftbrother. getright (). setcolor (color. black); leftrotate (leftbrother); leftbrother = node. getparent (). getleft ();} else {leftbrother. setcolor (node. getparent (). getcolor (); node. getparent (). setcol Or (color. black); leftbrother. getleft (). setcolor (color. black); rightrotate (node. getparent (); node = root ;}} node. setcolor (color. black);} // search for the public node successor (node) {node rightchild = node. getright (); If (rightchild! = Nil) {node previous = NULL; while (rightchild! = Nil) {previous = rightchild; rightchild = rightchild. getleft ();} return previous;} else {node parent = node. getparent (); While (parent! = Nil & node! = Parent. getleft () {node = parent; parent = parent. getparent () ;}return parent ;}// find the public node search (INT data) {node temp = root; while (temp! = Nil) {If (temp. getvalue () = data) {return temp;} else if (Data <temp. getvalue () {temp = temp. getleft ();} else {temp = temp. getright () ;}return NULL;} // left turn function private void leftrotate (node) {node rightnode = node. getright (); node. setright (rightnode. getleft (); If (rightnode. getleft ()! = Nil) {rightnode. getleft (). setparent (node);} rightnode. setparent (node. getparent (); If (node. getparent () = nil) {rightnode = root;} else if (node = node. getparent (). getleft () {node. getparent (). setleft (rightnode);} else {node. getparent (). setright (rightnode);} rightnode. setleft (node); node. setparent (rightnode);} // The right turn function private void rightrotate (node) {node leftnode = node. getleft (); node. setleft (LEF Tnode. getright (); If (leftnode. getright ()! = NULL) {leftnode. getright (). setparent (node);} leftnode. setparent (node. getparent (); If (node. getparent () = nil) {root = leftnode;} else if (node = node. getparent (). getleft () {node. getparent (). setleft (leftnode);} else {node. getparent (). setright (leftnode);} leftnode. setright (node); node. setparent (leftnode);} // traverse the red/black tree in the middle order public void printtree () {inordertraverse (Root);} private void inordertraverse (node nod E) {If (node! = Nil) {inordertraverse (node. getleft (); system. out. println ("node:" + node. the color of getvalue () + "is:" + node. getcolor (); inordertraverse (node. getright () ;}} public node getnil () {return nil ;}} class node {private node left; private node right; private node parent; private color; private int value; Public node (node left, node right, node parent, color, int value) {super (); this. left = left; this. right = right; this. parent = parent; this. color = color; this. value = value;} public node () {} public node (INT value) {This (null, value);} public node getleft () {return left;} public void setleft (node left) {This. left = left;} public node getright () {return right;} public void setright (node right) {This. right = right;} public node getparent () {return parent;} public void setparent (node parent) {This. parent = parent;} public color getcolor () {return color;} public void setcolor (color) {This. color = color;} public int getvalue () {return value;} public void setvalue (INT value) {This. value = value ;}} Enum color {red, black}
The following is the test code:
package com.algorithm.rbtree;public class RBTreeTest {/** * @param args */public static void main(String[] args) {RBTree rbTree = new RBTree();rbTree.rbInsert(new Node(41));rbTree.rbInsert(new Node(38));rbTree.rbInsert(new Node(31));rbTree.rbInsert(new Node(12));rbTree.rbInsert(new Node(19));rbTree.rbInsert(new Node(8));//rbTree.printTree();rbTree.rbDelete(19);rbTree.printTree();}}