Advertisement
Old_But_Gold

SplayTree Java

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