Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. package Main;
  2.  
  3. import java.util.NoSuchElementException;
  4. import java.lang.String;
  5.  
  6.  
  7. public class RB_Tree {
  8. public Node root;
  9. public static final boolean RED = true;
  10. public static final boolean BLACK = false;
  11.  
  12. public boolean isRed(Node x){
  13. if(x==null)
  14. return false;
  15. return x.color==RED;
  16. }
  17. public void insert(int key,String val){
  18. if(val==null){
  19. return;
  20. }
  21. root=insert(root,key,val);
  22. root.color=BLACK;
  23. }
  24.  
  25.  
  26. public Node insert(Node h,int key,String val){
  27. if(h==null)
  28. return new Node(key,val,RED);
  29.  
  30. int cmp= h.compareTo(key);
  31.  
  32. if(cmp<0)
  33. h.left=insert(h.left,key,val);
  34. else if(cmp>0)
  35. h.right = insert(h.right, key, val);
  36. else
  37. h.val=val;
  38. return h;
  39. }
  40. public void delete(float key) {
  41. if (0.0f == key) throw new IllegalArgumentException("argument to delete() is null");
  42. if (!contains(key)) return;
  43.  
  44. // if both children of root are black, set root to red
  45. if (!isRed(root.left) && !isRed(root.right))
  46. root.color = RED;
  47.  
  48. root = delete(root, key);
  49. if (!isEmpty()) root.color = BLACK;
  50. // assert check();
  51. }
  52.  
  53. // delete the key-String pair with the given key rooted at h
  54. private Node delete(Node h, float key) {
  55. if (key.compareTo(h.key) < 0) {
  56. if (!isRed(h.left) && !isRed(h.left.left))
  57. h = moveRedLeft(h);
  58. h.left = delete(h.left, key);
  59. }
  60. else {
  61. if (isRed(h.left))
  62. h = rotateRight(h);
  63. if (key.compareTo(h.key) == 0 && (h.right == null))
  64. return null;
  65. if (!isRed(h.right) && !isRed(h.right.left))
  66. h = moveRedRight(h);
  67. if (key.compareTo(h.key) == 0) {
  68. Node x = min(h.right);
  69. h.key = x.key;
  70. h.val = x.val;
  71. h.right = deleteMin(h.right);
  72. }
  73. else h.right = delete(h.right, key);
  74. }
  75. return balance(h);
  76. }
  77. private Node balance(Node h) {
  78.  
  79. if (isRed(h.right)) h = rotateLeft(h);
  80. if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
  81. if (isRed(h.left) && isRed(h.right)) flipColors(h);
  82.  
  83. h.size = size(h.left) + size(h.right) + 1;
  84. return h;
  85. }
  86. private Node rotateRight(Node h) {
  87. Node x = h.left;
  88. h.left = x.right;
  89. x.right = h;
  90. x.color = x.right.color;
  91. x.right.color = RED;
  92. x.size = h.size;
  93. h.size = size(h.left) + size(h.right) + 1;
  94. return x;
  95. }
  96. private Node rotateLeft(Node h) {
  97. Node x = h.right;
  98. h.right = x.left;
  99. x.left = h;
  100. x.color = x.left.color;
  101. x.left.color = RED;
  102. x.size = h.size;
  103. h.size = size(h.left) + size(h.right) + 1;
  104. return x;
  105. }
  106.  
  107. private void flipColors(Node h) {
  108. h.color = !h.color;
  109. h.left.color = !h.left.color;
  110. h.right.color = !h.right.color;
  111. }
  112.  
  113. private Node moveRedLeft(Node h) {
  114. flipColors(h);
  115. if (isRed(h.right.left)) {
  116. h.right = rotateRight(h.right);
  117. h = rotateLeft(h);
  118. flipColors(h);
  119. }
  120. return h;
  121. }
  122. private Node moveRedRight(Node h) {
  123. flipColors(h);
  124. if (isRed(h.left.left)) {
  125. h = rotateRight(h);
  126. flipColors(h);
  127. }
  128. return h;
  129. }
  130. public boolean isEmpty() {
  131. return root == null;
  132. }
  133. public void deleteMin() {
  134. if (isEmpty()) throw new NoSuchElementException("BST underflow");
  135. if (!isRed(root.left) && !isRed(root.right))
  136. root.color = RED;
  137.  
  138. root = deleteMin(root);
  139. if (!isEmpty()) root.color = BLACK;
  140. }
  141. private Node deleteMin(Node h) {
  142. if (h.left == null)
  143. return null;
  144.  
  145. if (!isRed(h.left) && !isRed(h.left.left))
  146. h = moveRedLeft(h);
  147.  
  148. h.left = deleteMin(h.left);
  149. return balance(h);
  150. }
  151. public void deleteMax() {
  152. if (isEmpty()) throw new NoSuchElementException("BST underflow");
  153. if (!isRed(root.left) && !isRed(root.right))
  154. root.color = RED;
  155.  
  156. root = deleteMax(root);
  157. if (!isEmpty()) root.color = BLACK;
  158.  
  159. }
  160. private Node deleteMax(Node h) {
  161. if (isRed(h.left))
  162. h = rotateRight(h);
  163.  
  164. if (h.right == null)
  165. return null;
  166.  
  167. if (!isRed(h.right) && !isRed(h.right.left))
  168. h = moveRedRight(h);
  169.  
  170. h.right = deleteMax(h.right);
  171.  
  172. return balance(h);
  173. }
  174.  
  175. public float min() {
  176. if (isEmpty()) throw new NoSuchElementException("calls min() with empty symbol table");
  177. return min(root).key;
  178. }
  179. private Node min(Node x) {
  180. if (x.left == null) return x;
  181. else return min(x.left);
  182. }
  183.  
  184. public float max() {
  185. if (isEmpty()) throw new NoSuchElementException("calls max() with empty symbol table");
  186. return max(root).key;
  187. }
  188. private Node max(Node x) {
  189. // assert x != null;
  190. if (x.right == null) return x;
  191. else return max(x.right);
  192. }
  193. private int size(Node x) {
  194. if (x == null) return 0;
  195. return x.size;
  196. }
  197. public int size() {
  198. return size(root);
  199. }
  200. public boolean contains(float key) {
  201. return get(key) != null;
  202. }
  203. public String get(float key) {
  204. if (0.0f == key) throw new IllegalArgumentException("argument to get() is null");
  205. return get(root, key);
  206. }
  207.  
  208. // String associated with the given key in subtree rooted at x; null if no such key
  209. private String get(Node x, float key) {
  210. while (x != null) {
  211. int cmp = key.compareTo(x.key);
  212. if (cmp < 0) x = x.left;
  213. else if (cmp > 0) x = x.right;
  214. else return x.val;
  215. }
  216. return null;
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement