Advertisement
Old_But_Gold

RBTree Java

Oct 22nd, 2023 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.43 KB | None | 0 0
  1. public class MyRbMap implements SortedMap<Integer, String> {
  2.  
  3. Node Root;
  4.  
  5. enum Color {
  6. RED, BLACK
  7. }
  8.  
  9. class Node {
  10. Integer key;
  11. String value;
  12. Node parent, left, right;
  13. Color color;
  14. public Node(Color color, Integer key, String value) {
  15. this.color = color;
  16. this.key = key;
  17. this.value = value;
  18. }
  19. }
  20.  
  21. @Override
  22. public String toString() {
  23. if (Root == null)
  24. return "{}";
  25. StringBuilder sb = new StringBuilder().append("{");
  26. inOrderTraversal(Root, sb);
  27. sb.replace(sb.length() - 2, sb.length(), "");
  28. sb.append("}");
  29. return sb.toString();
  30. }
  31.  
  32. private void inOrderTraversal(Node node, StringBuilder sb) {
  33. if (node != null) {
  34. inOrderTraversal(node.left, sb);
  35. sb.append(node.key + "=" + node.value + ", ");
  36. inOrderTraversal(node.right, sb);
  37. }
  38. }
  39.  
  40. @Override
  41. public int size() {
  42. return size(Root);
  43. }
  44.  
  45. int size(Node node) {
  46. if (node == null) {
  47. return 0;
  48. }
  49. return size(node.left) + size(node.right) + 1;
  50. }
  51.  
  52. @Override
  53. public boolean isEmpty() {
  54. return Root == null;
  55. }
  56.  
  57. @Override
  58. public boolean containsKey(Object key) {
  59. return SearchKey((Integer) key, Root) != null;
  60. }
  61.  
  62. Node SearchKey(Integer key, Node node) {
  63. if (node == null)
  64. return null;
  65. int comparison = key.compareTo(node.key);
  66. if (comparison == 0)
  67. return node;
  68.  
  69. return SearchKey(key, comparison < 0 ? node.left : node.right);
  70. }
  71.  
  72. @Override
  73. public boolean containsValue(Object value) {
  74. return containsValue(Root, value);
  75. }
  76.  
  77. boolean containsValue(Node node, Object value) {
  78. if (node == null) {
  79. return false;
  80. }
  81. if (value.equals(node.value)) {
  82. return true;
  83. }
  84. return containsValue(node.left, value) || containsValue(node.right, value);
  85. }
  86.  
  87. @Override
  88. public String get(Object key) {
  89. Node node = SearchKey((Integer) key, Root);
  90. return (node != null) ? node.value : null;
  91. }
  92.  
  93. @Override
  94. public String put(Integer key, String value) {
  95. if (Root == null) {
  96. Root = new Node(Color.BLACK, key, value);
  97. } else {
  98. Node newNode = new Node(Color.RED, key, value);
  99. Node current = Root;
  100. while (true) {
  101. newNode.parent = current;
  102. if (key.compareTo(current.key) < 0) {
  103. if (current.left == null) {
  104. current.left = newNode;
  105. break;
  106. } else {
  107. current = current.left;
  108. }
  109. } else if (key.compareTo(current.key) > 0) {
  110. if (current.right == null) {
  111. current.right = newNode;
  112. break;
  113. } else {
  114. current = current.right;
  115. }
  116. } else {
  117. String oldValue = current.value;
  118. current.value = value;
  119. return oldValue;
  120. }
  121. }
  122.  
  123. balanceAfterInsert(newNode);
  124. }
  125. return null;
  126. }
  127.  
  128. void balanceAfterInsert(Node node) {
  129. while (node != Root && node.color == Color.RED && node.parent.color == Color.RED) {
  130. Node parent = node.parent;
  131. Node grandparent = parent.parent;
  132.  
  133. if (parent == grandparent.left) {
  134. Node uncle = grandparent.right;
  135. if (uncle != null && uncle.color == Color.RED) {
  136. parent.color = Color.BLACK;
  137. uncle.color = Color.BLACK;
  138. grandparent.color = Color.RED;
  139. node = grandparent;
  140. } else {
  141. if (node == parent.right) {
  142. node = parent;
  143. RotateLeft(node);
  144. }
  145. parent.color = Color.BLACK;
  146. grandparent.color = Color.RED;
  147. RotateRight(grandparent);
  148. }
  149. } else {
  150. Node uncle = grandparent.left;
  151. if (uncle != null && uncle.color == Color.RED) {
  152. parent.color = Color.BLACK;
  153. uncle.color = Color.BLACK;
  154. grandparent.color = Color.RED;
  155. node = grandparent;
  156. } else {
  157. if (node == parent.left) {
  158. node = parent;
  159. RotateRight(node);
  160. }
  161. parent.color = Color.BLACK;
  162. grandparent.color = Color.RED;
  163. RotateLeft(grandparent);
  164. }
  165. }
  166. }
  167.  
  168. Root.color = Color.BLACK;
  169. }
  170.  
  171. Node getSuccessor(Node node) {
  172. Node successor = null;
  173. Node current = node.right;
  174.  
  175. while (current != null) {
  176. successor = current;
  177. current = current.left;
  178. }
  179.  
  180. return successor;
  181. }
  182.  
  183.  
  184. void deleteNode(Node node) {
  185. Node replacement;
  186. if (node.left != null && node.right != null) {
  187. Node successor = getSuccessor(node);
  188. node.key = successor.key;
  189. node.value = successor.value;
  190. node = successor;
  191. }
  192.  
  193. replacement = (node.left != null) ? node.left : node.right;
  194.  
  195. if (replacement != null) {
  196. replacement.parent = node.parent;
  197. if (node.parent == null) {
  198. Root = replacement;
  199. } else if (node == node.parent.left) {
  200. node.parent.left = replacement;
  201. } else {
  202. node.parent.right = replacement;
  203. }
  204.  
  205. if (node.color == Color.BLACK) {
  206. balanceDeletion(replacement);
  207. }
  208. } else if (node.parent == null) {
  209. Root = null;
  210. } else {
  211. if (node.color == Color.BLACK) {
  212. balanceDeletion(node);
  213. }
  214.  
  215. if (node.parent != null) {
  216. if (node == node.parent.left) {
  217. node.parent.left = null;
  218. } else if (node == node.parent.right) {
  219. node.parent.right = null;
  220. }
  221. node.parent = null;
  222. }
  223. }
  224. }
  225.  
  226.  
  227. Color getColor(Node node) {
  228. return (node == null) ? Color.BLACK : node.color;
  229. }
  230.  
  231. void setColor(Node node, Color color) {
  232. if (node != null) {
  233. node.color = color;
  234. }
  235. }
  236.  
  237. void RotateLeft(Node node)
  238. {
  239. Node right = node.right;
  240. node.right = right.left;
  241. if (right.left != null)
  242. right.left.parent = node;
  243. right.parent = node.parent;
  244. if (node.parent == null)
  245. Root = right;
  246. else if (node == node.parent.left)
  247. node.parent.left = right;
  248. else
  249. node.parent.right = right;
  250. right.left = node;
  251. node.parent = right;
  252. }
  253.  
  254. void RotateRight(Node node)
  255. {
  256. Node left = node.left;
  257. node.left = left.right;
  258. if (left.right != null)
  259. left.right.parent = node;
  260. left.parent = node.parent;
  261. if (node.parent == null)
  262. Root = left;
  263. else if (node == node.parent.right)
  264. node.parent.right = left;
  265. else
  266. node.parent.left = left;
  267. left.right = node;
  268. node.parent = left;
  269. }
  270.  
  271. void balanceDeletion(Node node) {
  272. while (node != Root && getColor(node) == Color.BLACK) {
  273. if (node == node.parent.left) {
  274. Node sibling = node.parent.right;
  275. if (getColor(sibling) == Color.RED) {
  276. setColor(sibling, Color.BLACK);
  277. setColor(node.parent, Color.RED);
  278. RotateLeft(node.parent);
  279. sibling = node.parent.right;
  280. }
  281. if (getColor(sibling.left) == Color.BLACK && getColor(sibling.right) == Color.BLACK) {
  282. setColor(sibling, Color.RED);
  283. node = node.parent;
  284. } else {
  285. if (getColor(sibling.right) == Color.BLACK) {
  286. setColor(sibling.left, Color.BLACK);
  287. setColor(sibling, Color.RED);
  288. RotateRight(sibling);
  289. sibling = node.parent.right;
  290. }
  291. setColor(sibling, getColor(node.parent));
  292. setColor(node.parent, Color.BLACK);
  293. setColor(sibling.right, Color.BLACK);
  294. RotateLeft(node.parent);
  295. node = Root;
  296. }
  297. } else {
  298. Node sibling = node.parent.left;
  299. if (getColor(sibling) == Color.RED) {
  300. setColor(sibling, Color.BLACK);
  301. setColor(node.parent, Color.RED);
  302. RotateRight(node.parent);
  303. sibling = node.parent.left;
  304. }
  305. if (getColor(sibling.right) == Color.BLACK && getColor(sibling.left) == Color.BLACK) {
  306. setColor(sibling, Color.RED);
  307. node = node.parent;
  308. } else {
  309. if (getColor(sibling.left) == Color.BLACK) {
  310. setColor(sibling.right, Color.BLACK);
  311. setColor(sibling, Color.RED);
  312. RotateLeft(sibling);
  313. sibling = node.parent.left;
  314. }
  315. setColor(sibling, getColor(node.parent));
  316. setColor(node.parent, Color.BLACK);
  317. setColor(sibling.left, Color.BLACK);
  318. RotateRight(node.parent);
  319. node = Root;
  320. }
  321. }
  322. }
  323.  
  324. setColor(node, Color.BLACK);
  325. }
  326.  
  327. @Override
  328. public String remove(Object key) {
  329. Node node = SearchKey((Integer) key, Root);
  330. if (node != null) {
  331. String oldValue = node.value;
  332. deleteNode(node);
  333. return oldValue;
  334. }
  335. return null;
  336. }
  337.  
  338. @Override
  339. public void clear() {
  340. Root = clear(Root);
  341. }
  342.  
  343. Node clear(Node node) {
  344. if (node == null)
  345. return null;
  346. node.left = clear(node.left);
  347. node.right = clear(node.right);
  348. return null;
  349. }
  350.  
  351. @Override
  352. public Integer firstKey() {
  353. Node first = firstNode(Root);
  354. return (first != null) ? first.key : null;
  355. }
  356.  
  357. Node firstNode(Node node) {
  358. while (node != null && node.left != null) {
  359. node = node.left;
  360. }
  361. return node;
  362. }
  363.  
  364. @Override
  365. public Integer lastKey() {
  366. Node last = lastNode(Root);
  367. return (last != null) ? last.key : null;
  368. }
  369.  
  370. Node lastNode(Node node) {
  371. while (node != null && node.right != null) {
  372. node = node.right;
  373. }
  374. return node;
  375. }
  376.  
  377. @Override
  378. public SortedMap<Integer, String> headMap(Integer toKey) {
  379. MyRbMap subMap = new MyRbMap();
  380. headMap(Root, toKey, subMap);
  381. return subMap;
  382. }
  383.  
  384. void headMap(Node node, Integer toKey, MyRbMap subMap) {
  385. if (node == null) {
  386. return;
  387. }
  388.  
  389. if (node.key.compareTo(toKey) < 0) {
  390. subMap.put(node.key, node.value);
  391. headMap(node.right, toKey, subMap);
  392. }
  393.  
  394. headMap(node.left, toKey, subMap);
  395. }
  396.  
  397.  
  398. @Override
  399. public SortedMap<Integer, String> tailMap(Integer fromKey) {
  400. MyRbMap subMap = new MyRbMap();
  401. tailMap(Root, fromKey, subMap);
  402. return subMap;
  403. }
  404.  
  405. void tailMap(Node node, Integer fromKey, MyRbMap subMap) {
  406. if (node == null) {
  407. return;
  408. }
  409.  
  410. if (node.key.compareTo(fromKey) >= 0) {
  411. subMap.put(node.key, node.value);
  412. tailMap(node.left, fromKey, subMap);
  413. }
  414.  
  415. tailMap(node.right, fromKey, subMap);
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement