Recently, I am reading the red/black tree and want to write a program to implement it myself. Because the deletion of the red/black tree has not yet been viewed, the program does not include the delete operation. After reading the tree, I will try again, haha...
I will not talk about the principles of the Red/black tree. I am a program written by referring to the pseudo code in <Introduction to algorithms>.
The code is implemented as follows:
package com.datastructure.tree;public class MyRbTree {RbTreeNode root=null;RbTreeNode nilNode=null;public RbTreeNode getRoot() {return root;}public MyRbTree(){this.nilNode=new RbTreeNode(-1);this.nilNode.color=TreeNodeColor.Black;this.nilNode.left=null;this.nilNode.right=null;this.nilNode.parent=null;this.root=this.nilNode;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMyRbTree mytree=new MyRbTree();int[] array={2,5,3,4,1,0};for(int i=0;i<array.length;i++){mytree.insert(array[i]);mytree.preorder(mytree.getRoot());System.out.println();}}private void leftRotate(MyRbTree tree,RbTreeNode x){RbTreeNode y=x.right;x.right=y.left;y.left.parent=x;y.parent=x.parent;if(x.parent==this.nilNode){tree.root=y;}else{if(x==x.parent.left)x.parent.left=y;elsex.parent.right=y;}y.left=x;x.parent=y;}private void rightRotate(MyRbTree tree,RbTreeNode y){RbTreeNode x=y.left;y.left=x.right;x.right.parent=y;x.parent=y.parent;if(y.parent==this.nilNode){tree.root=x;}else{if(y==y.parent.left)y.parent.left=x;elsey.parent.right=x;}x.right=y;y.parent=x;}private void insert(MyRbTree tree,int value){RbTreeNode y=this.nilNode;RbTreeNode x=tree.root;RbTreeNode z=new RbTreeNode(value);while(x!=this.nilNode){y=x;if(value<x.key)x=x.left;elsex=x.right;}z.parent=y;if(y==this.nilNode){tree.root=z;}else{if(value<y.key){y.left=z;}else{y.right=z;}}z.left=this.nilNode;z.right=this.nilNode;insertFixUp(tree,z);}public void insert(int value){insert(this,value);}private void insertFixUp(MyRbTree tree,RbTreeNode z){while(z.parent.color==TreeNodeColor.Red){if(z.parent==z.parent.parent.left){RbTreeNode uncle=z.parent.parent.right;if(uncle.color==TreeNodeColor.Red){z.parent.color=TreeNodeColor.Black;uncle.color=TreeNodeColor.Black;z.parent.parent.color=TreeNodeColor.Red;z=z.parent.parent;}else{if(z==z.parent.right){z=z.parent;leftRotate(tree,z);}z.parent.color=TreeNodeColor.Black;z.parent.parent.color=TreeNodeColor.Red;rightRotate(tree,z.parent.parent);}}else if(z.parent==z.parent.parent.right){RbTreeNode uncle=z.parent.parent.left;if(uncle.color==TreeNodeColor.Red){z.parent.color=TreeNodeColor.Black;uncle.color=TreeNodeColor.Black;z.parent.parent.color=TreeNodeColor.Red;z=z.parent.parent;}else{if(z==z.parent.left){z=z.parent;rightRotate(tree,z);}z.parent.color=TreeNodeColor.Black;z.parent.parent.color=TreeNodeColor.Red;leftRotate(tree,z.parent.parent);}}}tree.root.color=TreeNodeColor.Black;}public void preorder(RbTreeNode root){if(root!=null){preorder(root.left);getNodeInfo(root);preorder(root.right);}}public void getNodeInfo(RbTreeNode node){if(node!=this.nilNode){System.out.println(node.key+" "+node.color.toString());}}}class RbTreeNode{TreeNodeColor color=TreeNodeColor.Red;int key=-1;RbTreeNode left=null;RbTreeNode right=null;RbTreeNode parent=null;public RbTreeNode(int value){this.key=value;}}
The running result is as follows:
2 Black2 Black5 Red2 Red3 Black5 Red2 Black3 Black4 Red5 Black1 Red2 Black3 Black4 Red5 Black0 Red1 Black2 Red3 Black4 Red5 Black
From the running results, we can see that there is no problem with the program. You can mention the improvements. Thank you!
Today, I took a look at the knowledge about the deletion of the red and black trees in the introduction to algorithms. Now, I have implemented it myself and added it to the above program. The program code and running results are as follows, I wrote the Pseudo Code strictly in accordance with the introduction to algorithms. I believe you can see it clearly.
Package COM. datastructure. tree; public class myrbtree {rbtreenode root = NULL; rbtreenode nilnode = NULL; Public rbtreenode getroot () {return root;} public myrbtree () {This. nilnode = new rbtreenode (-1); this. nilnode. color = treenodecolor. black; this. nilnode. left = NULL; this. nilnode. right = NULL; this. nilnode. parent = NULL; this. root = This. nilnode;}/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-Ge Nerated method stubmyrbtree mytree = new myrbtree (); int [] array = {41,38, 31,12, 19,8}; For (INT I = 0; I <array. length; I ++) {system. out. println ("insert" + array [I]); mytree. insert (array [I]); mytree. preorder (mytree. getroot (); system. out. println () ;}for (INT I = 5; I> = 0; I --) {system. out. println ("delete" + array [I]); mytree. delete (array [I]); mytree. preorder (mytree. getroot (); system. out. println () ;}} private void leftrotate (myrbtree tre E, rbtreenode X) {rbtreenode y = x. right; X. right = y. left; Y. left. parent = x; Y. parent = x. parent; If (X. parent = This. nilnode) {tree. root = y;} else {If (x = x. parent. left) X. parent. left = y; elsex. parent. right = y;} y. left = x; X. parent = y;} private void rightrotate (myrbtree tree, rbtreenode y) {rbtreenode x = y. left; Y. left = x. right; X. right. parent = y; X. parent = y. parent; If (Y. parent = This. nilnode) {tree. root = x;} else {If (y = y. parent. left) Y. Parent. left = x; Elsey. parent. right = x;} X. right = y; Y. parent = x;} private void insert (myrbtree tree, int value) {rbtreenode y = This. nilnode; rbtreenode x = tree. root; rbtreenode z = new rbtreenode (value); While (X! = This. nilnode) {Y = x; If (value <X. key) x = x. left; elsex = x. right;} Z. parent = y; If (y = This. nilnode) {tree. root = z;} else {If (value <Y. key) {Y. left = z;} else {Y. right = z ;}} Z. left = This. nilnode; Z. right = This. nilnode; insertfixup (tree, Z);} public void insert (INT value) {insert (this, value);} private rbtreenode search (myrbtree tree, int value) {rbtreenode node = tree. root; while (node! = Tree. nilnode) & (node. Key! = Value) {If (value <node. key) {node = node. left;} else {node = node. right ;}} return node;} public Boolean search (INT value) {If (search (this, value )! = This. nilnode) return true; elsereturn false;} public rbtreenode minimum (rbtreenode X) {While (X. Left! = This. nilnode) {x = x. Left;} return X;} public rbtreenode successor (rbtreenode X) {If (X. Right! = This. nilnode) {return minimum (X. Right);} rbtreenode y = x. parent; while (y! = This. nilnode & X = y. right) {x = y; y = y. parent;} return y;} private Boolean Delete (myrbtree tree, int value) {rbtreenode z = search (tree, value); If (Z = tree. nilnode) {return false;} rbtreenode y = NULL; If (Z. left = tree. nilnode | Z. right = tree. nilnode) {Y = z;} else {Y = successor (z);} rbtreenode x = NULL; If (Y. left! = Tree. nilnode) {x = y. left;} else {x = y. right;} X. parent = y. parent; If (Y. parent = tree. nilnode) {This. root = x;} else {If (y = y. parent. left) {Y. parent. left = x;} else {Y. parent. right = x ;}} if (y! = Z) {z. key = y. key;} If (Y. color = treenodecolor. black) {deletefixup (tree, x);} return true;} private void deletefixup (myrbtree tree, rbtreenode X) {While (X! = Tree. root & X. color = treenodecolor. black) {If (x = x. parent. left) {rbtreenode W = x. parent. right; If (W. color = treenodecolor. red) {W. color = treenodecolor. black; X. color = treenodecolor. red; leftrotate (tree, X. parent); W = x. parent. right;} If (W. left. color = treenodecolor. black & W. right. color = treenodecolor. black) {W. color = treenodecolor. red; X = x. parent;} else {If (W. right. color = treenodecolor. black) {W. left. color = treenodecolo R. black; W. color = treenodecolor. red; rightrotate (tree, W); W = x. parent. right;} W. color = x. parent. color; X. parent. color = treenodecolor. black; W. right. color = treenodecolor. black; leftrotate (tree, X. parent); X = tree. root;} else {rbtreenode W = x. parent. left; If (W. color = treenodecolor. red) {W. color = treenodecolor. black; X. color = treenodecolor. red; rightrotate (tree, X. parent); W = x. parent. left;} If (W. left. color = treenodecolor. blac K & W. right. color = treenodecolor. black) {W. color = treenodecolor. red; X = x. parent;} else {If (W. left. color = treenodecolor. black) {W. right. color = treenodecolor. black; W. color = treenodecolor. red; leftrotate (tree, W); W = x. parent. left;} W. color = x. parent. color; X. parent. color = treenodecolor. black; W. left. color = treenodecolor. black; leftrotate (tree, X. parent); X = tree. root; }}x. color = treenodecolor. black;} public Boolean Delete (I NT value) {return Delete (this, value);} private void insertfixup (myrbtree tree, rbtreenode Z) {While (Z. parent. color = treenodecolor. red) {If (Z. parent = z. parent. parent. left) {rbtreenode uncle = z. parent. parent. right; If (uncle. color = treenodecolor. red) {z. parent. color = treenodecolor. black; uncle. color = treenodecolor. black; Z. parent. parent. color = treenodecolor. red; Z = z. parent. parent;} else {If (Z = z. parent. right) {z = z. par Ent; leftrotate (tree, Z);} Z. parent. color = treenodecolor. black; Z. parent. parent. color = treenodecolor. red; rightrotate (tree, Z. parent. parent) ;}} else if (Z. parent = z. parent. parent. right) {rbtreenode uncle = z. parent. parent. left; If (uncle. color = treenodecolor. red) {z. parent. color = treenodecolor. black; uncle. color = treenodecolor. black; Z. parent. parent. color = treenodecolor. red; Z = z. parent. parent;} else {If (Z = z. parent. left) {Z = z. parent; rightrotate (tree, Z);} Z. parent. color = treenodecolor. black; Z. parent. parent. color = treenodecolor. red; leftrotate (tree, Z. parent. parent) ;}} tree. root. color = treenodecolor. black;} public void preorder (rbtreenode root) {If (root! = NULL) {preorder (root. Left); getnodeinfo (Root); preorder (root. Right) ;}} public void getnodeinfo (rbtreenode node) {If (node! = This. nilnode) {system. out. println (node. key + "" + node. color. tostring () ;}} class rbtreenode {treenodecolor color = treenodecolor. red; int key =-1; rbtreenode left = NULL; rbtreenode right = NULL; rbtreenode parent = NULL; Public rbtreenode (INT value) {This. key = value ;}}
The running result is as follows:
Insert 4141 black insert 3838 red41 black insert 3131 red38 black41 red insert 1212 red31 black38 black41 black insert 1912 red19 black31 red38 black41 black insert 88 red12 black19 red31 black38 black41 black Delete 812 black19 red31 black38 black41 black delete 1912 red31 black38 black41 black Delete 1231 black38 black41 black Delete 3138 black41 red Delete 3841 black Delete 41
I used paper to draw a red-black tree map for insertion and deletion, which is consistent with the color of each node in the running result. I personally think there is no problem. If there is any problem, welcome to the discussion. Thank you ....