LinkedBinaryTree.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * LinkedBinaryTree implements the BinaryTreeADT interface
  3. *
  4. * @author Dr. Lewis
  5. * @author Dr. Chase
  6. * @version 1.0, 8/19/08
  7. */
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. public class LinkedBinaryTree<T> implements BinaryTreeADT<T> {
  11. protected int count;
  12. protected Node<T> root;
  13. /**
  14. * Creates an empty binary tree.
  15. */
  16. public LinkedBinaryTree() {
  17. count = 0;
  18. root = null;
  19. }
  20. /**
  21. * Creates a binary tree with the specified element as its root.
  22. *
  23. * @param element the element that will become the root of the new binary
  24. * tree
  25. */
  26. public LinkedBinaryTree (T key, T element) {
  27. count = 1;
  28. root = new Node<T> (key, element);
  29. }
  30. /**
  31. * Returns a reference to the element at the root
  32. *
  33. * @return a reference to the specified target
  34. * @throws EmptyCollectionException if the tree is empty
  35. */
  36. public T getRoot() throws EmptyCollectionException {
  37. return root.element;
  38. }
  39. /**
  40. * Returns true if this binary tree is empty and false otherwise.
  41. *
  42. * @return true if this binary tree is empty
  43. */
  44. public boolean isEmpty() {
  45. return (root == null);
  46. }
  47. /**
  48. * Returns the integer size of this tree.
  49. *
  50. * @return the integer size of this tree
  51. */
  52. public int size() {
  53. return count;
  54. }
  55. /**
  56. * Returns true if this tree contains an element that matches the
  57. * specified target element and false otherwise.
  58. *
  59. * @param targetElement the element being sought in this tree
  60. * @return true if the element in is this tree
  61. * @throws ElementNotFoundException if an element not found exception occurs
  62. */
  63. public boolean contains (T targetElement) {
  64. try {
  65. Node<T> current = new Node<T> (targetElement, null);
  66. current = find(current.getKey());
  67. return true;
  68. }
  69. catch (ElementNotFoundException e) {
  70. return false;
  71. }
  72. }
  73. /**
  74. * Returns a reference to the specified target element if it is
  75. * found in this binary tree. Throws a NoSuchElementException if
  76. * the specified target element is not found in the binary tree.
  77. *
  78. * @param targetElement the element being sought in this tree
  79. * @return a reference to the specified target
  80. * @throws ElementNotFoundException if an element not found exception occurs
  81. */
  82. public Node<T> find(T targetElement) throws ElementNotFoundException {
  83. Node<T> current = findAgain(targetElement, root);
  84. if( current == null )
  85. throw new ElementNotFoundException("binary tree");
  86. return (current);
  87. }
  88. /**
  89. * Returns a reference to the specified target element if it is
  90. * found in this binary tree.
  91. *
  92. * @param targetElement the element being sought in this tree
  93. * @param next the element to begin searching from
  94. */
  95. private Node<T> findAgain (T targetElement, Node<T> next) {
  96. if (next == null)
  97. return null;
  98. if (next.element.equals(targetElement))
  99. return next;
  100. Node<T> temp = findAgain(targetElement, next.getLeft());
  101. if (temp == null)
  102. temp = findAgain(targetElement, next.getRight());
  103. return temp;
  104. }
  105. /**
  106. * Returns a string representation of this binary tree.
  107. *
  108. * @return a string representation of this binary tree
  109. */
  110. public String toString() {
  111. String result = "";
  112. Iterator<T> iter = iteratorLevelOrder();
  113. while (iter.hasNext()){
  114. result += (iter.next() + "\n");
  115. }
  116. return result;
  117. }
  118. /**
  119. * Performs an inorder traversal on this binary tree by calling an
  120. * overloaded, recursive inorder method that starts with
  121. * the root.
  122. *
  123. * @return an in order iterator over this binary tree
  124. */
  125. public Iterator<T> iteratorInOrder() {
  126. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  127. inorder (root, tempList);
  128. return tempList.iterator();
  129. }
  130. /**
  131. * Performs a recursive inorder traversal.
  132. *
  133. * @param node the node to be used as the root for this traversal
  134. * @param tempList the temporary list for use in this traversal
  135. */
  136. protected void inorder (Node<T> node, ArrayUnorderedList<T> tempList) {
  137. if (node != null) {
  138. String out = " " + node.toString();
  139. inorder (node.getLeft(), tempList);
  140. tempList.addToRear((T)out);
  141. inorder (node.getRight(), tempList);
  142. }
  143. }
  144. /**
  145. * Performs an preorder traversal on this binary tree by calling
  146. * an overloaded, recursive preorder method that starts with
  147. * the root.
  148. *
  149. * @return an pre order iterator over this tree
  150. */
  151. public Iterator<T> iteratorPreOrder() {
  152. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  153. preorder (root, tempList);
  154. return tempList.iterator();
  155. }
  156. /**
  157. * Performs a recursive preorder traversal.
  158. *
  159. * @param node the node to be used as the root for this traversal
  160. * @param tempList the temporary list for use in this traversal
  161. */
  162. protected void preorder (Node<T> node, ArrayUnorderedList<T> tempList) {
  163. if (node != null) {
  164. tempList.addToRear(node.element);
  165. preorder (node.getLeft(), tempList);
  166. preorder (node.getRight(), tempList);
  167. }
  168. }
  169. /**
  170. * Performs an postorder traversal on this binary tree by calling
  171. * an overloaded, recursive postorder method that starts
  172. * with the root.
  173. *
  174. * @return a post order iterator over this tree
  175. */
  176. public Iterator<T> iteratorPostOrder() {
  177. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  178. postorder (root, tempList);
  179. return tempList.iterator();
  180. }
  181. /**
  182. * Performs a recursive postorder traversal.
  183. *
  184. * @param node the node to be used as the root for this traversal
  185. * @param tempList the temporary list for use in this traversal
  186. */
  187. protected void postorder (Node<T> node, ArrayUnorderedList<T> tempList) {
  188. if (node != null) {
  189. postorder (node.getLeft(), tempList);
  190. postorder (node.getRight(), tempList);
  191. tempList.addToRear(node.element);
  192. }
  193. }
  194. /**
  195. * Performs a levelorder traversal on this binary tree, using a
  196. * templist.
  197. *
  198. * @return a levelorder iterator over this binary tree
  199. */
  200. public Iterator<T> iteratorLevelOrder() {
  201. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  202. LinkedList<Node> queue = new LinkedList<Node>();
  203. Node<T> node;
  204. queue.addLast(root);
  205. while (!queue.isEmpty()) {
  206. node = queue.removeFirst();
  207. if (node!= null) {
  208. tempList.addToRear(node.element);
  209. queue.addLast(node.getLeft());
  210. queue.addLast(node.getRight());
  211. }// end if node <> null
  212. else
  213. tempList.addToRear(null);
  214. }//end while
  215. return tempList.iterator();
  216. }
  217. }