PermutationTree.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * PermutationTree
  3. *
  4. * This data structure builds a binary search tree of all the possible permutations of a given integer
  5. */
  6. import java.util.Iterator;
  7. import java.util.LinkedList;
  8. public class PermutationTree {
  9. protected int count;
  10. protected Node root;
  11. // Constructors
  12. public PermutationTree() {
  13. count = 0;
  14. }
  15. public PermutationTree(int e) {
  16. count = 1;
  17. root = new Node(e);
  18. this.split(e);
  19. }
  20. // Getters
  21. public int getCount() {
  22. return count;
  23. }
  24. // Insert
  25. public void insert(int e) {
  26. count = 1;
  27. root = new Node(e);
  28. this.split(e);
  29. }
  30. private void split(int e) {
  31. int strlen = String.valueOf(e).length();
  32. for(int mod = 10; mod <= e; mod *= 10) {
  33. int temp = e;
  34. for(int c = 0; c < strlen; ++c) {
  35. this.addElement(temp % mod);
  36. temp /= 10;
  37. }
  38. --strlen;
  39. }
  40. // Since we've recreated the root at the bottom-right of the tree,
  41. // move the root pointer to root's left and delete the old root.
  42. root = root.getLeftChild();
  43. }
  44. private void addElement(int e) {
  45. Comparable key = e;
  46. Node element = new Node(e);
  47. Node current = root;
  48. boolean added = false;
  49. // Add the new node
  50. while(!added) {
  51. if(key.compareTo(current.getElement()) < 0) {
  52. if(current.getLeftChild() == null) {
  53. current.setLeftChild(element);
  54. current.getLeftChild().setParent(current);
  55. added = true;
  56. } else {
  57. current = current.getLeftChild();
  58. }
  59. } else {
  60. if(current.getRightChild() == null) {
  61. current.setRightChild(element);
  62. current.getRightChild().setParent(current);
  63. added = true;
  64. } else {
  65. current = current.getRightChild();
  66. }
  67. }
  68. }
  69. ++count;
  70. }
  71. // Find a specific element
  72. public Node find(int e) throws ElementNotFoundException {
  73. Node current = locate(e, root);
  74. if(current == null)
  75. throw new ElementNotFoundException("Permutation Tree");
  76. return current;
  77. }
  78. private Node locate(int e, Node next) {
  79. if(next == null)
  80. return null;
  81. if (next.getElement() == e)
  82. return next;
  83. Node temp = locate(e, next.getLeftChild());
  84. if(next == null)
  85. temp = locate(e, next.getRightChild());
  86. return temp;
  87. }
  88. // Define a way to print the tree
  89. public String toString() {
  90. String result = "";
  91. Node temp = root;
  92. int spaceCount = 0;
  93. if(temp.hasLeftChild()) {
  94. while(temp.hasLeftChild()) {
  95. spaceCount += 2;
  96. temp = temp.getLeftChild();
  97. }
  98. }
  99. if (spaceCount > 0) { for(int c = 0; c < spaceCount; ++c) { result += " "; } }
  100. result += root.getElement();
  101. result += "\n";
  102. if (spaceCount > 0) {
  103. for(int c = 0; c < spaceCount - 1; ++c) { result += " "; }
  104. result += "/";
  105. if (root.hasRightChild()) { result += " \\"; }
  106. }
  107. return result;
  108. }
  109. }