|
| 1 | +package ashiqur_java_util.tree.splaytree; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | + |
| 10 | +class BinaryTree<T extends Comparable<T>> { |
| 11 | + private BinaryTree<T> left, right; |
| 12 | + private T data; |
| 13 | + |
| 14 | + public BinaryTree(T data) { |
| 15 | + this.data = data; |
| 16 | + } |
| 17 | + |
| 18 | + public BinaryTree<T> getLeft() { |
| 19 | + return left; |
| 20 | + } |
| 21 | + |
| 22 | + public void setLeft(BinaryTree<T> left) { |
| 23 | + this.left = left; |
| 24 | + // left.parent = this; |
| 25 | + } |
| 26 | + |
| 27 | + public BinaryTree<T> getRight() { |
| 28 | + return right; |
| 29 | + } |
| 30 | + |
| 31 | + public void setRight(BinaryTree<T> right) { |
| 32 | + this.right = right; |
| 33 | + // right.parent = this; |
| 34 | + } |
| 35 | + |
| 36 | + public T getData() { |
| 37 | + return data; |
| 38 | + } |
| 39 | + |
| 40 | + public void setData(T data) { |
| 41 | + this.data = data; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String toString() { |
| 46 | + BTreePrinter.printNode(this); |
| 47 | + return ""; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +class BTreePrinter { |
| 54 | + |
| 55 | + public static <T extends Comparable<T>> void printNode(BinaryTree<T> root) { |
| 56 | + int maxLevel = BTreePrinter.maxLevel(root); |
| 57 | + |
| 58 | + printNodeInternal(Collections.singletonList(root), 1, maxLevel); |
| 59 | + } |
| 60 | + |
| 61 | + private static <T extends Comparable<T>> void printNodeInternal(List<BinaryTree<T>> binaryTrees, int level, int maxLevel) { |
| 62 | + if (binaryTrees.isEmpty() || BTreePrinter.isAllElementsNull(binaryTrees)) |
| 63 | + return; |
| 64 | + |
| 65 | + int floor = maxLevel - level; |
| 66 | + int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0))); |
| 67 | + int firstSpaces = (int) Math.pow(2, (floor)) - 1; |
| 68 | + int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1; |
| 69 | + |
| 70 | + BTreePrinter.printWhitespaces(firstSpaces); |
| 71 | + |
| 72 | + List<BinaryTree<T>> newBinaryTrees = new ArrayList<BinaryTree<T>>(); |
| 73 | + for (BinaryTree<T> binaryTree : binaryTrees) { |
| 74 | + if (binaryTree != null) { |
| 75 | + System.out.print(binaryTree.getData()); |
| 76 | + newBinaryTrees.add(binaryTree.getLeft()); |
| 77 | + newBinaryTrees.add(binaryTree.getRight()); |
| 78 | + } else { |
| 79 | + newBinaryTrees.add(null); |
| 80 | + newBinaryTrees.add(null); |
| 81 | + System.out.print(" "); |
| 82 | + } |
| 83 | + |
| 84 | + BTreePrinter.printWhitespaces(betweenSpaces); |
| 85 | + } |
| 86 | + System.out.println(""); |
| 87 | + |
| 88 | + for (int i = 1; i <= endgeLines; i++) { |
| 89 | + for (int j = 0; j < binaryTrees.size(); j++) { |
| 90 | + BTreePrinter.printWhitespaces(firstSpaces - i); |
| 91 | + if (binaryTrees.get(j) == null) { |
| 92 | + BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1); |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (binaryTrees.get(j).getLeft() != null) |
| 97 | + System.out.print("/"); |
| 98 | + else |
| 99 | + BTreePrinter.printWhitespaces(1); |
| 100 | + |
| 101 | + BTreePrinter.printWhitespaces(i + i - 1); |
| 102 | + |
| 103 | + if (binaryTrees.get(j).getRight() != null) |
| 104 | + System.out.print("\\"); |
| 105 | + else |
| 106 | + BTreePrinter.printWhitespaces(1); |
| 107 | + |
| 108 | + BTreePrinter.printWhitespaces(endgeLines + endgeLines - i); |
| 109 | + } |
| 110 | + |
| 111 | + System.out.println(); |
| 112 | + } |
| 113 | + |
| 114 | + printNodeInternal(newBinaryTrees, level + 1, maxLevel); |
| 115 | + } |
| 116 | + |
| 117 | + private static void printWhitespaces(int count) { |
| 118 | + for (int i = 0; i < count; i++) |
| 119 | + System.out.print(" "); |
| 120 | + } |
| 121 | + |
| 122 | + private static <T extends Comparable<T>> int maxLevel(BinaryTree<T> binaryTree) { |
| 123 | + if (binaryTree == null) |
| 124 | + return 0; |
| 125 | + |
| 126 | + return Math.max(BTreePrinter.maxLevel(binaryTree.getLeft()), BTreePrinter.maxLevel(binaryTree.getRight())) + 1; |
| 127 | + } |
| 128 | + |
| 129 | + private static <T> boolean isAllElementsNull(List<T> list) { |
| 130 | + for (Object object : list) { |
| 131 | + if (object != null) |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +/**Note : Each Operation of a SplayTree tries returns the new splayed root after the operation **/ |
| 141 | +public class SplayTree<T extends Comparable<T>> { |
| 142 | + |
| 143 | + private BinaryTree<T> root; |
| 144 | + |
| 145 | + public SplayTree() { |
| 146 | + this.root = null; |
| 147 | + } |
| 148 | + |
| 149 | + public SplayTree(BinaryTree<T> root) { |
| 150 | + this.root = root; |
| 151 | + } |
| 152 | + |
| 153 | + public BinaryTree<T> getRoot() { |
| 154 | + return root; |
| 155 | + } |
| 156 | + |
| 157 | + private BinaryTree<T> rightRotate(BinaryTree<T> x) { |
| 158 | + BinaryTree<T> p = x.getLeft(); |
| 159 | + x.setLeft(p.getRight()); |
| 160 | + p.setRight(x); |
| 161 | + return p; |
| 162 | + } |
| 163 | + |
| 164 | + private BinaryTree<T> leftRotate(BinaryTree<T> x) { |
| 165 | + BinaryTree<T> p = x.getRight(); |
| 166 | + x.setRight(p.getLeft()); |
| 167 | + p.setLeft(x); |
| 168 | + return p; |
| 169 | + } |
| 170 | + |
| 171 | + private BinaryTree<T> splayUtil(BinaryTree<T> root, T key) { |
| 172 | + if (root == null || root.getData() == key) |
| 173 | + return root; |
| 174 | + |
| 175 | + if (root.getData().compareTo(key) > 0) { |
| 176 | + if (root.getLeft() == null) return root; |
| 177 | + |
| 178 | + if (root.getLeft().getData().compareTo(key) > 0) { |
| 179 | + |
| 180 | + root.getLeft().setLeft(splayUtil(root.getLeft().getLeft(), key)); |
| 181 | + |
| 182 | + root = rightRotate(root); |
| 183 | + } |
| 184 | + else if (root.getLeft().getData().compareTo(key) < 0) { |
| 185 | + |
| 186 | + root.getLeft().setRight(splayUtil(root.getLeft().getRight(), key)); |
| 187 | + |
| 188 | + if (root.getLeft().getRight() != null) |
| 189 | + root.setLeft(leftRotate(root.getLeft())); |
| 190 | + |
| 191 | + } |
| 192 | + return (root.getLeft() == null) ? root : rightRotate(root); |
| 193 | + } else { |
| 194 | + if (root.getRight() == null) return root; |
| 195 | + |
| 196 | + if (root.getRight().getData().compareTo(key) > 0) { |
| 197 | + root.getRight().setLeft(splayUtil(root.getRight().getLeft(), key)); |
| 198 | + if (root.getRight().getLeft() != null) |
| 199 | + root.setRight(rightRotate(root.getRight())); |
| 200 | + } else if (root.getRight().getData().compareTo(key) < 0)// Zag-Zag (Right Right) |
| 201 | + { |
| 202 | + root.getRight().setRight(splayUtil(root.getRight().getRight(), key)); |
| 203 | + root = leftRotate(root); |
| 204 | + } |
| 205 | + |
| 206 | + return (root.getRight() == null) ? root : leftRotate(root); |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + public BinaryTree<T> splay(T x) { |
| 212 | + if (root == null) return null; |
| 213 | + |
| 214 | + this.root = splayUtil(root, x); |
| 215 | + |
| 216 | + return this.root; |
| 217 | + } |
| 218 | + public BinaryTree<T> search(T x) { |
| 219 | + if (root == null) return null; |
| 220 | + |
| 221 | + this.root = splayUtil(root, x); |
| 222 | + |
| 223 | + return this.root.getData().compareTo(x)==0 ? this.root:null; |
| 224 | + } |
| 225 | + |
| 226 | + private ArrayList<BinaryTree<T>> split(T x) { |
| 227 | + BinaryTree<T> right; |
| 228 | + BinaryTree<T> left; |
| 229 | + |
| 230 | + if (x.compareTo(root.getData()) > 0) { |
| 231 | + right = root.getRight(); |
| 232 | + left = root; |
| 233 | + left.setRight(null); |
| 234 | + } else { |
| 235 | + left = root.getLeft(); |
| 236 | + right = root; |
| 237 | + right.setLeft(null); |
| 238 | + } |
| 239 | + ArrayList<BinaryTree<T>> l_r = new ArrayList<>(); |
| 240 | + l_r.add(left); |
| 241 | + l_r.add(right); |
| 242 | + |
| 243 | + return l_r; |
| 244 | + } |
| 245 | + |
| 246 | + public BinaryTree<T> insert(T x) { |
| 247 | + if (root == null) { |
| 248 | + root = new BinaryTree<>(x); |
| 249 | + return root; |
| 250 | + } |
| 251 | + splay(x); |
| 252 | + |
| 253 | + ArrayList<BinaryTree<T>> l_r = split(x); |
| 254 | + |
| 255 | + BinaryTree<T> left = l_r.get(0); |
| 256 | + BinaryTree<T> right = l_r.get(1); |
| 257 | + |
| 258 | + root = new BinaryTree<>(x); |
| 259 | + root.setLeft(left); |
| 260 | + root.setRight(right); |
| 261 | + |
| 262 | + |
| 263 | + return root; |
| 264 | + } |
| 265 | + |
| 266 | + public BinaryTree<T> delete(T x) { |
| 267 | + if (root == null) return null; |
| 268 | + |
| 269 | + BinaryTree<T> searchResult = splay(x); |
| 270 | + |
| 271 | + if (searchResult.getData().compareTo(x) != 0) |
| 272 | + return null; |
| 273 | + |
| 274 | + BinaryTree<T> leftSubtree = root.getLeft(); |
| 275 | + BinaryTree<T> rightSubtree = root.getRight(); |
| 276 | + |
| 277 | + // Set the 'to be deleted' key ready for garbage collection |
| 278 | + root.setLeft(null); |
| 279 | + root.setRight(null); |
| 280 | + root.setData(null); |
| 281 | + |
| 282 | + root = join(leftSubtree, rightSubtree); |
| 283 | + |
| 284 | + return root; |
| 285 | + } |
| 286 | + |
| 287 | + private BinaryTree<T> join(BinaryTree<T> L, BinaryTree<T> R) { |
| 288 | + |
| 289 | + if (L == null) { |
| 290 | + root = R; |
| 291 | + return R; |
| 292 | + } |
| 293 | + |
| 294 | + |
| 295 | + root = splayUtil(L, findMax(L)); |
| 296 | + |
| 297 | + root.setRight(R); |
| 298 | + |
| 299 | + return root; |
| 300 | + } |
| 301 | + public T findMax(BinaryTree<T> root){ |
| 302 | + BinaryTree<T> temp = root; |
| 303 | + while (temp.getRight()!=null) |
| 304 | + temp=temp.getRight(); |
| 305 | + return temp.getData(); |
| 306 | + } |
| 307 | + |
| 308 | + @Override |
| 309 | + public String toString() { |
| 310 | + |
| 311 | + System.out.println("Elements:"+inorder(root, new ArrayList<>())); |
| 312 | + return (root != null) ? root.toString() : null; |
| 313 | + } |
| 314 | + |
| 315 | + public ArrayList<T> inorder(BinaryTree<T> root, ArrayList<T> sorted) { |
| 316 | + |
| 317 | + if (root == null) { |
| 318 | + return sorted; |
| 319 | + } |
| 320 | + inorder(root.getLeft(), sorted); |
| 321 | + sorted.add(root.getData()); |
| 322 | + inorder(root.getRight(), sorted); |
| 323 | + return sorted; |
| 324 | + } |
| 325 | +} |
| 326 | + |
| 327 | +class SplayTreeRun { |
| 328 | + public static void main(String[] args) { |
| 329 | + |
| 330 | + SplayTree<Integer> splayTree = new SplayTree<>(); |
| 331 | + int[] data = {2, 29, 26,-1,10,0,2,11}; |
| 332 | + Scanner sc = new Scanner(System.in); |
| 333 | + for (int i : |
| 334 | + data) { |
| 335 | + splayTree.insert(i); |
| 336 | + } |
| 337 | + |
| 338 | + while (true) { |
| 339 | + System.out.println("1. Insert 2. Delete 3. Search 4. PrintTree"); |
| 340 | + int c = sc.nextInt(); |
| 341 | + switch (c) { |
| 342 | + case 1: |
| 343 | + System.out.println("Enter Data :"); |
| 344 | + splayTree.insert(sc.nextInt()); |
| 345 | + break; |
| 346 | + case 2: |
| 347 | + System.out.println("Enter Element to be Deleted:"); |
| 348 | + splayTree.delete(sc.nextInt()); |
| 349 | + break; |
| 350 | + case 3: |
| 351 | + System.out.println("Enter Element to be Searched and Splayed:"); |
| 352 | + System.out.println("Found :"+splayTree.search(sc.nextInt())); |
| 353 | + break; |
| 354 | + case 4: |
| 355 | + System.out.println(splayTree); |
| 356 | + break; |
| 357 | + } |
| 358 | + |
| 359 | + |
| 360 | + } |
| 361 | + |
| 362 | + } |
| 363 | +} |
| 364 | + |
0 commit comments