Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. package classes;
  2.  
  3. import java.sql.SQLOutput;
  4.  
  5. public class Map {
  6.  
  7. class Node {
  8.  
  9. private Node parent;
  10. private Node leftChild;
  11. private Node rightChild;
  12. private int value;
  13. private int key;
  14. private boolean color; //red = true, black = false
  15.  
  16. Node(int newKey, int newValue) {
  17. value = newValue;
  18. key = newKey;
  19. parent = null;
  20. leftChild = null;
  21. rightChild = null;
  22. color = true;
  23. }
  24.  
  25. Node() {
  26. parent = null;
  27. leftChild = null;
  28. rightChild = null;
  29. color = true;
  30. }
  31. public String toString() {
  32. String colorName = color ? "RED" : "BLACK";
  33. String msg = "key: " + key + ", value: " + value + ", color: " + colorName;
  34. if (this.parent != null)
  35. msg += ", parent's key: " + parent.key;
  36. return msg;
  37. }
  38.  
  39. }
  40.  
  41. private Node root = null;
  42.  
  43.  
  44. public void setValue(int key, int value) {
  45.  
  46. Node newNode = new Node(key, value);
  47.  
  48. if (root == null) {
  49. System.out.println("Zrobilo roota " + newNode.value);
  50. root = newNode;
  51. root.color = false;
  52. return;
  53. }
  54.  
  55. Node actualNode = root;
  56. boolean running = true;
  57.  
  58. while (running) {
  59.  
  60. if (actualNode.key < key && actualNode.rightChild != null) { //prawa droga
  61. actualNode = actualNode.rightChild;
  62. } else if (actualNode.key < key) {
  63. newNode.parent = actualNode;
  64. actualNode.rightChild = newNode;
  65. actualNode = actualNode.rightChild;
  66. running = false;
  67. }
  68.  
  69. if (actualNode.key > key && actualNode.leftChild != null) { //lewa droga
  70. actualNode = actualNode.leftChild;
  71. } else if (actualNode.key > key) {
  72. newNode.parent = actualNode;
  73. actualNode.leftChild = newNode;
  74. actualNode = actualNode.leftChild;
  75. running = false;
  76. }
  77. }
  78. restore(actualNode);
  79. }
  80.  
  81. private void restore(Node actualNode) {
  82.  
  83. System.out.println("Weszlo do restora " + actualNode.value);
  84.  
  85. while (actualNode.parent != root && actualNode.parent != null) {
  86.  
  87. System.out.println("Weszlo do while " + actualNode.value);
  88.  
  89. switch (isLeftChild(actualNode.parent)) {
  90.  
  91. case 1:
  92. System.out.println("Case ojciec lewe dziecko " + actualNode.value);
  93. if (brotherColor(actualNode.parent)) {
  94. System.out.println("Brat czerwony " + actualNode.value);
  95. actualNode.parent.color = false;
  96. actualNode.parent.parent.rightChild.color = false;
  97. actualNode = actualNode.parent.parent;
  98. actualNode.color = true;
  99. break;
  100. }
  101. if (isLeftChild(actualNode) == 0) {
  102. System.out.println("Prawe dziecko, brat czarny" + actualNode.value);
  103. actualNode = actualNode.parent;
  104. leftRotate(actualNode);
  105. }
  106. System.out.println("Lewe dziecko brat czarny " + actualNode.value);
  107. actualNode.parent.color = false;
  108. actualNode.parent.parent.color = true;
  109. rightRotate(actualNode.parent.parent);
  110. break;
  111. case 0:
  112. System.out.println("Case ojciec prawe dziecko"+ actualNode.value);
  113. if (brotherColor(actualNode.parent)) {
  114. System.out.println("Brat czerwony "+ actualNode.value);
  115. actualNode.parent.color = false;
  116. actualNode.parent.parent.leftChild.color = false;
  117. actualNode = actualNode.parent.parent;
  118. actualNode.color = true;
  119. break;
  120. }
  121. if (isLeftChild(actualNode) == 0) {
  122. System.out.println("Prawe dziecko, brat czarny" + actualNode.value);
  123. actualNode = actualNode.parent;
  124. rightRotate(actualNode);
  125. }
  126. System.out.println("Lewe dziecko brat czarny " + actualNode.value);
  127. actualNode.parent.color = false;
  128. actualNode.parent.parent.color = true;
  129. leftRotate(actualNode.parent.parent);
  130. break;
  131. }
  132.  
  133.  
  134. }
  135. root.color = false;
  136. }
  137.  
  138. private int isLeftChild(Node node) {
  139. if (node.parent.leftChild != null) {
  140. if (node.parent.leftChild.key == node.key)
  141. return 1; //jest lewym synem - 1, jest prawym 0
  142. else return 0;
  143. } else return 0;
  144. }
  145.  
  146. private boolean brotherColor(Node node) {
  147. if (node.parent.leftChild == node) {
  148. if (node.parent.rightChild == null) return false;
  149. else return node.parent.rightChild.color;
  150. } else if (node.parent.leftChild == null) return false;
  151. else return node.parent.leftChild.color;
  152. }
  153.  
  154.  
  155. private void rightRotate(Node node) {
  156. Node inputNode = node;
  157. Node P = node.leftChild;
  158.  
  159. if (P.rightChild != null) {
  160. node.leftChild = P.rightChild;
  161. node.leftChild.parent = node;
  162. }else node.leftChild = null;
  163.  
  164. P.parent = node.parent;
  165. node.parent = P;
  166. P.rightChild = node;
  167.  
  168. if (inputNode == root) {
  169. root = P;
  170. } else {
  171. if (isLeftChild(inputNode) == 1)
  172. inputNode.parent.leftChild = P;
  173. else inputNode.parent.rightChild = P;
  174. }
  175.  
  176. }
  177.  
  178. private void leftRotate(Node node) {
  179. Node inputNode = node;
  180. Node Q = node.rightChild;
  181.  
  182. if (Q.leftChild != null) {
  183. node.rightChild = Q.leftChild;
  184. node.rightChild.parent = node;
  185. } else node.rightChild = null;
  186.  
  187. Q.parent = node.parent;
  188. node.parent = Q;
  189. Q.leftChild = node;
  190. if (inputNode == root) {
  191. root = Q;
  192. } else {
  193. if (isLeftChild(node) == 1)
  194. inputNode.parent.leftChild = Q;
  195. else
  196. inputNode.parent.rightChild = Q;
  197. }
  198. }
  199.  
  200. public void write() {
  201. writeRec(root);
  202. }
  203.  
  204. private void writeRec(Node cur) {
  205. if(cur != null) {
  206. writeRec(cur.leftChild);
  207. System.out.println(cur);
  208. writeRec(cur.rightChild);
  209. }
  210.  
  211. }
  212.  
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement