Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class BinarySearchTree<E extends Comparable<E>> {
  4.  
  5.  
  6. class Node {
  7. E value;
  8. Node leftChild = null;
  9. Node rightChild = null;
  10. Node(E value) {
  11. this.value = value;
  12. }
  13. @Override
  14. public boolean equals(Object obj) {
  15. if ((obj instanceof BinarySearchTree.Node) == false)
  16. return false;
  17. @SuppressWarnings("unchecked")
  18. Node other = (BinarySearchTree<E>.Node)obj;
  19. return other.value.compareTo(value) == 0 &&
  20. other.leftChild == leftChild && other.rightChild == rightChild;
  21. }
  22. }
  23.  
  24. protected Node root = null;
  25.  
  26. protected void visit(Node n) {
  27. System.out.println(n.value);
  28. }
  29.  
  30. public boolean contains(E val) {
  31. return contains(root, val);
  32. }
  33.  
  34. protected boolean contains(Node n, E val) {
  35. if (n == null) return false;
  36.  
  37. if (n.value.equals(val)) {
  38. return true;
  39. } else if (n.value.compareTo(val) > 0) {
  40. return contains(n.leftChild, val);
  41. } else {
  42. return contains(n.rightChild, val);
  43. }
  44. }
  45.  
  46. public boolean add(E val) {
  47. if (root == null) {
  48. root = new Node(val);
  49. return true;
  50. }
  51. return add(root, val);
  52. }
  53.  
  54. protected boolean add(Node n, E val) {
  55. if (n == null) {
  56. return false;
  57. }
  58. int cmp = val.compareTo(n.value);
  59. if (cmp == 0) {
  60. return false; // this ensures that the same value does not appear more than once
  61. } else if (cmp < 0) {
  62. if (n.leftChild == null) {
  63. n.leftChild = new Node(val);
  64. return true;
  65. } else {
  66. return add(n.leftChild, val);
  67. }
  68. } else {
  69. if (n.rightChild == null) {
  70. n.rightChild = new Node(val);
  71. return true;
  72. } else {
  73. return add(n.rightChild, val);
  74. }
  75. }
  76. }
  77.  
  78. public boolean remove(E val) {
  79. return remove(root, null, val);
  80. }
  81.  
  82. protected boolean remove(Node n, Node parent, E val) {
  83. if (n == null) return false;
  84.  
  85. if (val.compareTo(n.value) == -1) {
  86. return remove(n.leftChild, n, val);
  87. } else if (val.compareTo(n.value) == 1) {
  88. return remove(n.rightChild, n, val);
  89. } else {
  90. if (n.leftChild != null && n.rightChild != null){
  91. n.value = maxValue(n.leftChild);
  92. remove(n.leftChild, n, n.value);
  93. } else if (parent == null) {
  94. root = n.leftChild != null ? n.leftChild : n.rightChild;
  95. } else if (parent.leftChild == n){
  96. parent.leftChild = n.leftChild != null ? n.leftChild : n.rightChild;
  97. } else {
  98. parent.rightChild = n.leftChild != null ? n.leftChild : n.rightChild;
  99. }
  100. return true;
  101. }
  102. }
  103.  
  104. protected E maxValue(Node n) {
  105. if (n.rightChild == null) {
  106. return n.value;
  107. } else {
  108. return maxValue(n.rightChild);
  109. }
  110. }
  111.  
  112.  
  113. /*********************************************
  114. *
  115. * IMPLEMENT THE METHODS BELOW!
  116. *
  117. *********************************************/
  118.  
  119.  
  120. // Method #1.
  121. public Node findNode(E val) {
  122.  
  123. /* IMPLEMENT THIS METHOD! */
  124. if(val == null)
  125. return null;
  126. if(contains(val))
  127. return findNode(root,val);
  128. else
  129. return null; // this line is here only so this code will compile if you don't modify it
  130.  
  131. }
  132.  
  133. protected Node findNode(Node n, E val)
  134. {
  135.  
  136. if (n.value.equals(val)) {
  137. return n;
  138. } else if (n.value.compareTo(val) > 0) {
  139. return findNode(n.leftChild, val);
  140. } else {
  141. return findNode(n.rightChild, val);
  142. }
  143.  
  144. }
  145.  
  146.  
  147. // Method #2.
  148. protected int depth(E val) {
  149.  
  150. /* IMPLEMENT THIS METHOD! */
  151.  
  152. if(val == null || findNode(val) == null)
  153. return -1;
  154.  
  155.  
  156. int depth = 0;
  157. Node n = root;
  158.  
  159. //basic case to return the root depth
  160.  
  161. if (n.value == val) {
  162. return depth;
  163. }
  164.  
  165. while (!n.value.equals(val)) {
  166. if (n.value.compareTo(val) > 0) {
  167. n = n.leftChild;
  168. depth++;
  169. } else if (n.value.compareTo(val) < 0) {
  170. n = n.rightChild;
  171. depth++;
  172. }
  173. }
  174.  
  175. if (depth > 0) {
  176. return depth;
  177. }
  178.  
  179.  
  180. return -2; // this line is here only so this code will compile if you don't modify it
  181.  
  182. }
  183.  
  184.  
  185.  
  186. // Method #3.
  187. protected int height(E val) {
  188.  
  189. /* IMPLEMENT THIS METHOD! */
  190.  
  191. Node n = findNode(val);
  192. int lcounter = 0;
  193. int rcounter = 0;
  194.  
  195. //Goes on until find that the left or right child is null
  196. //keeps increasing the counter
  197.  
  198. if(n != null)
  199. {
  200. while(n.leftChild != null) {
  201. n = n.leftChild;
  202. lcounter++;
  203. }
  204. n = findNode(val);
  205. while(n.rightChild != null) {
  206. n = n.rightChild;
  207. rcounter++;
  208. }
  209. }
  210.  
  211. //Different small conditions
  212.  
  213. if(rcounter>lcounter)
  214. return rcounter;
  215. else if(lcounter>rcounter)
  216. return lcounter;
  217. else if(val == null || n == null)
  218. return -1;
  219. else if(rcounter == lcounter)
  220. return rcounter;
  221. else if(n.rightChild == null || n.leftChild == null)
  222. return 0;
  223.  
  224.  
  225.  
  226. return -2; // this line is here only so this code will compile if you don't modify it
  227.  
  228. }
  229.  
  230.  
  231.  
  232.  
  233. // Method #4.
  234. protected boolean isBalanced(Node n) {
  235.  
  236. /* IMPLEMENT THIS METHOD! */
  237.  
  238. //Subtle test cases to be taken care
  239.  
  240. if(n == null || findNode(n.value) == null)
  241. return false;
  242.  
  243. if(n.leftChild == null || n.rightChild == null)
  244. return true; //because difference will be 0
  245.  
  246. int left = height(n.leftChild.value);
  247. int right = height(n.rightChild.value);
  248. int h = Math.abs((left - right));
  249.  
  250. if (h >= 0 && h <= 1)
  251. return true;
  252.  
  253.  
  254. return false; // this line is here only so this code will compile if you don't modify it
  255.  
  256. }
  257.  
  258. // Method #5. .
  259. public boolean isBalanced() {
  260.  
  261. /* IMPLEMENT THIS METHOD! */
  262.  
  263. return false;
  264. }
  265.  
  266.  
  267. private static Stack iterate(BinarySearchTree bst)
  268. {
  269.  
  270. Stack<BinarySearchTree> nodes = new Stack<>();
  271. nodes.push(bst);
  272. while (!nodes.isEmpty()) {
  273. BinarySearchTree node = nodes.pop();
  274. if (node == null)
  275. continue;
  276. else {
  277. //System.out.println(node.node);
  278. // nodes.push(node.rightChild);
  279. // nodes.push(node.leftChild);
  280. }
  281. }
  282. return nodes;
  283.  
  284. }
  285.  
  286.  
  287. }
Add Comment
Please, Sign In to add comment