Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.16 KB | None | 0 0
  1. //**********************************************************
  2. //Program Name: LinkedBinaryTree.java
  3. //Author : Jason Hargrave
  4. //Date Written : 10 Apr 2014
  5. //Class : CSC 205AA
  6. //**********************************************************
  7.  
  8. package jsjf;
  9.  
  10. import java.util.*;
  11.  
  12. import jsjf.exceptions.*;
  13.  
  14. /**
  15. * LinkedBinaryTree implements the BinaryTreeADT interface
  16. *
  17. * @author Java Foundations
  18. * @version 4.0
  19. */
  20. public class LinkedBinaryTree<T> implements BinaryTreeADT<T>, Iterable<T>
  21. {
  22. protected BinaryTreeNode<T> root;
  23. protected int modCount;
  24.  
  25. /**
  26. * Creates an empty binary tree.
  27. */
  28. public LinkedBinaryTree()
  29. {
  30. root = null;
  31. }
  32.  
  33. /**
  34. * Creates a binary tree with the specified element as its root.
  35. *
  36. * @param element the element that will become the root of the binary tree
  37. */
  38. public LinkedBinaryTree(T element)
  39. {
  40. root = new BinaryTreeNode<T>(element);
  41. }
  42.  
  43. /**
  44. * Creates a binary tree with the specified element as its root and the
  45. * given trees as its left child and right child
  46. *
  47. * @param element the element that will become the root of the binary tree
  48. * @param left the left subtree of this tree
  49. * @param right the right subtree of this tree
  50. */
  51. public LinkedBinaryTree(T element, LinkedBinaryTree<T> left,
  52. LinkedBinaryTree<T> right)
  53. {
  54. // root = new BinaryTreeNode<T>(element);
  55. // root.setLeft(left.root);
  56. // root.setRight(right.root);
  57. root = new BinaryTreeNode<T>(element);
  58. if(left != null){
  59. root.setLeft(left.root);
  60. }
  61. else
  62. root.setLeft(null);
  63.  
  64. if(right != null){
  65. root.setRight(right.root);
  66. }
  67. else
  68. root.setRight(null);
  69. }
  70.  
  71. /**
  72. * Returns a reference to the element at the root
  73. *
  74. * @return a reference to the specified target
  75. * @throws EmptyCollectionException if the tree is empty
  76. */
  77. public T getRootElement() throws EmptyCollectionException
  78. {
  79. if (isEmpty())
  80. throw new EmptyCollectionException("LinkedBinaryTree getRootElement");
  81.  
  82. return this.root.getElement();
  83. }
  84.  
  85. /**
  86. * Returns a reference to the node at the root
  87. *
  88. * @return a reference to the specified node
  89. * @throws EmptyCollectionException if the tree is empty
  90. */
  91. protected BinaryTreeNode<T> getRootNode() throws EmptyCollectionException
  92. {
  93. if (isEmpty())
  94. throw new EmptyCollectionException("LinkedBinaryTree getRootNode");
  95.  
  96. return this.root;
  97. }
  98.  
  99. /**
  100. * Returns the left subtree of the root of this tree.
  101. *
  102. * @return a link to the left subtree fo the tree
  103. */
  104. public LinkedBinaryTree<T> getLeft()
  105. {
  106. LinkedBinaryTree<T> ret = new LinkedBinaryTree<T>();
  107. ret.root = this.root.getLeft();
  108.  
  109. return ret;
  110. }
  111.  
  112. /**
  113. * Returns the right subtree of the root of this tree.
  114. *
  115. * @return a link to the right subtree of the tree
  116. */
  117. public LinkedBinaryTree<T> getRight()
  118. {
  119. LinkedBinaryTree<T> ret = new LinkedBinaryTree<T>();
  120. ret.root = this.root.getRight();
  121.  
  122. return ret;
  123. }
  124.  
  125. /**
  126. * Returns true if this binary tree is empty and false otherwise.
  127. *
  128. * @return true if this binary tree is empty, false otherwise
  129. */
  130. public boolean isEmpty()
  131. {
  132. return (root == null);
  133. }
  134.  
  135. /**
  136. * Returns the integer size of this tree.
  137. *
  138. * @return the integer size of the tree
  139. */
  140. public int size()
  141. {
  142. return root.numChildren();
  143. }
  144.  
  145. /**
  146. * Returns true if this tree contains an element that matches the
  147. * specified target element and false otherwise.
  148. *
  149. * @param targetElement the element being sought in this tree
  150. * @return true if the element in is this tree, false otherwise
  151. */
  152. public boolean contains(T targetElement)
  153. {
  154. boolean found;
  155. T temp = find(targetElement);
  156.  
  157. if (temp == null)
  158. found = false;
  159. else
  160. found = true;
  161.  
  162. return found;
  163. }
  164.  
  165. /**
  166. * Returns a reference to the specified target element if it is
  167. * found in this binary tree. Throws a ElementNotFoundException if
  168. * the specified target element is not found in the binary tree.
  169. *
  170. * @param targetElement the element being sought in this tree
  171. * @return a reference to the specified target
  172. * @throws ElementNotFoundException if the element is not in the tree
  173. */
  174. public T find(T targetElement) throws ElementNotFoundException
  175. {
  176. BinaryTreeNode<T> current = findNode(targetElement, root);
  177.  
  178. if (current == null)
  179. throw new ElementNotFoundException("LinkedBinaryTree");
  180.  
  181. return (current.getElement());
  182. }
  183.  
  184. /**
  185. * Returns a reference to the specified target element if it is
  186. * found in this binary tree.
  187. *
  188. * @param targetElement the element being sought in this tree
  189. * @param next the element to begin searching from
  190. */
  191. private BinaryTreeNode<T> findNode(T targetElement,
  192. BinaryTreeNode<T> next)
  193. {
  194. if (next == null)
  195. return null;
  196.  
  197. if (next.getElement().equals(targetElement))
  198. return next;
  199.  
  200. BinaryTreeNode<T> temp = findNode(targetElement, next.getLeft());
  201.  
  202. if (temp == null)
  203. temp = findNode(targetElement, next.getRight());
  204.  
  205. return temp;
  206. }
  207.  
  208. /**
  209. * Returns a string representation of this binary tree showing
  210. * the nodes in an inorder fashion.
  211. *
  212. * @return a string representation of this binary tree
  213. */
  214. public String toString()
  215. {
  216. Iterator iter1 = iteratorInOrder();
  217. String ret = "";
  218.  
  219. while(iter1.hasNext()){
  220. ret += iter1.next();
  221. }
  222.  
  223. return ret;
  224. }
  225.  
  226. /**
  227. * Returns an iterator over the elements in this tree using the
  228. * iteratorInOrder method
  229. *
  230. * @return an in order iterator over this binary tree
  231. */
  232. public Iterator<T> iterator()
  233. {
  234. return iteratorInOrder();
  235. }
  236.  
  237. /**
  238. * Performs an inorder traversal on this binary tree by calling an
  239. * overloaded, recursive inorder method that starts with
  240. * the root.
  241. *
  242. * @return an in order iterator over this binary tree
  243. */
  244. public Iterator<T> iteratorInOrder()
  245. {
  246. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  247. inOrder(root, tempList);
  248.  
  249. return new TreeIterator(tempList.iterator());
  250. }
  251.  
  252. /**
  253. * Performs a recursive inorder traversal.
  254. *
  255. * @param node the node to be used as the root for this traversal
  256. * @param tempList the temporary list for use in this traversal
  257. */
  258. protected void inOrder(BinaryTreeNode<T> node,
  259. ArrayUnorderedList<T> tempList)
  260. {
  261. if (node != null)
  262. {
  263. inOrder(node.getLeft(), tempList);
  264. tempList.addToRear(node.getElement());
  265. inOrder(node.getRight(), tempList);
  266. }
  267. }
  268.  
  269. /**
  270. * Performs an preorder traversal on this binary tree by calling
  271. * an overloaded, recursive preorder method that starts with
  272. * the root.
  273. *
  274. * @return a pre order iterator over this tree
  275. */
  276. public Iterator<T> iteratorPreOrder()
  277. {
  278. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  279. preOrder(root, tempList);
  280.  
  281. return new TreeIterator(tempList.iterator());
  282. }
  283.  
  284. /**
  285. * Performs a recursive preorder traversal.
  286. *
  287. * @param node the node to be used as the root for this traversal
  288. * @param tempList the temporary list for use in this traversal
  289. */
  290. protected void preOrder(BinaryTreeNode<T> node,
  291. ArrayUnorderedList<T> tempList)
  292. {
  293. if (node != null){
  294. tempList.addToRear(node.getElement());
  295. preOrder(node.getLeft(), tempList);
  296. preOrder(node.getRight(), tempList);
  297. }
  298. }
  299.  
  300. /**
  301. * Performs an postorder traversal on this binary tree by calling
  302. * an overloaded, recursive postorder method that starts
  303. * with the root.
  304. *
  305. * @return a post order iterator over this tree
  306. */
  307. public Iterator<T> iteratorPostOrder()
  308. {
  309. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  310. postOrder(root, tempList);
  311.  
  312. return new TreeIterator(tempList.iterator());
  313. }
  314.  
  315. /**
  316. * Performs a recursive postorder traversal.
  317. *
  318. * @param node the node to be used as the root for this traversal
  319. * @param tempList the temporary list for use in this traversal
  320. */
  321. protected void postOrder(BinaryTreeNode<T> node,
  322. ArrayUnorderedList<T> tempList)
  323. {
  324. if (node != null){
  325. postOrder(node.getLeft(), tempList);
  326. postOrder(node.getRight(), tempList);
  327. tempList.addToRear(node.getElement());
  328. }
  329. }
  330.  
  331. /**
  332. * Performs a levelorder traversal on this binary tree, using a
  333. * templist.
  334. *
  335. * @return a levelorder iterator over this binary tree
  336. */
  337. public Iterator<T> iteratorLevelOrder()
  338. {
  339. ArrayUnorderedList<BinaryTreeNode<T>> nodes =
  340. new ArrayUnorderedList<BinaryTreeNode<T>>();
  341. ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
  342. BinaryTreeNode<T> current;
  343.  
  344. nodes.addToRear(root);
  345.  
  346. while (!nodes.isEmpty())
  347. {
  348. current = nodes.removeFirst();
  349.  
  350. if (current != null)
  351. {
  352. tempList.addToRear(current.getElement());
  353. if (current.getLeft() != null)
  354. nodes.addToRear(current.getLeft());
  355. if (current.getRight() != null)
  356. nodes.addToRear(current.getRight());
  357. }
  358. else
  359. tempList.addToRear(null);
  360. }
  361.  
  362. return new TreeIterator(tempList.iterator());
  363. }
  364.  
  365. public String returnInOrder(){
  366.  
  367. @SuppressWarnings("rawtypes")
  368. Iterator iter1 = iteratorInOrder();
  369. String ret = "";
  370.  
  371. while(iter1.hasNext()){
  372. ret += iter1.next();
  373. }
  374.  
  375. return ret;
  376. }
  377.  
  378. public String returnPreOrder(){
  379. String ret = "";
  380. @SuppressWarnings("rawtypes")
  381. Iterator iter1 = iteratorPreOrder();
  382.  
  383.  
  384. while(iter1.hasNext()){
  385. ret += iter1.next();
  386. }
  387.  
  388. return ret;
  389. }
  390.  
  391. public String returnPostOrder(){
  392. String ret = "";
  393. @SuppressWarnings("rawtypes")
  394. Iterator iter1 = iteratorPostOrder();
  395.  
  396.  
  397. while(iter1.hasNext()){
  398. ret += iter1.next();
  399. }
  400.  
  401. return ret;
  402. }
  403.  
  404. /**
  405. * Inner class to represent an iterator over the elements of this tree
  406. */
  407. private class TreeIterator implements Iterator<T>
  408. {
  409. private int expectedModCount;
  410. private Iterator<T> iter;
  411.  
  412. /**
  413. * Sets up this iterator using the specified iterator.
  414. *
  415. * @param iter the list iterator created by a tree traversal
  416. */
  417. public TreeIterator(Iterator<T> iter)
  418. {
  419. this.iter = iter;
  420. expectedModCount = modCount;
  421. }
  422.  
  423. /**
  424. * Returns true if this iterator has at least one more element
  425. * to deliver in the iteration.
  426. *
  427. * @return true if this iterator has at least one more element to deliver
  428. * in the iteration
  429. * @throws ConcurrentModificationException if the collection has changed
  430. * while the iterator is in use
  431. */
  432. public boolean hasNext() throws ConcurrentModificationException
  433. {
  434. if (!(modCount == expectedModCount))
  435. throw new ConcurrentModificationException();
  436.  
  437. return (iter.hasNext());
  438. }
  439.  
  440. /**
  441. * Returns the next element in the iteration. If there are no
  442. * more elements in this iteration, a NoSuchElementException is
  443. * thrown.
  444. *
  445. * @return the next element in the iteration
  446. * @throws NoSuchElementException if the iterator is empty
  447. */
  448. public T next() throws NoSuchElementException
  449. {
  450. if (hasNext())
  451. return (iter.next());
  452. else
  453. throw new NoSuchElementException();
  454. }
  455.  
  456. /**
  457. * The remove operation is not supported.
  458. *
  459. * @throws UnsupportedOperationException if the remove operation is called
  460. */
  461. public void remove()
  462. {
  463. throw new UnsupportedOperationException();
  464. }
  465. }
  466.  
  467.  
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement