fork download
  1. class Bintree
  2. {
  3. class Node
  4. {
  5. int value;
  6. Node left;
  7. Node right;
  8.  
  9. public Node(final int v)
  10. {
  11. value = v;
  12. }
  13.  
  14. public void addNode(final Node n)
  15. {
  16. if (n.value < value)
  17. {
  18. if (left == null)
  19. {
  20. left = n;
  21. }
  22. else
  23. {
  24. left.addNode(n);
  25. }
  26. }
  27. else
  28. {
  29. if (right == null)
  30. {
  31. right = n;
  32. }
  33. else
  34. {
  35. right.addNode(n);
  36. }
  37. }
  38. }
  39.  
  40. public Node delNode(final int v)
  41. {
  42. if (v < value)
  43. {
  44. if (left == null)
  45. {
  46. return this;
  47. }
  48. else
  49. {
  50. left = left.delNode(v);
  51. return this;
  52. }
  53. }
  54. else if (value < v)
  55. {
  56. if (right == null)
  57. {
  58. return this;
  59. }
  60. else
  61. {
  62. right = right.delNode(v);
  63. return this;
  64. }
  65. }
  66. else
  67. {
  68. if (left == null)
  69. {
  70. return right;
  71. }
  72. else if (right == null)
  73. {
  74. return left;
  75. }
  76. else
  77. {
  78. value = right.minValue();
  79. right = right.delMin();
  80. return this;
  81. }
  82. }
  83. }
  84.  
  85. int minValue()
  86. {
  87. if (left == null)
  88. {
  89. return value;
  90. }
  91. else
  92. {
  93. return left.minValue();
  94. }
  95. }
  96.  
  97. Node delMin()
  98. {
  99. if (left == null)
  100. {
  101. return right;
  102. }
  103. else
  104. {
  105. left = left.delMin();
  106. return this;
  107. }
  108. }
  109.  
  110. }
  111.  
  112. Node root;
  113.  
  114. public void insert(final int v)
  115. {
  116. final Node n = new Node(v);
  117.  
  118. if (root == null)
  119. {
  120. root = n;
  121. }
  122. else
  123. {
  124. root.addNode(n);
  125. }
  126. }
  127.  
  128. public void remove(final int v)
  129. {
  130. if (root == null)
  131. {
  132. return;
  133. }
  134. else
  135. {
  136. root = root.delNode(v);
  137. }
  138. }
  139. }
  140.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty