CSC 103 Assignment 2:
Trees

REVISED




Due Date

10PM Monday 30 April 2001

Program Specification

Part A

Implement the following class using a balanced binary search tree implementation (e.g., AVL tree or red-black tree).

/****
 *
 * <emphasis> Provide an appropriate top-level comment here. </emphsis>
 *
 * @author <emphasis>your name goes here</emphasis>
 * @version <emphasis>date of last edit goes here</emphasis>
 *
 */

public class SortedList {

    /**
     * Construct a new empty list.
     *
     */
    public SortedList() {}

    /**
     * Construct a new list containing as elements the elements of the given
     * array.
     *
     */
    public SortedList(Object array[]) {}


    /*-*
     * Constructive (i.e., storage) methods
     */

    /**
     * Put the given element in its correct sorted position in this.  Do
     * nothing if the given element is already stored in this.
     */
    public SortedList put(Object element) {}


    /*-*
     * Non-destructive access methods
     */

    /**
     * Return the element with the smallest value in this.
     */
    public Object getSmallest() {}

    /**
     * Return the element with the largest value in this.
     */
    public Object getLargest() {}

    /**
     * Return the element at the given position i of this, for 0 <= i <
     * this.size().  Return null if i < 0 or i >= this.size().
     */
    public Object get(int i) {}


    /*-*
     * Destructive access methods
     */

    /**
     * Remove and return the element with the smallest value in this.
     */
    public Object removeSmallest() {}

    /**
     * Remove and return the element with the largest value in this.
     */
    public Object removeLargest() {}

    /**
     * Remove and return the element at the given position i of this, for 0 <=
     * i < this.size().  Do nothing and return null if i < 0 or i >=
     * this.size().
     */
    public Object remove(int i) {}

    /*-*
     * Searching methods.
     */

    /**
     * Return true if this contains the given element, false if not.
     */
    public boolean elementOf(Object element) {}

    /**
     * Return the index position in this of the given element.  Return -1 if no
     * such element is found.
     */
    public int findIndex(Object element) {}

    /*-*
     * Utility methods
     */

    /**
     * Return a sublist of this from index positions i through j, inclusive.
     * Return null if i > j or i,j < 0 or i,j >= this.size().  Note that the
     * elements of the returned sublist are not copies of the corresponding
     * elements in this.  Hence changes made to sublist elements are reflected
     * in the original list.
     */
    public SortedList subList(int i, int j) {}

    /**
     * Return the number of elements in this.
     */
    public int size() {}

    /**
     * Return true if this is empty, false otherwise.
     */
    public  boolean isEmpty() {}

    /**
     * Return true if this.size() == list.size() and forall 0 <= i <
     * this.size(), this.get(i).equals(list.get(i)).  Return false otherwise.
     */
    public boolean equals(SortedList list) {}

    /**
     * Return a string of the following format:
     *                                                                      <p>
     *     [ get(0).toString(), ..., get(N-1).toString() ]
     *                                                                      <p>
     * assuming this contains N elements.  Return the empty string if this is
     * empty.
     */
    public String toString() {}

}

Part B

Write a class named GeneralTreeImplementation that implements the following interface

/****
 *
 * <emphasis> Supply a suitable header comment in the implementing class.
 * </emphasis>
 *
 * <emphasis> supply @author tag in the implementing class </emphasis>
 * <emphasis> supply @author tag in the implementing class </emphasis>
 *
 */

public interface GeneralTree {

    /**
     * Insert the given node value as the ith child of the given parent.  The
     * parent must be a GeneralTreeNode contained in this, which is typically
     * retrieved using the getSubtree or getLeaf method.
     *                                                                      <p>
     * Note that this is shallow insert, that only works at the level of
     * children immediately below the given parent as root.  Users can perform
     * a deep insert by calling the findSubtree method before insertChild.
     */
    public GeneralTree insertChild(Object parent, GeneralTreeNode child, int i);

    /**
     * Return the shallowest subtree of this with the given value as its root.
     */
    public GeneralTreeNode findSubtree(Object value);

    /**
     * Find the leftmost leaf in this with the given value.
     */
    public GeneralTreeNode findLeaf(Object value);

    /**
     * Return the Object value of the ith child of the given parent.  The
     * parent must be a GeneralTreeNode contained in this, which is typically
     * retrieved using the getSubtree method.
     */
    public Object getChild(Object parent, int i);

    /**
     * Return the index of child in parent, -1 if no children.  The parent must
     * be a GeneralTreeNode contained in this, which is typically retrieved
     * using the getSubtree method.
     */
    public int getIndexOfChild(Object parent, Object child);

    /**
     * Return the number of children of parent, 0 if no children.  The parent
     * must be a GeneralTreeNode contained in this, which is typically
     * retrieved using the getSubtree method.
     */
    public int getChildCount(Object parent);

    /**
     * Return the root of this tree.
     */
    public Object getRoot();

    /**
     * Return true if given node is a leaf.
     */
    public boolean isLeaf(Object node) ;


    /**
     * A node in a GeneralTree.
     */
    public interface GeneralTreeNode {}

}

Performance Requirements

Part A

In the javadoc comments for each method, add a description of the worst-case running time for the method. You must implement the methods such that the running times for each method are the best possible given the AVL or red-black tree implementation. "Best possible" means within an order of magnitude among O(c), O(log N), and O(N). None of the methods can take O(N2) time, i.e., all methods must perform in linear time or less.

Part B

There are no specific performance requirements for part B.

Empirical Analysis

Part A

The plotting part of the original assignment is NO LONGER REQUIRED. Your SortedList class will be tested with a series of method calls to fully exercise the implementation. You should test your own implementation with tests that exercise the methods to your satisfaction. The actual test program will not be revealed prior to turnin.

Hint: The general remove method is clearly the most difficult part of the implementation. My test program will test things in the following order, from the simple to the more difficult: constructor, the constructive put method, the utility methods, the non-destructive access methods, the searching methods, the destructive access methods.

Part B

The GeneralTreeImplementation class must compile and execute successfully with the GeneralTreeTest.java test driver program.