/* * Sequential statistics tree */public class Orderstatistictree {public static final Ostreenode NIL = new Ostreenode (rbcolor.black,0);p rivate Ostreenode root = null;public static void Main (string[] args) {Orderstatistictree ost = new Orderstatistictree ();//test Insert OS T.insertostreenode (New Ostreenode (11,rbcolor.black)); Ost.insertostreenode (new Ostreenode (2,rbcolor.red)); O St.insertostreenode (New Ostreenode (1,rbcolor.black)); Ost.insertostreenode (new Ostreenode (7,rbcolor.black)); O St.insertostreenode (New Ostreenode (5,rbcolor.red)); Ost.insertostreenode (new Ostreenode (8,rbcolor.red)); O St.insertostreenode (New Ostreenode (14,rbcolor.black)); Ost.insertostreenode (new Ostreenode (15,RbColor.RED)); O St.printtree (); Ost.deleteostreenode (Ost.searchostreenode (Ost.root, 7)); Ost.printtree ();// Print a small number of System.out.println (Ost.osselect (ost.root,7). key);//Find the order System.out.println of a node (Ost.osrank ( Ost.searchostreenode (Ost.root, 14)));} Public Orderstatistictree () {this.root = NIL;} Middle sequence traversal binary tree private void TraverseTree (Ostreenode x) {if (x! = NIL) {TraverseTree (x.left); System.out.println (X.key + "+ X.color +" "+x.size); TraverseTree (x.right);}} private void Printtree () {System.out.println ("root:" +root.key+ "" +root.color); TraverseTree (root); System.out.println ();} Set the order statistics tree for each node of size//private int setostreenodesize (Ostreenode x) {//if (x = = NIL) {//return 0;//} else {//return Setostreen Odesize (X.left) +setostreenodesize (x.right) +1;//}//}//Find the node of small i keyword private ostreenode osselect (Ostreenode x,int i) {if (i<0) {System.out.println ("invalidate parameter!"); return null;} int r = x.left.size+1;if (i==r) {return x;} else if (i<r) {return osselect (x.left,i);} else {return Osselect (x.right,i-r) ;}} Find the order of the X node private int osrank (Ostreenode x) {int r = X.left.size+1;ostreenode Y=x;while (Y! = root) {if (y = = y.parent.right) {R = R + y.parent.left.size + 1;} y = y.parent;} return r;} Find a child node private Ostreenode searchostreenode (Ostreenode x,int key) {while (x! = NIL && Key! = X.key) {if (Key < X . key) {x = X.left;} else {x = X.right;}} return x;}Insert a child node private void Insertostreenode (Ostreenode z) {Ostreenode y = nil;ostreenode x = root;while (x! = NIL) {y = x;if (z.k EY < X.key) {x = X.left;} else {x = X.right;}} Z.parent = y;if (y = = NIL) {root = Z;} else if (Z.key < Y.key) {y.left = Z;} else {y.right = Z;} Z.left = Nil;z.right = Nil;z.color = rbcolor.red;//is inserted into a leaf node, and the leaf node size is 1z.size = 1;sizefixup (z.parent); Osinsertfixup (z); /insert fix}//insert fix private void Osinsertfixup (Ostreenode z) {Ostreenode y = null;while (Z.parent.color = = rbcolor.red) {///when the parent node of Z If it is black, it is not necessary to correct if (z.parent = = Z.parent.parent.left) {//left branch case y = z.parent.parent.right;//according to tert-node sub-condition if (Y.color = = rbcolor.red) {Z.parent.color = Rbcolor.black;y.color = Rbcolor.black;z.parent.parent.color = Rbcolor.red;z = z.parent.parent;} else {if (z = = z.parent.right) {z = Z.parent;leftrotate (z);} Z.parent.color = Rbcolor.black;z.parent.parent.color = Rbcolor.red;rightrotate (z.parent.parent);}} else {y = z.parent.parent.left;//according to the tert-node sub-condition if (Y.color = = rbcolor.red) {Z.parent.color = Rbcolor.black;y.color = Rbcolor.black;z.parent.parent.color = Rbcolor.red;z = z.parent.parent;} else {if (z = = z.parent.left) {z = Z.parent;rightrotate (z);} Z.parent.color = Rbcolor.black;z.parent.parent.color = Rbcolor.red;leftrotate (z.parent.parent);}}} Root.color = Rbcolor.black;} Delete a child node private void Deleteostreenode (Ostreenode z) {Ostreenode y = z;ostreenode x = NIL; Rbcolor Yoriginalcolor = y.color;if (Z.left = = NIL) {x = Z.right;ostransplant (z,z.right); Z.right.size--;sizefixup ( z.right.parent);//fixed size} else if (z.right = = NIL) {z.parent.size--;sizefixup (z.right.parent) from the root node to the z-node path; x = Z.left;o Stransplant (Z,z.left); Z.left.size--;sizefixup (z.left.parent);//fix size from root node to Z-node path} else {y = Searchminnode (z.right) ; yoriginalcolor = Y.color;x = y.right;if (y.parent = = z) {x.parent = y;} else {ostransplant (y,y.right); y.right = Z.right;z. Right.parent = y;} Ostransplant (z,y); y.left = Z.left;z.left.parent = Y;y.color = Z.color;x.parent.size--;sizefixup (x.parent.parent);// Fix size}if (Yoriginalcolor = = Rbcolor from the root node to the Z-node path. BLACK) {osdeletefixup (x);}} private void Sizefixup (Ostreenode x) {Ostreenode y = x;while (Y! = NIL) {y.size = y.left.size + y.right.size + 1;y = Y.pare NT;}} private void Osdeletefixup (Ostreenode x) {//x always points to a non-root node with double black Ostreenode w = nil;while (x! = root && X.color = RbC Olor. BLACK) {if (x = = X.parent.left) {w = x.parent.right;//w points to Brother node if (W.color = = rbcolor.red) {W.color = Rbcolor.black;//case 1x.p Arent.color = Rbcolor.red;//case 1leftRotate (x.parent);//case 1w = X.parent.right;//case 1}if (W.left.color = = Rbcolor.black && W.right.color = = rbcolor.black) {W.color = rbcolor.black;//case 2x = x.parent;//case 2}else {if (w . Right.color = = rbcolor.black) {W.left.color = rbcolor.black;//case 3w.color = rbcolor.red;//case 3rightRotate (w);// Case 3w = x.parent.right;//case 3}w.color = x.parent.color;//case 4x.parent.color = rbcolor.black;//case 4w.right.color = Rbcolor.black;//case 4leftRotate (x.parent);//case 4x = Root;//case 4}} else {w = x.parent.left;//w point to Brother if (W.color = = Rbco Lor. RED) {W.color = rbcolor.black;//case 1x.parent.color = rbcolor.red;//case 1rightRotate (x.parent);//case 1w = X.parent.left;//case 1}if (W.right.color = = Rbcolor.black && W.left.color = rbcolor.black) {W.color = Rbcolor.black;//case 2x = x.parent;//case 2}else {if (W.left.color = = rbcolor.black) {W.right.color = RbColor.BLACK;// Case 3w.color = Rbcolor.red;//case 3leftRotate (w);//case 3w = x.parent.left;//case 3}w.color = X.parent.color; X.parent.color = Rbcolor.black;w.left.color = Rbcolor.black;rightrotate (x.parent); x = root;}}} private void Ostransplant (Ostreenode U,ostreenode v) {if (u.parent = = NIL) {root = v;} else if (U = = u.parent.left) {U.paren T.left = V;} else {u.parent.right = v;} V.parent = u.parent;} Gets the minimum key value node private Ostreenode searchminnode (Ostreenode x) {while (x.left! = NIL) {x = X.left;} return x;} Gets the maximum key value node private Ostreenode searchmaxnode (Ostreenode x) {while (x.right! = NIL) {x = X.right;} return x;} L-private void Leftrotate (Ostreenode x) {Ostreenode y = X.right;x.rigHT = Y.LEFT;IF (x.right! = NIL) {y.left.parent = x;} Y.parent = x.parent;if (x.parent = = NIL) {//If the root node is root = y;} else if (x = = x.parent.left) {x.parent.left = y;} else {X.paren T.right = y;} Y.left = X;x.parent = y;//size Property Maintenance y.size = X.size;x.size = x.left.size + x.right.size + 1;} Right-handed private void rightrotate (Ostreenode y) {Ostreenode x = Y.left;y.left = X.right;if (x.right! = NIL) {x.right.parent = y; }x.parent = y.parent;if (y.parent = = NIL) {root = x;} else if (y = = y.parent.left) {y.parent.left = x;} else {Y.parent.right = x;} X.right = Y;y.parent = x;//size Property Maintenance x.size = Y.size;y.size = y.left.size + y.right.size + 1;} Red black tree Node class private static class Ostreenode {Ostreenode left = Null;ostreenode right = Null;ostreenode parent = null; Rbcolor color = Rbcolor.red;int Key = 0;int size = 0;public ostreenode (int key,rbcolor color) {This.key = Key;this.color = Color;} Public Ostreenode (rbcolor color,int size) {This.color = Color;this.size = size;}} Private enum Rbcolor {Red,black}}
Sequential Statistics tree