Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. public class BinarySearchTree {
  2. private class BSTNode{ //private class to hold a tree node
  3.  
  4. private int value;
  5. private BSTNode leftChild; //left subtree
  6. private BSTNode rightChild; //right subtree
  7.  
  8. public BSTNode(int v){
  9. value = v;
  10. leftChild = null;
  11. rightChild = null;
  12. }
  13.  
  14. public BSTNode getLeftChild(){
  15. return leftChild;
  16. }
  17. public BSTNode getRightChild(){
  18. return rightChild;
  19. }
  20. public void setLeftChild(BSTNode n){
  21. leftChild = n;
  22. }
  23. public void setRightChild(BSTNode n){
  24. rightChild = n;
  25. }
  26. public int getValue(){
  27. return value;
  28. }
  29.  
  30. public void insertNode(BSTNode n){
  31. if(n.value < this.value){ //goes into the left subtree
  32. if(this.getLeftChild() == null){
  33. this.setLeftChild(n);
  34. }else{
  35. this.getLeftChild().insertNode(n);
  36. }
  37. }else if(n.value > this.value){ //goes into the right subtree
  38. if (this.getRightChild() == null){
  39. this.setRightChild(n);
  40. }else{
  41. this.getRightChild().insertNode(n);
  42. }
  43. }else{ //Trying to insert a duplicate, which we don't allow: could also raise exception
  44. return;
  45. }
  46. }
  47.  
  48. public boolean containsNodeWithValue(int v){
  49. if(v < this.value){
  50. if(this.getLeftChild() == null){
  51. return false;
  52. }else{
  53. return this.getLeftChild().containsNodeWithValue(v);
  54. }
  55. }else if(v > this.value){
  56. if (this.getRightChild() == null){
  57. return false;
  58. }else{
  59. return this.getRightChild().containsNodeWithValue(v);
  60. }
  61. }
  62. return true; //this.value == v
  63. }
  64.  
  65. public BSTNode getLargestValueNode(){
  66. //decend down the right subtree until we get the largest value.
  67. //i.e. until we cannot continue to go down the right subtree
  68. if (this.getRightChild() == null){
  69. return this;
  70. }else{
  71. return this.getRightChild().getLargestValueNode();
  72. }
  73. }
  74.  
  75. public BSTNode deleteNodeWithValue(int v){
  76. if(v == this.value){ //this is the node we want to remove
  77. //does it have two children?
  78. if (this.getLeftChild() != null && this.getRightChild() != null){
  79. BSTNode largestLeft = this.getLeftChild().getLargestValueNode(); //find the largest value in the left subtree
  80. this.value = largestLeft.value; //set the current node value to this node
  81. this.leftChild = this.getLeftChild().deleteNodeWithValue(largestLeft.value); //remove the largest left node
  82. return this;
  83. //otherwise we promote the only valid subtree node
  84. }else if(this.getLeftChild() != null){
  85. return this.getLeftChild();
  86. }else if(this.getRightChild() != null){
  87. return this.getRightChild();
  88. }else{ // no children. this is a leaf node
  89. System.out.println("Deleting Node with value "+this.value+" Returning Null");
  90. return null;
  91. }
  92. }else{ //this isn't the node we want to remove, so keep going
  93. if(v > this.value && this.getRightChild()!= null){
  94. this.rightChild = this.getRightChild().deleteNodeWithValue(v);
  95. return this;
  96. }else if(v < this.value && this.getLeftChild()!=null){
  97. this.leftChild = this.getLeftChild().deleteNodeWithValue(v);
  98. return this;
  99. }else{ //We are trying to remove a non existent node
  100. return this;
  101. }
  102. }
  103. }
  104.  
  105. public int numberOfNodes(){
  106. int valueToReturn = 1; //this is 1 to count this node
  107. if(leftChild != null)
  108. valueToReturn += leftChild.numberOfNodes(); //add the nodes in the left subtree
  109. if(rightChild != null)
  110. valueToReturn +=rightChild.numberOfNodes(); //add the nodes in the right subtree
  111. return valueToReturn; //return the number of nodes
  112. }
  113.  
  114. // Part 1: complete
  115. public void inOrderTraversal(){
  116. if(rootNode.leftChild != null) {
  117. rootNode = rootNode.leftChild;
  118. inOrderTraversal();
  119. }
  120.  
  121. System.out.println(rootNode.value);
  122.  
  123. if(rootNode.rightChild != null) {
  124. rootNode = rootNode.rightChild;
  125. inOrderTraversal();
  126. }
  127.  
  128.  
  129. }
  130.  
  131. public void inOrderTraversal(DLinkedList dll) {
  132.  
  133. if(this.leftChild != null) {
  134. // rootNode = rootNode.leftChild;
  135. this.getLeftChild().inOrderTraversal(dll);
  136. //inOrderTraversal(dll);
  137. }
  138.  
  139. dll.addAtTail(this.value);
  140. System.err.print(this.value + ", ");
  141.  
  142. if(this.rightChild != null) {
  143. // rootNode = rootNode.rightChild;
  144. this.getRightChild().inOrderTraversal(dll);
  145. }
  146.  
  147. }
  148.  
  149.  
  150. }
  151.  
  152. private BSTNode rootNode = null;
  153.  
  154. public void insert(int v){
  155. if (rootNode == null){
  156. rootNode = new BSTNode(v);
  157. }else{
  158. rootNode.insertNode(new BSTNode(v));
  159. }
  160. }
  161.  
  162. public void remove(int v){
  163. if (rootNode != null){
  164. rootNode = rootNode.deleteNodeWithValue(v);
  165. }
  166. }
  167.  
  168. public boolean contains(int v){
  169. if (rootNode != null){
  170. return rootNode.containsNodeWithValue(v);
  171. }else{
  172. return false;
  173. }
  174. }
  175.  
  176. // Part 1
  177. public void inOrderTraversal(){
  178. if (rootNode != null)
  179. rootNode.inOrderTraversal();
  180. }
  181.  
  182. // Part 2: complete
  183. public DLinkedList returnInOrderTraversal(){
  184. DLinkedList dll = new DLinkedList();
  185. System.err.print("[");
  186. if(rootNode != null) {
  187. rootNode.inOrderTraversal(dll);
  188. }
  189. System.err.println("]");
  190. return dll;
  191. }
  192.  
  193. public static void main(String[] args){
  194.  
  195. System.out.println("******* Tree 1 : 3 nodes ***********");
  196. BinarySearchTree bst1 = new BinarySearchTree();
  197. bst1.insert(1);
  198. bst1.insert(2);
  199. bst1.insert(3);
  200. bst1.insert(4);
  201. bst1.inOrderTraversal();
  202.  
  203. System.out.println("******* Tree 2 : 1 node ***********");
  204. BinarySearchTree bst2 = new BinarySearchTree();
  205. bst2.insert(1);
  206. bst2.inOrderTraversal();
  207.  
  208. System.out.println("******* Tree 3 : empty ***********");
  209. BinarySearchTree bst3 = new BinarySearchTree();
  210. bst3.inOrderTraversal();
  211.  
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement