| 1 2 17 18 19 package org.apache.poi.util; 20 21 import java.util.*; 22 23 91 public final class BinaryTree 93 extends AbstractMap 94 { 95 private Node[] _root = new Node[] 96 { 97 null, null 98 }; 99 private int _size = 0; 100 private int _modifications = 0; 101 private Set[] _key_set = new Set[] 102 { 103 null, null 104 }; 105 private Set[] _entry_set = new Set[] 106 { 107 null, null 108 }; 109 private Collection[] _value_collection = new Collection[] 110 { 111 null, null 112 }; 113 private static final int _KEY = 0; 114 private static final int _VALUE = 1; 115 private static final int _INDEX_SUM = _KEY + _VALUE; 116 private static final int _MINIMUM_INDEX = 0; 117 private static final int _INDEX_COUNT = 2; 118 private static final String [] _data_name = new String [] 119 { 120 "key", "value" 121 }; 122 123 126 127 public BinaryTree() 128 { 129 } 130 131 148 149 public BinaryTree(final Map map) 150 throws ClassCastException , NullPointerException , 151 IllegalArgumentException  152 { 153 putAll(map); 154 } 155 156 169 170 public Object getKeyForValue(final Object value) 171 throws ClassCastException , NullPointerException  172 { 173 return doGet(( Comparable ) value, _VALUE); 174 } 175 176 184 185 public Object removeValue(final Object value) 186 { 187 return doRemove(( Comparable ) value, _VALUE); 188 } 189 190 209 210 public Set entrySetByValue() 211 { 212 if (_entry_set[ _VALUE ] == null) 213 { 214 _entry_set[ _VALUE ] = new AbstractSet() 215 { 216 public Iterator iterator() 217 { 218 return new BinaryTreeIterator(_VALUE) 219 { 220 protected Object doGetNext() 221 { 222 return _last_returned_node; 223 } 224 }; 225 } 226 227 public boolean contains(Object o) 228 { 229 if (!(o instanceof Map.Entry)) 230 { 231 return false; 232 } 233 Map.Entry entry = ( Map.Entry ) o; 234 Object key = entry.getKey(); 235 Node node = lookup(( Comparable ) entry.getValue(), 236 _VALUE); 237 238 return (node != null) && node.getData(_KEY).equals(key); 239 } 240 241 public boolean remove(Object o) 242 { 243 if (!(o instanceof Map.Entry)) 244 { 245 return false; 246 } 247 Map.Entry entry = ( Map.Entry ) o; 248 Object key = entry.getKey(); 249 Node node = lookup(( Comparable ) entry.getValue(), 250 _VALUE); 251 252 if ((node != null) && node.getData(_KEY).equals(key)) 253 { 254 doRedBlackDelete(node); 255 return true; 256 } 257 return false; 258 } 259 260 public int size() 261 { 262 return BinaryTree.this.size(); 263 } 264 265 public void clear() 266 { 267 BinaryTree.this.clear(); 268 } 269 }; 270 } 271 return _entry_set[ _VALUE ]; 272 } 273 274 292 293 public Set keySetByValue() 294 { 295 if (_key_set[ _VALUE ] == null) 296 { 297 _key_set[ _VALUE ] = new AbstractSet() 298 { 299 public Iterator iterator() 300 { 301 return new BinaryTreeIterator(_VALUE) 302 { 303 protected Object doGetNext() 304 { 305 return _last_returned_node.getData(_KEY); 306 } 307 }; 308 } 309 310 public int size() 311 { 312 return BinaryTree.this.size(); 313 } 314 315 public boolean contains(Object o) 316 { 317 return containsKey(o); 318 } 319 320 public boolean remove(Object o) 321 { 322 int old_size = _size; 323 324 BinaryTree.this.remove(o); 325 return _size != old_size; 326 } 327 328 public void clear() 329 { 330 BinaryTree.this.clear(); 331 } 332 }; 333 } 334 return _key_set[ _VALUE ]; 335 } 336 337 355 356 public Collection valuesByValue() 357 { 358 if (_value_collection[ _VALUE ] == null) 359 { 360 _value_collection[ _VALUE ] = new AbstractCollection() 361 { 362 public Iterator iterator() 363 { 364 return new BinaryTreeIterator(_VALUE) 365 { 366 protected Object doGetNext() 367 { 368 return _last_returned_node.getData(_VALUE); 369 } 370 }; 371 } 372 373 public int size() 374 { 375 return BinaryTree.this.size(); 376 } 377 378 public boolean contains(Object o) 379 { 380 return containsValue(o); 381 } 382 383 public boolean remove(Object o) 384 { 385 int old_size = _size; 386 387 removeValue(o); 388 return _size != old_size; 389 } 390 391 public boolean removeAll(Collection c) 392 { 393 boolean modified = false; 394 Iterator iter = c.iterator(); 395 396 while (iter.hasNext()) 397 { 398 if (removeValue(iter.next()) != null) 399 { 400 modified = true; 401 } 402 } 403 return modified; 404 } 405 406 public void clear() 407 { 408 BinaryTree.this.clear(); 409 } 410 }; 411 } 412 return _value_collection[ _VALUE ]; 413 } 414 415 425 426 private Object doRemove(final Comparable o, final int index) 427 { 428 Node node = lookup(o, index); 429 Object rval = null; 430 431 if (node != null) 432 { 433 rval = node.getData(oppositeIndex(index)); 434 doRedBlackDelete(node); 435 } 436 return rval; 437 } 438 439 449 450 private Object doGet(final Comparable o, final int index) 451 { 452 checkNonNullComparable(o, index); 453 Node node = lookup(o, index); 454 455 return ((node == null) ? null 456 : node.getData(oppositeIndex(index))); 457 } 458 459 466 467 private int oppositeIndex(final int index) 468 { 469 470 return _INDEX_SUM - index; 474 } 475 476 485 486 private Node lookup(final Comparable data, final int index) 487 { 488 Node rval = null; 489 Node node = _root[ index ]; 490 491 while (node != null) 492 { 493 int cmp = compare(data, node.getData(index)); 494 495 if (cmp == 0) 496 { 497 rval = node; 498 break; 499 } 500 else 501 { 502 node = (cmp < 0) ? node.getLeft(index) 503 : node.getRight(index); 504 } 505 } 506 return rval; 507 } 508 509 518 519 private static int compare(final Comparable o1, final Comparable o2) 520 { 521 return (( Comparable ) o1).compareTo(o2); 522 } 523 524 534 535 private static Node leastNode(final Node node, final int index) 536 { 537 Node rval = node; 538 539 if (rval != null) 540 { 541 while (rval.getLeft(index) != null) 542 { 543 rval = rval.getLeft(index); 544 } 545 } 546 return rval; 547 } 548 549 557 558 private Node nextGreater(final Node node, final int index) 559 { 560 Node rval = null; 561 562 if (node == null) 563 { 564 rval = null; 565 } 566 else if (node.getRight(index) != null) 567 { 568 569 rval = leastNode(node.getRight(index), index); 572 } 573 else 574 { 575 576 Node parent = node.getParent(index); 583 Node child = node; 584 585 while ((parent != null) && (child == parent.getRight(index))) 586 { 587 child = parent; 588 parent = parent.getParent(index); 589 } 590 rval = parent; 591 } 592 return rval; 593 } 594 595 603 604 private static void copyColor(final Node from, final Node to, 605 final int index) 606 { 607 if (to != null) 608 { 609 if (from == null) 610 { 611 612 to.setBlack(index); 614 } 615 else 616 { 617 to.copyColor(from, index); 618 } 619 } 620 } 621 622 629 630 private static boolean isRed(final Node node, final int index) 631 { 632 return ((node == null) ? false 633 : node.isRed(index)); 634 } 635 636 643 644 private static boolean isBlack(final Node node, final int index) 645 { 646 return ((node == null) ? true 647 : node.isBlack(index)); 648 } 649 650 656 657 private static void makeRed(final Node node, final int index) 658 { 659 if (node != null) 660 { 661 node.setRed(index); 662 } 663 } 664 665 671 672 private static void makeBlack(final Node node, final int index) 673 { 674 if (node != null) 675 { 676 node.setBlack(index); 677 } 678 } 679 680 687 688 private static Node getGrandParent(final Node node, final int index) 689 { 690 return getParent(getParent(node, index), index); 691 } 692 693 700 701 private static Node getParent(final Node node, final int index) 702 { 703 return ((node == null) ? null 704 : node.getParent(index)); 705 } 706 707 714 715 private static Node getRightChild(final Node node, final int index) 716 { 717 return (node == null) ? null 718 : node.getRight(index); 719 } 720 721 728 729 private static Node getLeftChild(final Node node, final int index) 730 { 731 return (node == null) ? null 732 : node.getLeft(index); 733 } 734 735 746 747 private static boolean isLeftChild(final Node node, final int index) 748 { 749 return (node == null) ? true 750 : ((node.getParent(index) == null) ? false 751 : (node 752 == node.getParent( 753 index).getLeft( 754 index))); 755 } 756 757 768 769 private static boolean isRightChild(final Node node, final int index) 770 { 771 return (node == null) ? true 772 : ((node.getParent(index) == null) ? false 773 : (node 774 == node.getParent( 775 index).getRight( 776 index))); 777 } 778 779 785 786 private void rotateLeft(final Node node, final int index) 787 { 788 Node right_child = node.getRight(index); 789 790 node.setRight(right_child.getLeft(index), index); 791 if (right_child.getLeft(index) != null) 792 { 793 right_child.getLeft(index).setParent(node, index); 794 } 795 right_child.setParent(node.getParent(index), index); 796 if (node.getParent(index) == null) 797 { 798 799 _root[ index ] = right_child; 801 } 802 else if (node.getParent(index).getLeft(index) == node) 803 { 804 node.getParent(index).setLeft(right_child, index); 805 } 806 else 807 { 808 node.getParent(index).setRight(right_child, index); 809 } 810 right_child.setLeft(node, index); 811 node.setParent(right_child, index); 812 } 813 814 820 821 private void rotateRight(final Node node, final int index) 822 { 823 Node left_child = node.getLeft(index); 824 825 node.setLeft(left_child.getRight(index), index); 826 if (left_child.getRight(index) != null) 827 { 828 left_child.getRight(index).setParent(node, index); 829 } 830 left_child.setParent(node.getParent(index), index); 831 if (node.getParent(index) == null) 832 { 833 834 _root[ index ] = left_child; 836 } 837 else if (node.getParent(index).getRight(index) == node) 838 { 839 node.getParent(index).setRight(left_child, index); 840 } 841 else 842 { 843 node.getParent(index).setLeft(left_child, index); 844 } 845 left_child.setRight(node, index); 846 node.setParent(left_child, index); 847 } 848 849 856 857 private void doRedBlackInsert(final Node inserted_node, final int index) 858 { 859 Node current_node = inserted_node; 860 861 makeRed(current_node, index); 862 while ((current_node != null) && (current_node != _root[ index ]) 863 && (isRed(current_node.getParent(index), index))) 864 { 865 if (isLeftChild(getParent(current_node, index), index)) 866 { 867 Node y = getRightChild(getGrandParent(current_node, index), 868 index); 869 870 if (isRed(y, index)) 871 { 872 makeBlack(getParent(current_node, index), index); 873 makeBlack(y, index); 874 makeRed(getGrandParent(current_node, index), index); 875 current_node = getGrandParent(current_node, index); 876 } 877 else 878 { 879 if (isRightChild(current_node, index)) 880 { 881 current_node = getParent(current_node, index); 882 rotateLeft(current_node, index); 883 } 884 makeBlack(getParent(current_node, index), index); 885 makeRed(getGrandParent(current_node, index), index); 886 if (getGrandParent(current_node, index) != null) 887 { 888 rotateRight(getGrandParent(current_node, index), 889 index); 890 } 891 } 892 } 893 else 894 { 895 896 Node y = getLeftChild(getGrandParent(current_node, index), 898 index); 899 900 if (isRed(y, index)) 901 { 902 makeBlack(getParent(current_node, index), index); 903 makeBlack(y, index); 904 makeRed(getGrandParent(current_node, index), index); 905 current_node = getGrandParent(current_node, index); 906 } 907 else 908 { 909 if (isLeftChild(current_node, index)) 910 { 911 current_node = getParent(current_node, index); 912 rotateRight(current_node, index); 913 } 914 makeBlack(getParent(current_node, index), index); 915 makeRed(getGrandParent(current_node, index), index); 916 if (getGrandParent(current_node, index) != null) 917 { 918 rotateLeft(getGrandParent(current_node, index), 919 index); 920 &nb
|