1. Engineering
  2. Computer Science
  3. binarysearchtreeadt defines the interface to a binary search...

Question: binarysearchtreeadt defines the interface to a binary search...

Question details

Objectives: Creating an array-based implementation of a binary search tree using computational strategy Using an iterator Using recursion Lab Assignment: In this lab you are going to implement BinaryTreeADT and BinarySearchTreeADT interfaces using a computational rray strategy in order to create a BinarySearch Tree 1 . Dont use an instance variable count to keep track of the number of nodes in the tree In computational strategy, for any element stored in position n of the array, that elements left child will be stored in position ( (2 * n) + 1) and that elements right child will be stored in position ( (2 * n) +2) 2. Write two constructors. One that creates an empty tree and one that creates a tree with a specified element (received by parameter) as its root. 3. Method toString should return a string representation of the tree in the level order 4. Method iterator should return an inorder iterator 5. Create an EmptyTreeException class. Throw this exception in getRootElement, findMin, findMax and find methods when the tree is empty 6. Your textbook uses ArrayUnorderedList object to implement traversal methods. You can use ArrayList class from java.util package (this way you dont have to write ArrayUnorderedList class). To add element the the ArrayList use method add instead of addToRear 7. Write an application that creates an ArrayBinarySearchTree object as displayed in Fig 1. In the applications call methods from the ArrayBinarySearchTree class to test the methods. You dont have to use GUI in your application Fig 1.

/**
* BinarySearchTreeADT defines the interface to a binary search tree.
* @param <T>
*/
public interface BinarySearchTreeADT<T> extends BinaryTreeADT<T> {

/**
* Adds the specified element to the proper location in this tree.
*
* @param element the element to be added to this tree
*/
public void add(T element);

  
/**
* Returns the smallest element in this tree without removing it.
*
* @return the smallest element in the tree
*/
public T findMin();

/**
* Returns the largest element in this tree without removing it.
*
* @return the largest element in the tree
*/
public T findMax();
}

------------------------------------------------------------------------------------------

/**
* BinaryTreeADT defines the interface to a binary tree data structure.
*
*/
import java.util.Iterator;

public interface BinaryTreeADT<T>
{
/**
* Returns a reference to the root element
*
* @return a reference to the root
* @throws EmptyCollectionException if the tree is empty
*/
public T getRootElement ();

/**
* Returns true if this binary tree is empty and false otherwise.
*
* @return true if this binary tree is empty
*/
public boolean isEmpty();

/**
* Returns the number of elements in this binary tree.
*
* @return the integer number of elements in this tree
*/
public int size();

/**
* Returns true if the binary tree contains an element that matches
* the specified element and false otherwise.
*
* @param targetElement the element being sought in the tree
* @return true if the tree contains the target element
*/
public boolean contains (T targetElement);

/**
* Returns a reference to the specified element if it is found in
* this binary tree. Throws an exception if the specified element
* is not found.
*
* @param targetElement the element being sought in the tree
* @return a reference to the specified element
* @throws ElementNotFoundException if an element is not found
*/
public T find (T targetElement);

/**
* Returns the string representation of the binary tree.
*
* @return a string representation of the binary tree
*/
public String toString();

/**
* Performs an inorder traversal on this binary tree by calling an
* overloaded, recursive inorder method that starts with the root.
*
* @return an iterator over the elements of this binary tree
*/
public Iterator<T> iteratorInOrder();

/**
* Performs a preorder traversal on this binary tree by calling an
* overloaded, recursive preorder method that starts with the root.
*
* @return an iterator over the elements of this binary tree
*/
public Iterator<T> iteratorPreOrder();

/**
* Performs a postorder traversal on this binary tree by calling an
* overloaded, recursive postorder method that starts with the root.
*
* @return an iterator over the elements of this binary tree
*/
public Iterator<T> iteratorPostOrder();

/**
* Performs a levelorder traversal on the binary tree, using a queue.
*
* @return an iterator over the elements of this binary tree
*/
public Iterator<T> iteratorLevelOrder();
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution