1 4 package com.tc.util; 5 6 import com.tc.exception.ImplementMe; 7 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.NoSuchElementException ; 12 13 22 public class AATree { 23 24 private AANode root; 25 private AANode deletedNode; 26 private AANode lastNode; 27 private Comparable deletedElement; 28 private boolean inserted; 29 30 private static final AANode nullNode; 31 32 static { 34 nullNode = new AANode(null); 35 nullNode.left = nullNode.right = nullNode; 36 nullNode.level = 0; 37 } 38 39 42 public AATree() { 43 root = nullNode; 44 } 45 46 53 public boolean insert(Comparable x) { 54 inserted = true; 55 root = insert(x, root); 56 return inserted; 57 } 58 59 65 public Comparable remove(Comparable x) { 66 deletedNode = nullNode; 67 root = remove(x, root); 68 Comparable d = deletedElement; 69 deletedElement = null; 72 return d; 73 } 74 75 80 public Comparable findMin() { 81 if (isEmpty()) return null; 82 83 AANode ptr = root; 84 85 while (ptr.left != nullNode) 86 ptr = ptr.left; 87 88 return ptr.element; 89 } 90 91 96 public Comparable findMax() { 97 if (isEmpty()) return null; 98 99 AANode ptr = root; 100 101 while (ptr.right != nullNode) 102 ptr = ptr.right; 103 104 return ptr.element; 105 } 106 107 113 114 public Comparable find(Comparable x) { 115 AANode current = root; 116 117 while (current != nullNode) { 118 int res = x.compareTo(current.element); 119 if (res < 0) current = current.left; 120 else if (res > 0) current = current.right; 121 else return current.element; 122 } 123 return null; 124 } 125 126 129 public void clear() { 130 root = nullNode; 131 } 132 133 138 public boolean isEmpty() { 139 return root == nullNode; 140 } 141 142 public Iterator iterator() { 143 return new AATreeIterator(); 144 } 145 146 154 private AANode insert(Comparable x, AANode t) { 155 if (t == nullNode) { 156 t = new AANode(x); 157 } else if (x.compareTo(t.element) < 0) { 158 t.left = insert(x, t.left); 159 } else if (x.compareTo(t.element) > 0) { 160 t.right = insert(x, t.right); 161 } else { 162 inserted = false; 165 return t; 166 } 167 168 t = skew(t); 169 t = split(t); 170 return t; 171 } 172 173 181 private AANode remove(Comparable x, AANode t) { 182 if (t != nullNode) { 183 lastNode = t; 185 if (x.compareTo(t.element) < 0) { 186 t.left = remove(x, t.left); 187 } else { 188 deletedNode = t; 189 t.right = remove(x, t.right); 190 } 191 192 if (t == lastNode) { 195 if (deletedNode == nullNode || x.compareTo(deletedNode.element) != 0) { 196 } else { 200 deletedElement = deletedNode.element; 201 deletedNode.element = t.element; 202 t = t.right; 203 } 204 } 205 206 else if (t.left.level < t.level - 1 || t.right.level < t.level - 1) { 208 if (t.right.level > --t.level) t.right.level = t.level; 209 t = skew(t); 210 t.right = skew(t.right); 211 t.right.right = skew(t.right.right); 212 t = split(t); 213 t.right = split(t.right); 214 } 215 } 216 return t; 217 } 218 219 225 private static AANode skew(AANode t) { 226 if (t.left.level == t.level) t = rotateWithLeftChild(t); 227 return t; 228 } 229 230 236 private static AANode split(AANode t) { 237 if (t.right.right.level == t.level) { 238 t = rotateWithRightChild(t); 239 t.level++; 240 } 241 return t; 242 } 243 244 247 private static AANode rotateWithLeftChild(AANode k2) { 248 AANode k1 = k2.left; 249 k2.left = k1.right; 250 k1.right = k2; 251 return k1; 252 } 253 254 257 private static AANode rotateWithRightChild(AANode k1) { 258 AANode k2 = k1.right; 259 k1.right = k2.left; 260 k2.left = k1; 261 return k2; 262 } 263 264 public String dump() { 265 return "AATree = { " + root.dump() + " }"; 266 } 267 268 private static class AANode { 269 AANode(Comparable theElement) { 271 element = theElement; 272 left = right = nullNode; 273 level = 1; 274 } 275 276 public String dump() { 278 String ds = String.valueOf(element); 279 if (left != nullNode) { 280 ds = ds + "," + left.dump(); 281 } 282 if (right != nullNode) { 283 ds = ds + "," + right.dump(); 284 } 285 return ds; 286 } 287 288 Comparable element; AANode left; AANode right; int level; 293 public String toString() { 294 return "AANode@" + System.identityHashCode(this) + "{" + element + "}"; 295 } 296 } 297 298 302 private class AATreeIterator implements Iterator { 303 304 List s = new ArrayList (); 306 AANode next = root; 307 308 public boolean hasNext() { 309 return (next != nullNode); 310 } 311 312 public Object next() { 313 if (next == nullNode) { throw new NoSuchElementException (); } 314 Object element = next.element; 315 boolean leftNotNull = next.left != nullNode; 316 boolean rightNotNull = next.right != nullNode; 317 if (leftNotNull && rightNotNull) { 318 s.add(next.right); 319 next = next.left; 320 } else if (leftNotNull) { 321 next = next.left; 322 } else if (rightNotNull) { 323 next = next.right; 324 } else if (s.size() > 0) { 325 next = ((AANode) s.remove(s.size() - 1)); 326 } else { 327 next = nullNode; 328 } 329 return element; 330 } 331 332 public void remove() { 334 throw new ImplementMe(); 335 } 336 337 } 338 339 369 public static void main(String [] args) { 370 AATree t = new AATree(); 371 System.out.println("Inserted = " + t.insert(new Integer (8))); 372 System.out.println("Tree is : " + t.dump()); 373 System.out.println("From Iterator : " + dumpUsingIterator(t)); 374 System.out.println("Inserted = " + t.insert(new Integer (4))); 375 System.out.println("Tree is : " + t.dump()); 376 System.out.println("From Iterator : " + dumpUsingIterator(t)); 377 System.out.println("Inserted = " + t.insert(new Integer (10))); 378 System.out.println("Tree is : " + t.dump()); 379 System.out.println("From Iterator : " + dumpUsingIterator(t)); 380 System.out.println("Inserted = " + t.insert(new Integer (2))); 381 System.out.println("Tree is : " + t.dump()); 382 System.out.println("From Iterator : " + dumpUsingIterator(t)); 383 System.out.println("Inserted = " + t.insert(new Integer (6))); 384 System.out.println("Tree is : " + t.dump()); 385 System.out.println("From Iterator : " + dumpUsingIterator(t)); 386 System.out.println("Inserted = " + t.insert(new Integer (9))); 387 System.out.println("Tree is : " + t.dump()); 388 System.out.println("From Iterator : " + dumpUsingIterator(t)); 389 System.out.println("Inserted = " + t.insert(new Integer (11))); 390 System.out.println("Tree is : " + t.dump()); 391 System.out.println("From Iterator : " + dumpUsingIterator(t)); 392 System.out.println("Inserted = " + t.insert(new Integer (1))); 393 System.out.println("Tree is : " + t.dump()); 394 System.out.println("From Iterator : " + dumpUsingIterator(t)); 395 System.out.println("Inserted = " + t.insert(new Integer (3))); 396 System.out.println("Tree is : " + t.dump()); 397 System.out.println("From Iterator : " + dumpUsingIterator(t)); 398 System.out.println("Inserted = " + t.insert(new Integer (5))); 399 System.out.println("Tree is : " + t.dump()); 400 System.out.println("From Iterator : " + dumpUsingIterator(t)); 401 System.out.println("Inserted = " + t.insert(new Integer (7))); 402 System.out.println("Tree is : " + t.dump()); 403 System.out.println("From Iterator : " + dumpUsingIterator(t)); 404 System.out.println("Inserted = " + t.insert(new Integer (12))); 405 System.out.println("Tree is : " + t.dump()); 406 System.out.println("From Iterator : " + dumpUsingIterator(t)); 407 System.out.println("Inserted = " + t.insert(new Integer (1))); 408 System.out.println("Tree is : " + t.dump()); 409 System.out.println("From Iterator : " + dumpUsingIterator(t)); 410 System.out.println("Inserted = " + t.insert(new Integer (3))); 411 System.out.println("Tree is : " + t.dump()); 412 System.out.println("From Iterator : " + dumpUsingIterator(t)); 413 414 System.out.println("Deleted = " + t.remove(new Integer (6))); 415 System.out.println("Tree is : " + t.dump()); 416 System.out.println("From Iterator : " + dumpUsingIterator(t)); 417 System.out.println("Deleted = " + t.remove(new Integer (8))); 418 System.out.println("Tree is : " + t.dump()); 419 System.out.println("From Iterator : " + dumpUsingIterator(t)); 420 System.out.println("Deleted = " + t.remove(new Integer (10))); 421 System.out.println("Tree is : " + t.dump()); 422 System.out.println("From Iterator : " + dumpUsingIterator(t)); 423 System.out.println("Deleted = " + t.remove(new Integer (12))); 424 System.out.println("Tree is : " + t.dump()); 425 System.out.println("From Iterator : " + dumpUsingIterator(t)); 426 System.out.println("Deleted = " + t.remove(new Integer (6))); 427 System.out.println("Tree is : " + t.dump()); 428 System.out.println("From Iterator : " + dumpUsingIterator(t)); 429 System.out.println("Deleted = " + t.remove(new Integer (8))); 430 System.out.println("Tree is : " + t.dump()); 431 System.out.println("From Iterator : " + dumpUsingIterator(t)); 432 System.out.println("Deleted = " + t.remove(new Integer (1))); 433 System.out.println("Tree is : " + t.dump()); 434 System.out.println("From Iterator : " + dumpUsingIterator(t)); 435 436 } 437 438 private static String dumpUsingIterator(AATree t) { 439 StringBuffer sb = new StringBuffer (); 440 for (Iterator i = t.iterator(); i.hasNext();) { 441 sb.append(i.next()); 442 if (i.hasNext()) { 443 sb.append(','); 444 } 445 } 446 return sb.toString(); 447 } 448 } | Popular Tags |