歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 紅黑樹的插入實現

紅黑樹的插入實現

日期:2017/3/1 11:09:45   编辑:Linux編程

紅黑樹的插入實現

  1. package com.lkl;
  2. import java.util.*;
  3. public class RBTree {
  4. private static final boolean RED=false;
  5. private static final boolean BLACK=true;
  6. static class Node{
  7. int key;
  8. Node left;
  9. Node right;
  10. Node parent;
  11. boolean color;
  12. //the default color of the new Node is Red
  13. public Node(int k){
  14. this.key=k;
  15. this.color=RED;
  16. }
  17. }
  18. private Node root;
  19. public RBTree(){
  20. root=null;
  21. }
  22. /** LEFT ROTATE
  23. * X Y
  24. * ------ ------------
  25. * | Y X |
  26. * a ----- -------- c
  27. * | | | |
  28. * b c a b
  29. *
  30. */
  31. public void leftRotate(RBTree T,Node x){
  32. Node y=x.right;
  33. x.right=y.left;
  34. if(y.left!=null){
  35. y.left.parent=x;
  36. }
  37. y.parent=x.parent;
  38. if(x.parent==null){
  39. T.root=y;
  40. }else if(x==x.parent.left){
  41. x.parent.left=y;
  42. }else{
  43. x.parent.right=y;
  44. }
  45. x.parent=y;
  46. y.left=x;
  47. }
  48. /** X Y
  49. * -------- -------
  50. * Y c a X
  51. * ----- ------
  52. * a b b c
  53. *
  54. * @param t
  55. * @param x
  56. */
  57. public void rightRotate(RBTree t,Node x){
  58. Node y=x.left;
  59. x.left=y.right;
  60. if(y.right!=null){
  61. y.right.parent=x;
  62. }
  63. y.parent=x.parent;
  64. if(x.parent==null){ //x is the root
  65. t.root=y;
  66. }else if(x==x.parent.left){
  67. x.parent.left=y;
  68. }else{
  69. x.parent.right=y;
  70. }
  71. y.right=x;
  72. x.parent=y;
  73. }
  74. public void insert(RBTree t,Node z){
  75. Node y=null;
  76. Node x=t.root;
  77. while(x!=null){ //find the right location
  78. y=x;
  79. if(z.key<x.key){
  80. x=x.left;
  81. }else{
  82. x=x.right;
  83. }
  84. }
  85. z.parent=y; //insert node z
  86. if(y==null){ //the RBTRee is empty
  87. t.root=z; //the root must be z
  88. }else if(z.key<y.key){
  89. y.left=z;
  90. }else{
  91. y.right=z;
  92. }
  93. insertFixup(t,z); //insert the node may break the rule 4
  94. }
  95. //when we insert a Node ,we can classify the cases into 6 type
  96. //case2,3,5,6 (when the uncle Node is bleak or uncle don't exist)
  97. private void insertFixup(RBTree t, Node z) {
  98. while((z.parent!=null)&&(z.parent.color==RED)){ //this break the rule 4,there have 6 cases
  99. Node uncle;
  100. if(z.parent==z.parent.parent.left) { //case 1-3
  101. uncle=z.parent.parent.right; //find the uncle
  102. if((uncle!=null)&&(uncle.color==RED)){ //case 1
  103. z.parent.color=BLACK;
  104. uncle.color=BLACK;
  105. z.parent.parent.color=RED;
  106. z=z.parent.parent;
  107. }else {
  108. if(z==z.parent.right){ //judge uncle.color==black is wrong, case 2
  109. z=z.parent; //when only 2 nodes in the tree,the uncle don't exist
  110. t.leftRotate(t, z); //change case 2 to case 3
  111. }
  112. z.parent.color=BLACK; //case 3
  113. z.parent.parent.color=RED; //case 3
  114. t.rightRotate(t, z.parent.parent);
  115. }
  116. }else{
  117. uncle=z.parent.parent.left;
  118. if((uncle!=null)&&(uncle.color==RED)){ //case 4
  119. z.parent.color=BLACK;
  120. uncle.color=BLACK;
  121. z.parent.parent.color=RED;
  122. z=z.parent.parent;
  123. }else {
  124. if(z==z.parent.left){ //case 5
  125. z=z.parent;
  126. t.rightRotate(t,z); //change case5 to case6
  127. }
  128. z.parent.color=BLACK;
  129. z.parent.parent.color=RED;
  130. t.leftRotate(t, z.parent.parent);
  131. }
  132. }
  133. }
  134. t.root.color=BLACK;
  135. }
  136. public static void main(String[] args){
  137. RBTree t=new RBTree();
  138. int MAX=1000000;
  139. Node x[]=new Node[MAX];
  140. Random r=new Random();
  141. long startTime=System.currentTimeMillis();
  142. for(int i=0;i<MAX;i++){
  143. x[i]=new Node((int) (Math.random()*100));
  144. t.insert(t, x[i]);
  145. }
  146. long endTime=System.currentTimeMillis();
  147. System.out.println(" process runtime :"+(endTime-startTime)+"ms");
  148. // Node a=new Node(41);
  149. // Node b=new Node(38);
  150. // Node c=new Node(31);
  151. // Node d=new Node(12);
  152. // Node e=new Node(19);
  153. // Node f=new Node(8);
  154. // // x[10]=new Node(3);
  155. // t.insert(t, a);
  156. // t.insert(t, b);
  157. // t.insert(t, c);
  158. // t.insert(t, d);
  159. // t.insert(t, e);
  160. // t.insert(t, f);
  161. System.out.println(t.root.color);
  162. System.out.println(x[10].color);
  163. }
  164. }
Copyright © Linux教程網 All Rights Reserved