Write a test client for BinarySearchTree that checks the imp

Write a test client for BinarySearchTree that checks the implementation of the following methods:

min ()

max ()

keys ()

select ()

rank (), floor (), ceiling ()

For example, the client for size () is:

StdOut.println(\"size = \" + st.size());

Solution

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.TreeMap;

public class ST<Key extends Comparable<Key>, Value> implements Iterable<Key>
{

    private TreeMap<Key, Value> st;

  
    public ST()
    {
        st = new TreeMap<Key, Value>();
    }


  
    public Value get(Key item)
    {
        if (item == null)
       throw new IllegalArgumentException(\"called get() with null key\");
        return st.get(item);
    }

  
    public void put(Key item, Value val)
    {
        if (item == null)
       throw new IllegalArgumentException(\"called put() with null key\");
        if (val == null)
       st.remove(item);
        else           
       st.put(item, val);
    }


    public void delete(Key item) {
        if (item == null)
       throw new IllegalArgumentException(\"called delete() with null key\");
        st.remove(item);
    }


    public boolean contains(Key item)
    {
        if (item == null)
       throw new IllegalArgumentException(\"called contains() with null key\");
        return st.containsKey(item);
    }


    public int size()
    {
        return st.size();
    }

  
    public boolean isEmpty()
    {
        return size() == 0;
    }


    public Iterable<Key> keys()
    {
        return st.keySet();
    }


    public Iterator<Key> iterator()
    {
        return st.keySet().iterator();
    }

    public Iterable<Key> keys()
    {
        return st.keySet();
    }

    public Key min()
    {
        if (isEmpty())
       throw new NoSuchElementException(\"called min() with empty symbol table\");
        return st.firstKey();
    }


    public Key max()
    {
        if (isEmpty())
       throw new NoSuchElementException(\"called max() with empty symbol table\");
        return st.lastKey();
    }


    public Key ceiling(Key item)
    {
        if (item == null)
       throw new IllegalArgumentException(\"called ceiling() with null key\");
        Key k = st.ceilingKey(item);
        if (k == null)
       throw new NoSuchElementException(\"all keys are less than \" + item);
        return k;
    }

  
    public Key floor(Key item)
    {
        if (key == null)
       throw new IllegalArgumentException(\"called floor() with null key\");
        Key k = st.floorKey(key);
        if (k == null)
       throw new NoSuchElementException(\"all keys are greater than \" + item);
        return k;
    }

    public static void main(String[] args)
    {
        ST<String, Integer> st = new ST<String, Integer>();
        for (int i = 0; !StdIn.isEmpty(); i++)
   {
            String item = StdIn.readString();
            st.put(item, i);
        }

        StdOut.println(\"min key: \" + st.min());
        StdOut.println(\"max key: \" + st.max());
        StdOut.println(\"size:    \" + st.size());
        StdOut.println();

        for (String s : st.keys())
            StdOut.println(s + \" \" + st.get(s));
    }
}

Write a test client for BinarySearchTree that checks the implementation of the following methods: min () max () keys () select () rank (), floor (), ceiling ()
Write a test client for BinarySearchTree that checks the implementation of the following methods: min () max () keys () select () rank (), floor (), ceiling ()
Write a test client for BinarySearchTree that checks the implementation of the following methods: min () max () keys () select () rank (), floor (), ceiling ()

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site