123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- /**
- * LinkedBinarySearchTree implements the BinarySearchTreeADT interface
- * with links.
- *
- * Heavilyly modified by me to work with an AVL tree implementation of a dictionary.
- *
- * @author Dr. Lewis
- * @author Dr. Chase
- * @version 1.0, 8/19/08
- */
- public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T> implements BinarySearchTreeADT<T> {
- /**
- * Creates an empty binary search tree.
- */
- public LinkedBinarySearchTree() {
- super();
- }
-
- /**
- * Creates a binary search with the specified key/element pair as its root.
- *
- * @param key, element: the key/element pair that will be the root of the
- * new binary search tree
- */
- public LinkedBinarySearchTree (T key, T element) {
- super (key, element);
- }
- /**
- * Adds the specified object to the binary search tree in the
- * appropriate position according to its key value. Note that
- * equal elements are added to the right.
- *
- * @param key, element: the key of the element to be added to the binary
- * search tree
- */
- public void addElement (Node<T> element) {
- Comparable<T> comparableKey = (Comparable<T>)element.getKey();
- if (isEmpty())
- root = element;
- else {
- Node<T> current = root;
- boolean added = false;
- while (!added) {
- if (comparableKey.compareTo(current.getKey()) < 0) {
- if (current.left == null) {
- current.setLeft(element);
- current.getLeft().setParent(current);
- added = true;
- } else {
- current = current.left;
- }
- } else {
- if (current.right == null) {
- current.setRight(element);
- current.getRight().setParent(current);
- added = true;
- } else {
- current = current.right;
- }
- }
- }
- }
-
- count++;
- }
-
- /**
- * Removes the first element that matches the specified target
- * element from the binary search tree and returns a reference to
- * it. Throws a ElementNotFoundException if the specified target
- * element is not found in the binary search tree.
- *
- * @param targetElement the element being sought in the binary
- * search tree
- * @throws ElementNotFoundException if an element not found exception occurs
- */
- public T removeElement (T targetElement) throws ElementNotFoundException {
- T result = null;
- if (!isEmpty()) {
- if (((Comparable)targetElement).equals(root.getKey())) {
- result = root.getKey();
- root = replacement(root);
- count--;
- } else {
- Node<T> current, parent = root;
- boolean found = false;
- if (((Comparable)targetElement).compareTo(root.getKey()) < 0)
- current = root.getLeft();
- else
- current = root.getRight();
- while (current != null && !found) {
- if (targetElement.equals(current.getKey())) {
- found = true;
- count--;
- result = current.getKey();
-
- if (current == parent.getLeft()) {
- parent.setLeft(replacement(current));
- } else
- parent.setRight(replacement(current));
- } else {
- parent = current;
-
- if (((Comparable)targetElement).compareTo(current.getKey()) < 0)
- current = current.getLeft();
- else
- current = current.getRight();
- }
- } // End while
-
- if (!found)
- throw new ElementNotFoundException("binary search tree");
- }
- }
- return result;
- }
- /**
- * Removes elements that match the specified target element from
- * the binary search tree. Throws a ElementNotFoundException if
- * the sepcified target element is not found in this tree.
- *
- * @param targetElement the element being sought in the binary \
- * search tree
- * @throws ElementNotFoundException if an element not found exception occurs
- */
- public void removeAllOccurrences (T targetElement) throws ElementNotFoundException {
- removeElement(targetElement);
-
- try {
- while (contains( (T) targetElement))
- removeElement(targetElement);
- } catch (Exception ElementNotFoundException) {
- }
- }
- /**
- * Removes the node with the least value from the binary search
- * tree and returns a reference to its element. Throws an
- * EmptyBinarySearchTreeException if this tree is empty.
- *
- * @return a reference to the node with the least
- * value
- * @throws EmptyCollectionException if an empty collection exception occurs
- */
- public T removeMin() throws EmptyCollectionException {
- T result = null;
- if (isEmpty())
- throw new EmptyCollectionException ("binary search tree");
- else {
- if (root.left == null) {
- result = root.element;
- root = root.right;
- } else {
- Node<T> parent = root;
- Node<T> current = root.left;
- while (current.left != null) {
- parent = current;
- current = current.left;
- }
- result = current.element;
- parent.left = current.right;
- }
- count--;
- }
- return result;
- }
- /**
- * Removes the node with the highest value from the binary
- * search tree and returns a reference to its element. Throws an
- * EmptyBinarySearchTreeException if this tree is empty.
- *
- * @return a reference to the node with the highest value
- * @throws EmptyCollectionException if an empty collection exception occurs
- */
- public T removeMax() throws EmptyCollectionException {
- T result = null;
- if (isEmpty())
- throw new EmptyCollectionException ("binary search tree");
- else {
- if (root.right == null) {
- result = root.element;
- root = root.left;
- } else {
- Node<T> parent = root;
- Node<T> current = root.right;
- while (current.right != null) {
- parent = current;
- current = current.right;
- }
- result = current.element;
- parent.right = current.left;
- }
- count--;
- }
- return result;
- }
- /**
- * Returns the element with the least value in the binary search
- * tree. It does not remove the node from the binary search tree.
- * Throws an EmptyBinarySearchTreeException if this tree is empty.
- *
- * @return the element with the least value
- * @throws EmptyCollectionException if an empty collection exception occurs
- */
- public T findMin() throws EmptyCollectionException {
- T result = null;
- if (isEmpty())
- throw new EmptyCollectionException ("binary search tree");
- else {
- if (root.left == null)
- result = root.element;
- else {
- Node<T> current = root.left;
- while (current.left != null)
- current = current.left;
- result = current.element;
- }
- }
- return result;
- }
- /**
- * Returns the element with the highest value in the binary
- * search tree. It does not remove the node from the binary
- * search tree. Throws an EmptyBinarySearchTreeException if this
- * tree is empty.
- *
- * @return the element with the highest value
- * @throws EmptyCollectionException if an empty collection exception occurs
- */
- public T findMax() throws EmptyCollectionException {
- T result = null;
- if (isEmpty())
- throw new EmptyCollectionException ("binary search tree");
- else {
- if (root.right == null)
- result = root.element;
- else {
- Node<T> current = root.right;
- while (current.right != null)
- current = current.right;
- result = current.element;
- }
- }
- return result;
- }
- /**
- * Returns a reference to the specified target element if it is
- * found in the binary tree. Throws a NoSuchElementException if
- * the specified target element is not found in this tree.
- *
- * @param targetElement the element being sough in the binary tree
- * @throws ElementNotFoundException if an element not found exception occurs
- */
- public Node<T> find (T targetElement) throws ElementNotFoundException {
- Node<T> current = root;
-
- if (isEmpty())
- throw new ElementNotFoundException("binary search tree");
- else {
- boolean found = false;
- while (current != null && !found) {
- if (((Comparable)targetElement).equals(current.getKey()))
- found = true;
- else if (((Comparable)targetElement).compareTo(current.getKey())<0)
- current = current.left;
- else
- current = current.right;
- }
- if (!found)
- throw new ElementNotFoundException("binary search tree");
- }
- return current;
- }
- /**
- * Returns a reference to the specified target element if it is
- * found in this tree.
- *
- * @param targetElement the element being sought in the tree
- * @param next the tree node to being searching on
- */
- private Node<T> findAgain (T targetElement, Node<T> next) {
- Node<T> result = null;
-
- if (isEmpty())
- throw new ElementNotFoundException("binary search tree");
- else {
- Node<T> current = next;
- boolean found = false;
-
- while (current != null && !found) {
- if (((Comparable)targetElement).equals(current.element)) {
- result = current;
- found = true;
- }
- else if (((Comparable)targetElement).compareTo(current.element)<0)
- current = current.left;
- else
- current = current.right;
- }
- if (!found)
- throw new ElementNotFoundException("binary search tree");
- }
- return result;
- }
- /**
- * Returns a reference to a node that will replace the one
- * specified for removal. In the case where the removed node has
- * two children, the inorder successor is used as its replacement.
- *
- * @param node the node to be removeed
- * @return a reference to the replacing node
- */
- protected Node<T> replacement (Node<T> node) {
- Node<T> result = null;
- if ((node.left == null)&&(node.right==null))
- result = null;
-
- else if ((node.left != null)&&(node.right==null))
- result = node.left;
-
- else if ((node.left == null)&&(node.right != null))
- result = node.right;
-
- else {
- Node<T> current = node.right;
- Node<T> parent = node;
-
- while (current.left != null) {
- parent = current;
- current = current.left;
- }
-
- if (node.right == current)
- current.left = node.left;
- else {
- parent.left = current.right;
- current.right = node.right;
- current.left = node.left;
- }
- result = current;
- }
- return result;
- }
- }
|