Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. public class MyTreeMap {
  2.  
  3. private Node root;
  4. private int size;
  5. private Node nullNode;
  6.  
  7. public int getValue(double key) {
  8. return find(key);
  9. }
  10.  
  11. public MyTreeMap() {
  12. nullNode = new Node();
  13. nullNode.left = nullNode;
  14. nullNode.right = nullNode;
  15. nullNode.parent = null;
  16. nullNode.color = 1;
  17. nullNode.key = 0.0;
  18. root = nullNode;
  19. size = 0;
  20. }
  21.  
  22. public boolean isEmpty() {
  23. return size == 0;
  24. }
  25.  
  26. public int size() {
  27. return size;
  28. }
  29.  
  30. public int find(double key) {
  31. Node start = root;
  32.  
  33. while (start != nullNode) {
  34. if (start.key > key)
  35. start = start.left;
  36. else if (start.key < key)
  37. start = start.right;
  38. else
  39. return start.value;
  40. }
  41. return 0;
  42. }
  43.  
  44. public void insert(double key, int value) {
  45. Node start = root;
  46. Node p = null;
  47.  
  48. ++size;
  49. while (start != nullNode) {
  50. p = start;
  51. if (start.key > key)
  52. start = start.left;
  53. else if (start.key < key)
  54. start = start.right;
  55. else {
  56. start.value = value;
  57. return;
  58. }
  59. }
  60.  
  61. start = new Node();
  62. start.key = key;
  63. start.value = value;
  64. start.parent = p;
  65. start.left = nullNode;
  66. start.right = nullNode;
  67. start.color = 0;
  68.  
  69. if (p != null)
  70. if (p.key > key)
  71. p.left = start;
  72. else
  73. p.right = start;
  74. else
  75. root = start;
  76.  
  77. while (start != root && start.parent.color == 0) {
  78. if (start.parent.parent.left == start.parent) {
  79. if (start.parent.parent.right.color == 0) {
  80. start.parent.color = 1;
  81. start.parent.parent.right.color = 1;
  82. start.parent.parent.color = 0;
  83. start = start.parent.parent;
  84. }
  85. else {
  86. if (start.parent.left != start) {
  87. start = start.parent;
  88. rotateLeft(start);
  89. }
  90.  
  91. start.parent.color = 1;
  92. start.parent.parent.color = 0;
  93. rotateRight(start.parent.parent);
  94. }
  95. }
  96. else {
  97. if (start.parent.parent.left.color == 0) {
  98. start.parent.color = 1;
  99. start.parent.parent.left.color = 1;
  100. start.parent.parent.color = 0;
  101. start = start.parent.parent;
  102. }
  103. else {
  104. if (start.parent.right != start) {
  105. start = start.parent;
  106. rotateRight(start);
  107. }
  108.  
  109. start.parent.color = 1;
  110. start.parent.parent.color = 0;
  111. rotateLeft(start.parent.parent);
  112. }
  113. }
  114. }
  115. root.color = 1;
  116. }
  117.  
  118. private void rotateLeft(Node v) {
  119. Node temp = v.right;
  120. v.right = temp.left;
  121. if (temp.left != nullNode)
  122. temp.left.parent = v;
  123. if (temp != nullNode)
  124. temp.parent = v.parent;
  125. if (v.parent != null)
  126. if (v.parent.left == v)
  127. v.parent.left = temp;
  128. else
  129. v.parent.right = temp;
  130. else
  131. root = temp;
  132. temp.left = v;
  133. if (v != nullNode)
  134. v.parent = temp;
  135. }
  136.  
  137. private void rotateRight(Node v) {
  138. Node temp = v.left;
  139. v.left = temp.right;
  140. if (temp.right != nullNode)
  141. temp.right.parent = v;
  142. if (temp != nullNode)
  143. temp.parent = v.parent;
  144. if (v.parent != null)
  145. if (v.parent.left == v)
  146. v.parent.left = temp;
  147. else
  148. v.parent.right = temp;
  149. else
  150. root = temp;
  151. temp.right = v;
  152. v.parent = temp;
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement