Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1.  
  2. import java.util.Stack;
  3.  
  4. public class OrderedBSTreeResha {
  5. Node root;
  6. Stack<Integer> stack = new Stack<Integer>();
  7. class Node {
  8. int val;
  9. Node left, right;
  10.  
  11. public Node(int item) {
  12. val = item;
  13. left = right = null;
  14. }
  15.  
  16. public Node getLeft() {
  17. return left;
  18. }
  19. public Node getRight() {
  20. return right;
  21. }
  22. }
  23.  
  24. // Root of BST
  25.  
  26.  
  27.  
  28. public int getMin() {
  29. Node current = root;
  30. while (current.left != null) {
  31. current = current.left;
  32. }
  33. return (current.val);
  34. }
  35.  
  36. public int getMax() {
  37.  
  38. Node current = root;
  39. while (current.right != null) {
  40. current = current.right;
  41. }
  42. return (current.val);
  43. }
  44.  
  45. public boolean delete(int val){
  46.  
  47. try {
  48. if (deletion(root, val) != null) {
  49. stack.pop();
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. catch (Exception e){
  56. return false;
  57. }
  58. }
  59.  
  60. private Node deletion(Node root, int val){
  61. if(val < root.val) root.left = deletion(root.left, val);
  62. else if(val > root.val) root.right = deletion(root.right, val);
  63. else
  64. {
  65. if(root.getLeft() == null && root.getRight() == null)
  66. {
  67. return null;
  68. }
  69. else if(root.getLeft() == null)
  70. {
  71. // node with one node (no left node)
  72. return root.getRight();
  73. }
  74. else if(root.getRight() == null)
  75. {
  76. // node with one node (no right node)
  77. return root.getLeft();
  78. }
  79. }
  80. return root;
  81. }
  82.  
  83. boolean insert(int val) {
  84.  
  85. if (insertion(root, val) == null) {
  86. return true;
  87. } else {
  88. root = insertion(root, val);
  89. return false;
  90. }
  91. }
  92.  
  93. private Node insertion(Node root, int val) {
  94.  
  95. if (root == null) {
  96. root = new Node(val);
  97. stack.push(val);
  98. return root;
  99. }
  100. if (val < root.val) {
  101. root.left = insertion(root.left, val);
  102. stack.push(val);
  103. } else if (val > root.val) {
  104. root.right = insertion(root.right, val);
  105. stack.push(val);
  106. }
  107.  
  108. return root;
  109.  
  110. }
  111.  
  112. public int removeLast(){
  113. int temp = stack.peek();
  114. delete(temp);
  115. stack.pop();
  116. return stack.peek();
  117. }
  118. public int getLast() {
  119. return stack.peek();
  120. }
  121.  
  122. public void inorder() {
  123. inorderRec(root);
  124. }
  125.  
  126. // A utility function to do inorder traversal of BST
  127. void inorderRec(Node root) {
  128. if (root != null) {
  129. inorderRec(root.left);
  130. System.out.println(root.val);
  131. inorderRec(root.right);
  132. }
  133. }
  134.  
  135. public static void main(String[] args) throws Exception {
  136. //example1
  137. OrderedBSTreeResha tree = new OrderedBSTreeResha();
  138. tree.insert(15);
  139. tree.insert(12);
  140. tree.insert(10);
  141. tree.insert(29);
  142. tree.insert(5);
  143. tree.insert(100);
  144. tree.insert(100);
  145. System.out.println(tree.getMax()); //100
  146. System.out.println(tree.getLast()); //100
  147. System.out.println(tree.removeLast()); //100
  148. System.out.println(tree.delete(100)); //false
  149. System.out.println(tree.getLast()); //5
  150. System.out.println(tree.getMin()); //5
  151. System.out.println(tree.getMax()); //29
  152. System.out.println(tree.delete(12)); //true
  153. System.out.println(tree.delete(12)); //false
  154. System.out.println(tree.insert(10)); //false
  155.  
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement