fork download
  1. class BinTree {
  2. private BinNode root;
  3.  
  4. public BinTree(BinNode root) {
  5. this.root = root;
  6. }
  7.  
  8. public void recreate(int key) {
  9. if(root != null) { root = root.getNewRoot(key); }
  10. if(root != null) { root.search(root, key); }
  11. }
  12.  
  13. @Override public String toString() {
  14. return "{" + (root == null ? "" : "" + root.getKey() + (root.getLeft() == null ? "" : ", left: " + root.getLeft()) + (root.getRight() == null ? "" : ", right: " + root.getRight())) + "}";
  15. }
  16. }
  17.  
  18. class BinNode {
  19. private int key;
  20. private BinNode left;
  21. private BinNode right;
  22. private BinNode parent;
  23.  
  24. public BinNode(int key, BinNode left, BinNode right) {
  25. this.key = key;
  26. this.left = left;
  27. this.right = right;
  28. if(this.left != null) { this.left.parent = this; }
  29. if(this.right != null) { this.right.parent = this; }
  30. }
  31.  
  32. public BinNode getNewRoot(int key) {
  33. BinNode newRoot = this;
  34. for( ; newRoot != null; ) {
  35. if(key == newRoot.key) { throw new IllegalArgumentException(); }
  36. if(key < newRoot.key) { newRoot = newRoot.left; }
  37. else { break; }
  38. }
  39. return newRoot;
  40. }
  41.  
  42. public void search(BinNode p, int key) {
  43. if(p == null) { return; }
  44. else if(p.key < key) {
  45. search(p.right, key);
  46. } else if(p.key > key) {
  47. if(p.parent != null) { p.parent.right = p.left; }
  48. search(p.left, key);
  49. } else {
  50. }
  51. }
  52.  
  53. public int getKey() { return key; }
  54. public BinNode getLeft() { return left; }
  55. public BinNode getRight() { return right; }
  56.  
  57. @Override public String toString() {
  58. return "[" + key + (left == null ? "" : ", left: " + left) + (right == null ? "" : ", right: " + right) + "]";
  59. }
  60. }
  61.  
  62. public class BSTMinDistiller {
  63. public static void main(String[] args) {
  64. BinNode n02 = new BinNode( 2, null, null);
  65. BinNode n10 = new BinNode(10, null, null);
  66. BinNode n14 = new BinNode(14, null, null);
  67. BinNode n22 = new BinNode(22, null, null);
  68. BinNode n26 = new BinNode(26, null, null);
  69. BinNode n34 = new BinNode(34, null, null);
  70. BinNode n38 = new BinNode(38, null, null);
  71. BinNode n46 = new BinNode(46, null, null);
  72. BinNode n52 = new BinNode(52, null, null);
  73. BinNode n60 = new BinNode(60, null, null);
  74. BinNode n64 = new BinNode(64, null, null);
  75. BinNode n72 = new BinNode(72, null, null);
  76. BinNode n76 = new BinNode(76, null, null);
  77. BinNode n84 = new BinNode(84, null, null);
  78. BinNode n88 = new BinNode(88, null, null);
  79. BinNode n96 = new BinNode(96, null, null);
  80.  
  81. BinNode n06 = new BinNode( 6, n02, n10);
  82. BinNode n18 = new BinNode(18, n14, n22);
  83. BinNode n30 = new BinNode(30, n26, n34);
  84. BinNode n42 = new BinNode(42, n38, n46);
  85. BinNode n56 = new BinNode(56, n52, n60);
  86. BinNode n68 = new BinNode(68, n64, n72);
  87. BinNode n80 = new BinNode(80, n76, n84);
  88. BinNode n92 = new BinNode(92, n88, n96);
  89.  
  90. BinNode n12 = new BinNode(12, n06, n18);
  91. BinNode n36 = new BinNode(36, n30, n42);
  92. BinNode n62 = new BinNode(62, n56, n68);
  93. BinNode n86 = new BinNode(86, n80, n92);
  94.  
  95. BinNode n25 = new BinNode(25, n12, n36);
  96. BinNode n75 = new BinNode(75, n62, n86);
  97.  
  98. BinTree tree01 = new BinTree(new BinNode(50, n25, n75));
  99. System.out.println("tree01");
  100. System.out.println(tree01);
  101.  
  102. BinTree tree02 = new BinTree(new BinNode(50, n25, n75));
  103. tree02.recreate(65);
  104. System.out.println("tree02: 65");
  105. System.out.println(tree02);
  106.  
  107. BinTree tree03 = new BinTree(new BinNode(50, n25, n75));
  108. tree03.recreate(49);
  109. System.out.println("tree03: 49");
  110. System.out.println(tree03);
  111.  
  112. BinTree tree04 = new BinTree(new BinNode(50, n25, n75));
  113. tree04.recreate(1);
  114. System.out.println("tree04: 1");
  115. System.out.println(tree04);
  116.  
  117. BinTree tree05 = new BinTree(new BinNode(50, n25, n75));
  118. tree05.recreate(97);
  119. System.out.println("tree05: 97");
  120. System.out.println(tree05);
  121.  
  122. BinTree tree06 = new BinTree(new BinNode(50, n25, n75));
  123. tree06.recreate(9);
  124. System.out.println("tree06: 9");
  125. System.out.println(tree06);
  126.  
  127. System.out.println();
  128.  
  129. BinTree tree07 = new BinTree(new BinNode(2, null, null));
  130. System.out.println("tree07");
  131. System.out.println(tree07);
  132.  
  133. BinTree tree08 = new BinTree(new BinNode(2, null, null));
  134. tree08.recreate(3);
  135. System.out.println("tree08: 3");
  136. System.out.println(tree08);
  137.  
  138. BinTree tree09 = new BinTree(new BinNode(2, null, null));
  139. tree09.recreate(1);
  140. System.out.println("tree09: 1");
  141. System.out.println(tree09);
  142.  
  143. System.out.println();
  144.  
  145. BinTree tree10 = new BinTree(null);
  146. System.out.println("tree10");
  147. System.out.println(tree10);
  148.  
  149. BinTree tree11 = new BinTree(null);
  150. tree11.recreate(99);
  151. System.out.println("tree11: 99");
  152. System.out.println(tree11);
  153. }
  154. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty