Advertisement
Guest User

AnimalNode.java

a guest
Apr 9th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1.  
  2. public class AnimalNode<E>
  3. {
  4. private E data;
  5. private AnimalNode<E> left, right;
  6. public AnimalNode(E initialData, AnimalNode<E> initialLeft, AnimalNode<E> initialRight)
  7. {
  8. data = initialData;
  9. left = initialLeft;
  10. right = initialRight;
  11. }
  12.  
  13. public E getData()
  14. {
  15. return data;
  16. }
  17. //left of node
  18. public AnimalNode<E> getLeft()
  19. {
  20. return left;
  21. }
  22. //return farthest left of node
  23. public E getLeftmostData()
  24. {
  25. if (left == null)
  26. return data;
  27. else
  28. return left.getLeftmostData();
  29. }
  30. //return right of node
  31. public AnimalNode<E> getRight()
  32. {
  33. return right;
  34. }
  35. //return farthest right of node
  36. public E getRightmostData()
  37. {
  38. if (left == null)
  39. return data;
  40. else
  41. return left.getRightmostData();
  42. }
  43.  
  44.  
  45. //print data below node
  46. public void inorderPrint()
  47. {
  48. if (left != null)
  49. left.inorderPrint();
  50. System.out.println(data);
  51. if (right != null)
  52. right.inorderPrint();
  53. }
  54. //find if node is a leaf
  55. public boolean isLeaf( )
  56. {
  57. return (left == null) && (right == null);
  58. }
  59.  
  60. //print data below or at node
  61. public void preorderPrint( )
  62. {
  63. System.out.println(data);
  64. if (left != null)
  65. left.preorderPrint( );
  66. if (right != null)
  67. right.preorderPrint( );
  68. }
  69.  
  70. public void postorderPrint( )
  71. {
  72. if (left != null)
  73. left.postorderPrint( );
  74. if (right != null)
  75. right.postorderPrint( );
  76. System.out.println(data);
  77. }
  78. //0 as RootOfTree and 1 for children, etc.
  79. public void print(int depth)
  80. {
  81. int i;
  82.  
  83. // Print the indentation and the data from the current node:
  84. for (i = 1; i <= depth; i++)
  85. System.out.print(" ");
  86. System.out.println(data);
  87.  
  88. // Print the left subtree (or a dash if there is a right child and no left child)
  89. if (left != null)
  90. left.print(depth+1);
  91. else if (right != null)
  92. {
  93. for (i = 1; i <= depth+1; i++)
  94. System.out.print(" ");
  95. System.out.println("--");
  96. }
  97.  
  98. // Print the right subtree (or a dash if there is a left child and no left child)
  99. if (right != null)
  100. right.print(depth+1);
  101. else if (left != null)
  102. {
  103. for (i = 1; i <= depth+1; i++)
  104. System.out.print(" ");
  105. System.out.println("--");
  106. }
  107. }
  108. //remove farthest node on left
  109. public AnimalNode<E> removeLeftmost( )
  110. {
  111. if (left == null)
  112. return right;
  113. else
  114. {
  115. left = left.removeLeftmost( );
  116. return this;
  117. }
  118. }
  119.  
  120. //remove farthest right node
  121. public AnimalNode<E> removeRightmost( )
  122. {
  123. if (right == null)
  124. return left;
  125. else
  126. {
  127. right = right.removeRightmost( );
  128. return this;
  129. }
  130. }
  131. public void setData(E newData)
  132. {
  133. data = newData;
  134. }
  135.  
  136. //new left child
  137. public void setLeft(AnimalNode<E> newLeft)
  138. {
  139. left = newLeft;
  140. }
  141. //new right child
  142. public void setRight(AnimalNode<E> newRight)
  143. {
  144. right = newRight;
  145. }
  146. //copy all of tree
  147. public static <E> AnimalNode<E> CopyTree(AnimalNode<E> source)
  148. {
  149. AnimalNode<E> leftCopy, rightCopy;
  150.  
  151. if (source == null)
  152. return null;
  153. else
  154. {
  155. leftCopy = CopyTree(source.left);
  156. rightCopy = CopyTree(source.right);
  157. return new AnimalNode<E>(source.data, leftCopy, rightCopy);
  158. }
  159. }
  160.  
  161. //keep a count of the nodes in the tree
  162. public static <E> long treeSize(AnimalNode<E> RootOfTree)
  163. {
  164. if (RootOfTree == null)
  165. return 0;
  166. else
  167. return 1 + treeSize(RootOfTree.left) + treeSize(RootOfTree.right);
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement