Can you please Complete the implementaion of of the LinkedBinarySearchTree class presented in this chapter. Specifically, complete the implementations of the removeMax, findMin, findMax, getLeft, and getRight operations.
Can you make a driver class also for this code written below:
package jsjf; import jsjf.exceptions.*; /** * LinkedBinarySearchTree implements the BinarySearchTreeADT interface * with links. * * @author Java Foundations * @version 4.0 */ public class LinkedBinarySearchTree extends LinkedBinaryTree implements BinarySearchTreeADT { /** * Creates an empty binary search tree. */ public LinkedBinarySearchTree() { super(); } /** * Creates a binary search with the specified element as its root. * * @param element the element that will be the root of the new binary * search tree */ public LinkedBinarySearchTree(T element) { super(element); if (!(element instanceof Comparable)) throw new NonComparableElementException(
"LinkedBinarySearchTree"); } /** * Adds the specified object to the binary search tree in the * appropriate position according to its natural order. Note that * equal elements are added to the right. * * @param element the element to be added to the binary search tree */ public void addElement(T element) { if (!(element instanceof Comparable)) throw new NonComparableElementException("LinkedBinarySearchTree"); Comparable comparableElement = (Comparable)element; if (isEmpty()) root = new BinaryTreeNode(element); else { if (comparableElement.compareTo(root.getElement()) < 0) { if (root.getLeft() == null) this.getRootNode().setLeft(new BinaryTreeNode(element)); else addElement(element, root.getLeft()); } else { if (root.getRight() == null) this.getRootNode().setRight(new BinaryTreeNode(element)); else addElement(element, root.getRight()); } } modCount++; } /** * Adds the specified object to the binary search tree in the * appropriate position according to its natural order. Note that * equal elements are added to the right. * * @param element the element to be added to the binary search tree */ private void addElement(T element, BinaryTreeNode node) { Comparable comparableElement = (Comparable)element; if (comparableElement.compareTo(node.getElement()) < 0) { if (node.getLeft() == null) node.setLeft(new BinaryTreeNode(element)); else addElement(element, node.getLeft()); } else { if (node.getRight() == null) node.setRight(new BinaryTreeNode(element)); else addElement(element, node.getRight()); } } /** * 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 the target element is not found */ public T removeElement(T targetElement) throws ElementNotFoundException { T result = null; if (isEmpty()) throw new ElementNotFoundException("LinkedBinarySearchTree"); else { BinaryTreeNode parent = null; if (((Comparable)targetElement).equals(root.element)) { result = root.element; BinaryTreeNode temp = replacement(root); if (temp == null) root = null; else { root.element = temp.element; root.setRight(temp.right); root.setLeft(temp.left); } modCount--; } else { parent = root; if (((Comparable)targetElement).compareTo(root.element) < 0) result = removeElement(targetElement, root.getLeft(), parent); else result = removeElement(targetElement, root.getRight(), parent); } } return result; } /** * 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 * @param node the node from which to search * @param parent the parent of the node from which to search * @throws ElementNotFoundException if the target element is not found */ private T removeElement(T targetElement, BinaryTreeNode node, BinaryTreeNode parent) throws ElementNotFoundException { T result = null; if (node == null) throw new ElementNotFoundException("LinkedBinarySearchTree"); else { if (((Comparable)targetElement).equals(node.element)) { result = node.element; BinaryTreeNode temp = replacement(node); if (parent.right == node) parent.right = temp; else parent.left = temp; modCount--; } else { parent = node; if (((Comparable)targetElement).compareTo(node.element) < 0) result = removeElement(targetElement, node.getLeft(), parent); else result = removeElement(targetElement, node.getRight(), parent); } } 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 removed * @return a reference to the replacing node */ private BinaryTreeNode replacement(BinaryTreeNode node) { BinaryTreeNode 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 { BinaryTreeNode current = node.right; BinaryTreeNode parent = node; while (current.left != null) { parent = current; current = current.left; } current.left = node.left; if (node.right != current) { parent.left = current.right; current.right = node.right; } result = current; } return result; } /** * Removes elements that match the specified target element from * the binary search tree. Throws a ElementNotFoundException if * the specified target element is not found in this tree. * * @param targetElement the element being sought in the binary search tree * @throws ElementNotFoundException if the target element is not found */ 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 * EmptyCollectionException if this tree is empty. * * @return a reference to the node with the least value * @throws EmptyCollectionException if the tree is empty */ public T removeMin() throws EmptyCollectionException { T result = null; if (isEmpty()) throw new EmptyCollectionException("LinkedBinarySearchTree"); else { if (root.left == null) { result = root.element; root = root.right; } else { BinaryTreeNode parent = root; BinaryTreeNode current = root.left; while (current.left != null) { parent = current; current = current.left; } result = current.element; parent.left = current.right; } modCount--; } return result; } /** * Removes the node with the highest value from the binary * search tree and returns a reference to its element. Throws an * EmptyCollectionException if this tree is empty. * * @return a reference to the node with the highest value * @throws EmptyCollectionException if the tree is empty */ public T removeMax() throws EmptyCollectionException { // To be completed as a Programming Project return null; // temp } /** * 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 EmptyCollectionException if this tree is empty. * * @return the element with the least value * @throws EmptyCollectionException if the tree is empty */ public T findMin() throws EmptyCollectionException { // To be completed as a Programming Project return null; // temp } /** * 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 EmptyCollectionException if this * tree is empty. * * @return the element with the highest value * @throws EmptyCollectionException if the tree is empty */ public T findMax() throws EmptyCollectionException { // To be completed as a Programming Project return null; // temp } /** * Returns the left subtree of the root of this tree. * * @return a link to the left subtree of the tree */ public LinkedBinarySearchTree getLeft() { // To be completed as a Programming Project return null; // temp } /** * Returns the right subtree of the root of this tree. * * @return a link to the right subtree of the tree */ public LinkedBinarySearchTree getRight() { // To be completed as a Programming Project return null; // temp } }
Thanks