Question: In this programming project you will practice the implementation of binary search trees. Download the attached ZIP file for a starting place; it includes several
In this programming project you will practice the implementation of binary search trees. Download the
attached ZIP file for a starting place; it includes several interfaces and partial implementations that are
base for this ADT. You would only need to change three files: ArrayUnorderedList, LinkedBinaryTree, and
LinkedBinarySearchTree (should contain testing code).
1) Complete the implementation of ArrayList. Specifcally, implement these methods: removeFirst and
removeLast. Don't neglect support for the iterator in ArrayList. Do not import any packages other
than those already imported. [8 points]
2) Complete the implementation of ArrayUnorderedList. Specifcally, implement these methods: addToFront and addToRear. Don't neglect support for the iterator in ArrayList. Do not import any
packages other than those already imported. [8 points]
3) Complete the implementation of LinkedBinaryTree. Speciifcally, implement these methods: get-
RootElement(), getRootNode(), getLeft(), getRight(), size(), getHeight(), height(), contains(), toString(),
and iteratorPreOrder(). Review the comments in LinkedBinaryTree and BinaryTreeADT for implementation
requirements. [12 points]
4) [LC PP 11.2 modified] The LinkedBinarySearchTree class is currently using the find and contains
methods of the LinkedBinaryTree class via inheritance. This is not optimal! Implement these methods
for the LinkedBinarySearchTree class so that they will be more efficient by making use of the ordering
property of a binary search tree. Notice that six other methods in LinkedBinarySearchTree are not yet
implemented: removeMax(), findMin(), findMax(), find(), getLeft(), getRight(), findNode(). If any are
needed by your algorithms, implement them, otherwise omit them. [12 points]
/** * ArrayUnorderedList represents an array implementation of an unordered list. * * @author Lewis and Chase * @version 4.0 */ public class ArrayUnorderedList
/** * Creates an empty list using the specified capacity. * * @param initialCapacity the intial size of the list */ public ArrayUnorderedList(int initialCapacity) { super(initialCapacity); }
/** * Adds the specified element to the front of this list. * * @param element the element to be added to the front of the list */ @Override public void addToFront(T element) { // TODO: Implement this. }
/** * Adds the specified element to the rear of this list. * * @param element the element to be added to the list */ @Override public void addToRear(T element) { // TODO: Implement this. }
/** * Adds the specified element after the specified target element. * Throws an ElementNotFoundException if the target is not found. * * @param element the element to be added after the target element * @param target the target that the element is to be added after */ @Override public void addAfter(T element, T target) { if (size() == list.length) expandCapacity();
int scan = 0; // find the insertion point while (scan < rear && !target.equals(list[scan])) scan++; if (scan == rear) throw new ElementNotFoundException("UnorderedList"); scan++; // shift elements up one for (int shift=rear; shift > scan; shift--) list[shift] = list[shift-1];
// insert element list[scan] = element; rear++; modCount++; } }
/** * LinkedBinarySearchTree implements the BinarySearchTreeADT interface * with links. * * @author Lewis and Chase * @version 4.0 */ public class LinkedBinarySearchTree
Comparable
if (isEmpty()) root = new BinaryTreeNode
if (isEmpty()) throw new ElementNotFoundException("LinkedBinarySearchTree"); else { BinaryTreeNode
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
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
/** * 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 */ @Override 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
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 */ @Override public T removeMax() throws EmptyCollectionException { // TODO: May need to implement. }
/** * 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 */ @Override public T findMin() throws EmptyCollectionException { // TODO: May need to implement. }
/** * 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 */ @Override public T findMax() throws EmptyCollectionException { // TODO: May need to implement. }
// TODO: Implement find. // TODO: Implement contains. /** * Returns the left subtree of the root of this tree. * * @return a link to the left subtree fo the tree */ @Override public LinkedBinarySearchTree
import java.util.*;
/** * LinkedBinaryTree implements the BinaryTreeADT interface * * @author Lewis and Chase * @version 4.0 */ public class LinkedBinaryTree
/** * Creates a binary tree with the specified element as its root. * * @param element the element that will become the root of the binary tree */ public LinkedBinaryTree(T element) { root = new BinaryTreeNode<>(element); } /** * Creates a binary tree with the specified element as its root and the * given trees as its left child and right child * * @param element the element that will become the root of the binary tree * @param left the left subtree of this tree * @param right the right subtree of this tree */ public LinkedBinaryTree(T element, LinkedBinaryTree
/** * Returns the integer size of this tree. * * @return the integer size of the tree */ @Override public int size() { // TODO: Implement this. } /** * Returns the height of this tree. * * @return the height of the tree */ public int getHeight() { // TODO: Implement this. } /** * Returns the height of the specified node. * * @param node the node from which to calculate the height * @return the height of the tree */ private int height(BinaryTreeNode
/** * Returns a reference to the specified target element if it is * found in this binary tree. * * @param targetElement the element being sought in this tree * @param next the element to begin searching from */ private BinaryTreeNode
/** * Returns an iterator over the elements in this tree using the * iteratorInOrder method * * @return an in order iterator over this binary tree */ @Override public Iterator
/** * Performs a recursive inorder traversal. * * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void inOrder(BinaryTreeNode
/** * Performs an preorder traversal on this binary tree by calling * an overloaded, recursive preorder method that starts with * the root. * * @return a pre order iterator over this tree */ @Override public Iterator
/** * Performs a recursive preorder traversal. * * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void preOrder(BinaryTreeNode
/** * Performs an postorder traversal on this binary tree by calling * an overloaded, recursive postorder method that starts * with the root. * * @return a post order iterator over this tree */ @Override public Iterator
/** * Performs a recursive postorder traversal. * * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void postOrder(BinaryTreeNode
/** * Performs a levelorder traversal on this binary tree, using a * templist. * * @return a levelorder iterator over this binary tree */ @Override public Iterator
nodes.addToRear(root); while (!nodes.isEmpty()) { current = nodes.removeFirst(); if (current != null) { tempList.addToRear(current.getElement()); if (current.getLeft() != null) nodes.addToRear(current.getLeft()); if (current.getRight() != null) nodes.addToRear(current.getRight()); } else tempList.addToRear(null); } return new TreeIterator(tempList.iterator()); } /** * Inner class to represent an iterator over the elements of this tree */ private class TreeIterator implements Iterator
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
