Advertisement
lashrone1

bst

Nov 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1.  
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Stack;
  5.  
  6. public class BinaryTree {
  7.  
  8. Node root;
  9.  
  10. public void add(int value) {
  11. root = add(root, value);
  12. }
  13.  
  14. private Node add(Node current, int value) {
  15.  
  16. if (current == null) {
  17. return new Node(value);
  18. }
  19.  
  20. if (value < current.value) {
  21. current.left = add(current.left, value);
  22. } else if (value > current.value) {
  23. current.right = add(current.right, value);
  24. }
  25.  
  26. return current;
  27. }
  28.  
  29. public void delete(int value) {
  30. root = delete(root, value);
  31. }
  32.  
  33. private Node delete(Node current, int value) {
  34. if (current == null) {
  35. return null;
  36. }
  37.  
  38. if (value == current.value) {
  39. // Case 1: no children
  40. if (current.left == null && current.right == null) {
  41. return null;
  42. }
  43.  
  44. // Case 2: only 1 child
  45. if (current.right == null) {
  46. return current.left;
  47. }
  48.  
  49. if (current.left == null) {
  50. return current.right;
  51. }
  52.  
  53. // Case 3: 2 children
  54. int smallestValue = findSmallestValue(current.right);
  55. current.value = smallestValue;
  56. current.right = delete(current.right, smallestValue);
  57. return current;
  58. }
  59. if (value < current.value) {
  60. current.left = delete(current.left, value);
  61. return current;
  62. }
  63.  
  64. current.right = delete(current.right, value);
  65. return current;
  66. }
  67.  
  68. private int findSmallestValue(Node root) {
  69. return root.left == null ? root.value : findSmallestValue(root.left);
  70. }
  71.  
  72. public int Depth (){
  73. return maxDepth(root);
  74. }
  75.  
  76.  
  77.  
  78. private int maxDepth(Node node) {
  79.  
  80. if (node == null)
  81. return 0;
  82. else {
  83.  
  84. int lDepth = maxDepth(node.left);
  85. int rDepth = maxDepth(node.right);
  86.  
  87. if (lDepth > rDepth)
  88. return (lDepth + 1);
  89. else
  90. return (rDepth + 1);
  91. }
  92. }
  93.  
  94.  
  95. public void traverseInOrder() {
  96. Stack<Node> stack = new Stack<Node>();
  97. Node current = root;
  98. stack.push(root);
  99. while(! stack.isEmpty()) {
  100. while(current.left != null) {
  101. current = current.left;
  102. stack.push(current);
  103. }
  104. current = stack.pop();
  105. printVal(current.value);
  106. if(current.right != null) {
  107. current = current.right;
  108. stack.push(current);
  109. }
  110. }
  111. }
  112.  
  113. public void traversePreOrder() {
  114. Stack<Node> stack = new Stack<Node>();
  115. Node current = root;
  116. stack.push(root);
  117. while(! stack.isEmpty()) {
  118. current = stack.pop();
  119. printVal(current.value);
  120.  
  121. if(current.right != null)
  122. stack.push(current.right);
  123.  
  124. if(current.left != null)
  125. stack.push(current.left);
  126. }
  127. }
  128.  
  129. public void traversePostOrder() {
  130. Stack<Node> stack = new Stack<Node>();
  131. Node prev = root;
  132. Node current = root;
  133. stack.push(root);
  134.  
  135. while (!stack.isEmpty()) {
  136. current = stack.peek();
  137. boolean hasChild = (current.left != null || current.right != null);
  138. boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));
  139.  
  140. if (!hasChild || isPrevLastChild) {
  141. current = stack.pop();
  142. printVal(current.value);
  143. prev = current;
  144. } else {
  145. if (current.right != null) {
  146. stack.push(current.right);
  147. }
  148. if (current.left != null) {
  149. stack.push(current.left);
  150. }
  151. }
  152. }
  153. }
  154.  
  155. private void printVal(int value) {
  156. System.out.print(" " + value);
  157. }
  158.  
  159. public void traverseLevelOrder() {
  160.  
  161. if (root == null) {
  162. return;
  163. }
  164.  
  165. Queue<Node> nodes = new LinkedList<>();
  166. nodes.add(root);
  167.  
  168. while (!nodes.isEmpty()) {
  169.  
  170. Node node = nodes.remove();
  171.  
  172. System.out.print(" " + node.value);
  173.  
  174. if (node.left != null) {
  175. nodes.add(node.left);
  176. }
  177.  
  178. if (node.right!= null) {
  179. nodes.add(node.right);
  180. }
  181. }
  182. }
  183.  
  184. public void rttraverseLevelOrder() {
  185.  
  186. if (root == null) {
  187. return;
  188. }
  189.  
  190. Queue<Node> nodes = new LinkedList<>();
  191. nodes.add(root);
  192.  
  193. while (!nodes.isEmpty()) {
  194.  
  195. Node node = nodes.remove();
  196.  
  197. System.out.print(" " + node.value);
  198.  
  199. if (node.left != null) {
  200. nodes.add(node.left);
  201. }
  202.  
  203. if (node.right!= null) {
  204. nodes.add(node.right);
  205. }
  206. }
  207. }
  208.  
  209.  
  210. class Node {
  211. int value;
  212. Node left;
  213. Node right;
  214.  
  215. Node(int value) {
  216. this.value = value;
  217. right = null;
  218. left = null;
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement