LinkedBinarySearchTree.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. * LinkedBinarySearchTree implements the BinarySearchTreeADT interface
  3. * with links.
  4. *
  5. * Heavilyly modified by me to work with an AVL tree implementation of a dictionary.
  6. *
  7. * @author Dr. Lewis
  8. * @author Dr. Chase
  9. * @version 1.0, 8/19/08
  10. */
  11. public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T> implements BinarySearchTreeADT<T> {
  12. /**
  13. * Creates an empty binary search tree.
  14. */
  15. public LinkedBinarySearchTree() {
  16. super();
  17. }
  18. /**
  19. * Creates a binary search with the specified key/element pair as its root.
  20. *
  21. * @param key, element: the key/element pair that will be the root of the
  22. * new binary search tree
  23. */
  24. public LinkedBinarySearchTree (T key, T element) {
  25. super (key, element);
  26. }
  27. /**
  28. * Adds the specified object to the binary search tree in the
  29. * appropriate position according to its key value. Note that
  30. * equal elements are added to the right.
  31. *
  32. * @param key, element: the key of the element to be added to the binary
  33. * search tree
  34. */
  35. public void addElement (Node<T> element) {
  36. Comparable<T> comparableKey = (Comparable<T>)element.getKey();
  37. if (isEmpty())
  38. root = element;
  39. else {
  40. Node<T> current = root;
  41. boolean added = false;
  42. while (!added) {
  43. if (comparableKey.compareTo(current.getKey()) < 0) {
  44. if (current.left == null) {
  45. current.setLeft(element);
  46. current.getLeft().setParent(current);
  47. added = true;
  48. } else {
  49. current = current.left;
  50. }
  51. } else {
  52. if (current.right == null) {
  53. current.setRight(element);
  54. current.getRight().setParent(current);
  55. added = true;
  56. } else {
  57. current = current.right;
  58. }
  59. }
  60. }
  61. }
  62. count++;
  63. }
  64. /**
  65. * Removes the first element that matches the specified target
  66. * element from the binary search tree and returns a reference to
  67. * it. Throws a ElementNotFoundException if the specified target
  68. * element is not found in the binary search tree.
  69. *
  70. * @param targetElement the element being sought in the binary
  71. * search tree
  72. * @throws ElementNotFoundException if an element not found exception occurs
  73. */
  74. public T removeElement (T targetElement) throws ElementNotFoundException {
  75. T result = null;
  76. if (!isEmpty()) {
  77. if (((Comparable)targetElement).equals(root.getKey())) {
  78. result = root.getKey();
  79. root = replacement(root);
  80. count--;
  81. } else {
  82. Node<T> current, parent = root;
  83. boolean found = false;
  84. if (((Comparable)targetElement).compareTo(root.getKey()) < 0)
  85. current = root.getLeft();
  86. else
  87. current = root.getRight();
  88. while (current != null && !found) {
  89. if (targetElement.equals(current.getKey())) {
  90. found = true;
  91. count--;
  92. result = current.getKey();
  93. if (current == parent.getLeft()) {
  94. parent.setLeft(replacement(current));
  95. } else
  96. parent.setRight(replacement(current));
  97. } else {
  98. parent = current;
  99. if (((Comparable)targetElement).compareTo(current.getKey()) < 0)
  100. current = current.getLeft();
  101. else
  102. current = current.getRight();
  103. }
  104. } // End while
  105. if (!found)
  106. throw new ElementNotFoundException("binary search tree");
  107. }
  108. }
  109. return result;
  110. }
  111. /**
  112. * Removes elements that match the specified target element from
  113. * the binary search tree. Throws a ElementNotFoundException if
  114. * the sepcified target element is not found in this tree.
  115. *
  116. * @param targetElement the element being sought in the binary \
  117. * search tree
  118. * @throws ElementNotFoundException if an element not found exception occurs
  119. */
  120. public void removeAllOccurrences (T targetElement) throws ElementNotFoundException {
  121. removeElement(targetElement);
  122. try {
  123. while (contains( (T) targetElement))
  124. removeElement(targetElement);
  125. } catch (Exception ElementNotFoundException) {
  126. }
  127. }
  128. /**
  129. * Removes the node with the least value from the binary search
  130. * tree and returns a reference to its element. Throws an
  131. * EmptyBinarySearchTreeException if this tree is empty.
  132. *
  133. * @return a reference to the node with the least
  134. * value
  135. * @throws EmptyCollectionException if an empty collection exception occurs
  136. */
  137. public T removeMin() throws EmptyCollectionException {
  138. T result = null;
  139. if (isEmpty())
  140. throw new EmptyCollectionException ("binary search tree");
  141. else {
  142. if (root.left == null) {
  143. result = root.element;
  144. root = root.right;
  145. } else {
  146. Node<T> parent = root;
  147. Node<T> current = root.left;
  148. while (current.left != null) {
  149. parent = current;
  150. current = current.left;
  151. }
  152. result = current.element;
  153. parent.left = current.right;
  154. }
  155. count--;
  156. }
  157. return result;
  158. }
  159. /**
  160. * Removes the node with the highest value from the binary
  161. * search tree and returns a reference to its element. Throws an
  162. * EmptyBinarySearchTreeException if this tree is empty.
  163. *
  164. * @return a reference to the node with the highest value
  165. * @throws EmptyCollectionException if an empty collection exception occurs
  166. */
  167. public T removeMax() throws EmptyCollectionException {
  168. T result = null;
  169. if (isEmpty())
  170. throw new EmptyCollectionException ("binary search tree");
  171. else {
  172. if (root.right == null) {
  173. result = root.element;
  174. root = root.left;
  175. } else {
  176. Node<T> parent = root;
  177. Node<T> current = root.right;
  178. while (current.right != null) {
  179. parent = current;
  180. current = current.right;
  181. }
  182. result = current.element;
  183. parent.right = current.left;
  184. }
  185. count--;
  186. }
  187. return result;
  188. }
  189. /**
  190. * Returns the element with the least value in the binary search
  191. * tree. It does not remove the node from the binary search tree.
  192. * Throws an EmptyBinarySearchTreeException if this tree is empty.
  193. *
  194. * @return the element with the least value
  195. * @throws EmptyCollectionException if an empty collection exception occurs
  196. */
  197. public T findMin() throws EmptyCollectionException {
  198. T result = null;
  199. if (isEmpty())
  200. throw new EmptyCollectionException ("binary search tree");
  201. else {
  202. if (root.left == null)
  203. result = root.element;
  204. else {
  205. Node<T> current = root.left;
  206. while (current.left != null)
  207. current = current.left;
  208. result = current.element;
  209. }
  210. }
  211. return result;
  212. }
  213. /**
  214. * Returns the element with the highest value in the binary
  215. * search tree. It does not remove the node from the binary
  216. * search tree. Throws an EmptyBinarySearchTreeException if this
  217. * tree is empty.
  218. *
  219. * @return the element with the highest value
  220. * @throws EmptyCollectionException if an empty collection exception occurs
  221. */
  222. public T findMax() throws EmptyCollectionException {
  223. T result = null;
  224. if (isEmpty())
  225. throw new EmptyCollectionException ("binary search tree");
  226. else {
  227. if (root.right == null)
  228. result = root.element;
  229. else {
  230. Node<T> current = root.right;
  231. while (current.right != null)
  232. current = current.right;
  233. result = current.element;
  234. }
  235. }
  236. return result;
  237. }
  238. /**
  239. * Returns a reference to the specified target element if it is
  240. * found in the binary tree. Throws a NoSuchElementException if
  241. * the specified target element is not found in this tree.
  242. *
  243. * @param targetElement the element being sough in the binary tree
  244. * @throws ElementNotFoundException if an element not found exception occurs
  245. */
  246. public Node<T> find (T targetElement) throws ElementNotFoundException {
  247. Node<T> current = root;
  248. if (isEmpty())
  249. throw new ElementNotFoundException("binary search tree");
  250. else {
  251. boolean found = false;
  252. while (current != null && !found) {
  253. if (((Comparable)targetElement).equals(current.getKey()))
  254. found = true;
  255. else if (((Comparable)targetElement).compareTo(current.getKey())<0)
  256. current = current.left;
  257. else
  258. current = current.right;
  259. }
  260. if (!found)
  261. throw new ElementNotFoundException("binary search tree");
  262. }
  263. return current;
  264. }
  265. /**
  266. * Returns a reference to the specified target element if it is
  267. * found in this tree.
  268. *
  269. * @param targetElement the element being sought in the tree
  270. * @param next the tree node to being searching on
  271. */
  272. private Node<T> findAgain (T targetElement, Node<T> next) {
  273. Node<T> result = null;
  274. if (isEmpty())
  275. throw new ElementNotFoundException("binary search tree");
  276. else {
  277. Node<T> current = next;
  278. boolean found = false;
  279. while (current != null && !found) {
  280. if (((Comparable)targetElement).equals(current.element)) {
  281. result = current;
  282. found = true;
  283. }
  284. else if (((Comparable)targetElement).compareTo(current.element)<0)
  285. current = current.left;
  286. else
  287. current = current.right;
  288. }
  289. if (!found)
  290. throw new ElementNotFoundException("binary search tree");
  291. }
  292. return result;
  293. }
  294. /**
  295. * Returns a reference to a node that will replace the one
  296. * specified for removal. In the case where the removed node has
  297. * two children, the inorder successor is used as its replacement.
  298. *
  299. * @param node the node to be removeed
  300. * @return a reference to the replacing node
  301. */
  302. protected Node<T> replacement (Node<T> node) {
  303. Node<T> result = null;
  304. if ((node.left == null)&&(node.right==null))
  305. result = null;
  306. else if ((node.left != null)&&(node.right==null))
  307. result = node.left;
  308. else if ((node.left == null)&&(node.right != null))
  309. result = node.right;
  310. else {
  311. Node<T> current = node.right;
  312. Node<T> parent = node;
  313. while (current.left != null) {
  314. parent = current;
  315. current = current.left;
  316. }
  317. if (node.right == current)
  318. current.left = node.left;
  319. else {
  320. parent.left = current.right;
  321. current.right = node.right;
  322. current.left = node.left;
  323. }
  324. result = current;
  325. }
  326. return result;
  327. }
  328. }