Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class NumericBinaryTree {
  6.  
  7. private List<Number> holdTheValues = new ArrayList<Number>();
  8. private Number rootValue;
  9. private NumericBinaryTree leftChild;
  10. private NumericBinaryTree rightChild;
  11.  
  12. /**
  13. * Constructor that will create an empty numeric
  14. * binary tree.
  15. * @param
  16. */
  17. public NumericBinaryTree() {
  18. this.rootValue = null;
  19. this.leftChild = null;
  20. this.rightChild = null;
  21. }
  22.  
  23. /**
  24. * Constructor that will create a numeric binary tree with
  25. * a value.
  26. * @param takes a value of type number and sets it as the
  27. * root of the tree
  28. */
  29. public NumericBinaryTree(Number rootValue) {
  30. if (rootValue == null) {
  31. throw new IllegalArgumentException("");
  32. }
  33. this.rootValue = rootValue;
  34. System.out.println("this is the root value" + rootValue);
  35. }
  36.  
  37. /**
  38. * Constructor that will create a binary tree with a value and
  39. * its left and right child.
  40. *
  41. * @param takes a value of type number and sets it as the
  42. * root of the tree. Takes a tree for its left child and right
  43. * child.
  44. */
  45.  
  46. public NumericBinaryTree(Number rootValue, NumericBinaryTree leftChild,
  47. NumericBinaryTree rightChild) {
  48. if (rootValue == null) {
  49. throw new IllegalArgumentException("");
  50. }
  51. this.rootValue = rootValue;
  52. this.leftChild = leftChild;
  53. this.rightChild = rightChild;
  54.  
  55. System.out.println("in a tree with children whose root value is" + rootValue);
  56. }
  57.  
  58. /**
  59. * Check to see if the tree is empty.
  60. *
  61. * @param
  62. */
  63. public boolean isEmpty() {
  64. return rootValue == null;
  65. }
  66. /**
  67. * Count how many subtrees are in the tree.
  68. *
  69. * @param
  70. */
  71. public int numberOfNodes() {
  72. int count = 0;
  73. if(isEmpty()) {
  74. return count;
  75. }
  76. if(rootValue!= null) {
  77. count ++;
  78. }
  79. if (getLeftChild () != null) {
  80. count += this.getLeftChild().numberOfNodes();
  81. }
  82. if (getRightChild() != null) {
  83. count += this.getRightChild().numberOfNodes();
  84. }
  85. return count;
  86. }
  87.  
  88. /**
  89. * Check and see if the node is a leaf
  90. *
  91. * @return
  92. */
  93. public boolean isLeaf() {
  94. /*if(root.isEmpty()) {
  95. throw new NullPointerException("");
  96. }*/
  97. return leftChild == null && rightChild == null;
  98. }
  99.  
  100. /**
  101. * Method to get the root value of the current tree.
  102. *
  103. * @return rootValue
  104. */
  105. public Number getValue() {
  106. this.rootValue = rootValue;
  107. if(rootValue == null) {
  108. throw new NullPointerException("");
  109. }
  110. return rootValue;
  111. }
  112.  
  113. /**
  114. * Method to get the left child of the current tree.
  115. *
  116. * @return leftChild
  117. */
  118. public NumericBinaryTree getLeftChild(){
  119. if (leftChild.isEmpty()) {
  120. throw new NullPointerException("");
  121. }
  122. return leftChild;
  123. }
  124. /**
  125. * Method to get the left child of the current tree.
  126. *
  127. * @return righChild
  128. */
  129. public NumericBinaryTree getRightChild(){
  130. // if (rightChild.isEmpty()) {
  131. // throw new NullPointerException("");
  132. // }
  133. return rightChild;
  134. }
  135.  
  136. /**
  137. * Method to set the left child of the current tree.
  138. *
  139. * @param child
  140. */
  141. public void setLeftChild(NumericBinaryTree child) {
  142. leftChild = child;
  143. if ( rootValue==null) {
  144. throw new NullPointerException("");
  145. }
  146. }
  147.  
  148. /**
  149. * Method to set the left child of the current tree.
  150. *
  151. * @param child
  152. */
  153. public void setRightChild(NumericBinaryTree child) {
  154. rightChild = child;
  155. if ( rootValue == null) {
  156. throw new NullPointerException("");
  157. }
  158. }
  159.  
  160. /**
  161. * Find how tall this tree is.
  162. *
  163. * @return height of tree;
  164. */
  165. public int numberOfLeaves() {
  166. return 0;
  167. }
  168. public final int height(NumericBinaryTree t) {
  169. if (t == null) {
  170. return -1;
  171. }
  172. return Math.max(height(t.leftChild), height(t.rightChild)) +1;
  173. }
  174. /** Determine if two trees are equal to each other
  175. *
  176. * @param Object
  177. * @return boolean
  178. * @Override
  179. */
  180.  
  181. public boolean equals(Object obj) {
  182. if(obj == null || rootValue == null) {
  183. return false;
  184. }
  185. NumericBinaryTree compareTree = (NumericBinaryTree) obj;
  186. return rootValue.equals(compareTree.getValue())
  187. && leftChild.getValue().equals(compareTree.getValue());
  188.  
  189.  
  190. }
  191.  
  192. /** Method to return a hash code value for this tree
  193. *
  194. * @return hashValue
  195. * @Override hashCode
  196. */
  197. public int hashCode(){
  198. int hashValue = rootValue.hashCode();
  199. if (leftChild!= null) {
  200. hashValue += 2 * leftChild.hashCode();
  201. }
  202. if (rightChild!= null) {
  203. hashValue += 8 * rightChild.hashCode();
  204. }
  205. return hashValue;
  206. }
  207. /** Method to that gets a list of all values using preorder traversal.
  208. *
  209. * @return List
  210. */
  211. /* public ArrayList<Number> preOrderValues() {
  212. this.holdTheValues.add();
  213. preOrder();
  214. holdTheValues.add(rootValue);
  215. return holdTheValues;
  216. }*/
  217. public List<Number> preOrder() {
  218. holdTheValues.add(rootValue);
  219. if(rootValue== null) {
  220. return holdTheValues;
  221. }
  222. if(leftChild !=null) {
  223. leftChild.preOrder();
  224. }
  225. if(rightChild !=null) {
  226. rightChild.preOrder();
  227. }
  228.  
  229. System.out.println("here are the values" + holdTheValues);
  230. return holdTheValues;
  231.  
  232. }
  233.  
  234. }
  235.  
  236. /*
  237. * To change this license header, choose License Headers in Project Properties.
  238. * To change this template file, choose Tools | Templates
  239. * and open the template in the editor.
  240. */
  241.  
  242.  
  243. import java.util.ArrayList;
  244. import java.util.List;
  245.  
  246. public class NumericBinaryTreeTest {
  247.  
  248. /**
  249. * @param args the command line arguments
  250. */
  251. public static void main(String[] args) {
  252. Number a = 0;
  253. Number b = 1;
  254. Number c = 2;
  255. Number d = 3;
  256. List values = new ArrayList<Number>();
  257.  
  258. NumericBinaryTree t1 = new NumericBinaryTree(a);
  259. NumericBinaryTree t2 = new NumericBinaryTree(c);
  260. NumericBinaryTree t3 = new NumericBinaryTree(c);
  261. NumericBinaryTree t4 = new NumericBinaryTree(c);
  262. NumericBinaryTree t5 = new NumericBinaryTree(c);
  263. NumericBinaryTree t6 = new NumericBinaryTree(b);
  264. NumericBinaryTree t = new NumericBinaryTree(a,t1,t2);
  265. NumericBinaryTree tree = new NumericBinaryTree(a,t1,t2);
  266.  
  267. t2.setLeftChild(t4);
  268. t2.setLeftChild(t4);
  269. t1.setLeftChild(t3);
  270. t4.setLeftChild(t6);
  271. //System.out.println(t.equals(t1));
  272. System.out.println("height " + t.height(t));
  273. System.out.println(t.hashCode());
  274. System.out.println(t6.hashCode());
  275. values = tree.preOrder();
  276. }
  277.  
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement