Advertisement
Guest User

rbt

a guest
May 6th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. public class RBTree <Key extends Comparable<Key>> {
  2.  
  3. private static final boolean RED = true;
  4. private static final boolean BLACK = false;
  5.  
  6. Node root;
  7.  
  8.  
  9. /* 1)jesli puste to dodajemy jako czarny korzeń
  10. * 2)else{ skladamy na prawo/lewo w zaleznosci od wielkosci
  11. * a) if parent is black OK!
  12. * b) if parent is red
  13. * -if black or absent sibling = rotuj
  14. * -if red sibling, recolour & check again
  15. */
  16. //NO LINK TO PARENT
  17.  
  18. public RBTree(){
  19. root=null;
  20. }
  21.  
  22. private boolean isRed( Node x){
  23. return x!= null && x.color==RED;
  24. }
  25.  
  26.  
  27. public void insert(Key key){
  28. root = insert(key, root);
  29. root.color=BLACK;
  30. }
  31.  
  32.  
  33. public Node insert(Key key, Node aktualny) {
  34. /*if (root == null) {
  35. root = new Node(key);
  36.  
  37. }*/
  38. if(aktualny == null){
  39. return new Node(key);
  40. }
  41. //aktualny = root;
  42. //while(true){
  43. int comp = key.compareTo(aktualny.key);
  44. //jesli wstawiany<aktualnego
  45. if(comp<0)
  46. aktualny.left=insert(key,aktualny.left);
  47. else if(comp>0)
  48. aktualny.right=insert(key,aktualny.right);
  49.  
  50. else throw new RuntimeException("Duplicate"+key.toString());
  51.  
  52. //break;}
  53.  
  54. if (isRed(aktualny.left) && isRed(aktualny.left.left))
  55. aktualny.color=RED;
  56. aktualny.left.color=BLACK;
  57. aktualny = rotateR(aktualny);
  58.  
  59. if (isRed(aktualny.right) && isRed(aktualny.right.right))
  60. aktualny.color=RED;
  61. aktualny.right.color=BLACK;
  62. aktualny = rotateL(aktualny);
  63.  
  64. if (isRed(aktualny.left) && isRed(aktualny.left.right))
  65. aktualny.color=RED;
  66. aktualny.left.right.color=BLACK;
  67. aktualny.left = rotateL(aktualny.left);
  68. aktualny = rotateR(aktualny);
  69.  
  70. if( isRed( aktualny.right ) && isRed( aktualny.right.left ) ) {
  71. aktualny.color =RED ;
  72. aktualny.right.left.color = BLACK ;
  73. aktualny.right= rotateR(aktualny.right);
  74. aktualny = rotateL( aktualny );
  75. }
  76.  
  77. if (isRed(aktualny.left) && isRed(aktualny.right))
  78. colorFlip(aktualny);
  79.  
  80.  
  81.  
  82.  
  83. return aktualny;
  84. }
  85.  
  86. /*public int zewn(Node aktualny){
  87. if(aktualny == null) return 1;
  88. return aktualny.key = zewn(aktualny.left)+zewn(aktualny.right);
  89. }*/
  90.  
  91. private void colorFlip(Node aktualny){
  92. aktualny.color=!aktualny.color;
  93. aktualny.left.color=!aktualny.left.color;
  94. aktualny.right.color=!aktualny.right.color;
  95. }
  96.  
  97. /*private Node rotateL(Node aktualny)
  98. { Node key=aktualny.right;
  99. aktualny.right=key.left;
  100. key.left=aktualny;
  101. key.color=aktualny.color;
  102. aktualny.color=RED;
  103. return key; }*/
  104.  
  105. private Node rotateL(Node aktualny) {
  106. assert (aktualny != null) && isRed(aktualny.right);
  107. Node x = aktualny.right;
  108. aktualny.right = x.left;
  109. x.left = aktualny;
  110. x.color = x.left.color;
  111. x.left.color = RED;
  112. return x;
  113. }
  114. private Node rotateR(Node aktualny) {
  115. assert (aktualny != null) && isRed(aktualny.right);
  116. Node x = aktualny.left;
  117. aktualny.left = x.right;
  118. x.right = aktualny;
  119. x.color = x.right.color;
  120. x.right.color = RED;
  121. return x;
  122. }
  123. /*private Node rotateR(Node aktualny)
  124. { Node key=aktualny.left;
  125. aktualny.left=key.right;
  126. key.right=aktualny;
  127. key.color=aktualny.color;
  128. aktualny.color=RED;
  129. return key; }*/
  130.  
  131.  
  132. public Node findNode(Key key){
  133. Node aktualny = root;
  134. int comp = 0;
  135. while (aktualny != null && (comp = key.compareTo(aktualny.key))!=0)
  136. aktualny = comp<0 ? aktualny.left : aktualny.right;
  137. return aktualny;
  138. }
  139.  
  140. public void printInOrder(Node aktualny)
  141. { //od najmniejszego do najwiekszego
  142. if(aktualny.left!=null)
  143. printInOrder(aktualny.left);
  144. System.out.println(aktualny);
  145. if(aktualny.right!=null)
  146. printInOrder(aktualny.right);
  147. }
  148. public void printPostOrder(Node aktualny)
  149. { //parent comes last
  150. if(aktualny.left!=null)
  151. printPostOrder(aktualny.left);
  152. if(aktualny.right!=null)
  153. printPostOrder(aktualny.right);
  154. System.out.println(aktualny);
  155. }
  156. public void printPreOrder(Node aktualny)
  157. {
  158. if(aktualny != null){
  159. System.out.println(aktualny);
  160. printPreOrder(aktualny.left);
  161. printPreOrder(aktualny.right);
  162. }
  163. }
  164. public int height(Node aktualny){
  165.  
  166. if(aktualny == null){
  167. return 0;
  168. }else{
  169. //jako ze to nie koniec to dodajemy jeden do obecnie obliczonej juz wysokosci,
  170. //wieksza z głebokosci, bo poddrzewo lew i prawe mogą byc roznej wiekosci
  171. return 1 + Math.max(height(aktualny.left), height(aktualny.right));
  172. }
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179. private class Node {
  180.  
  181. Key key; // key
  182. Node left;
  183. Node right; // links to left and right subtrees
  184. boolean color; // color of parent link
  185. int N; // subtree count
  186.  
  187. public Node(Key key) {
  188. this.key = key;
  189. left = right = null;
  190. color = RED;
  191. }
  192.  
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement