Question: Implement the ADT dictionary by using a binary search tree. Use the ADT dictionary in a word count program. Your class WordCount will take an
Implement the ADT dictionary by using a binary search tree. Use the ADT dictionary in a word count program. Your class WordCount will take an input from command line for the name of a text file. For this homework, the text file contains words or numbers that are tokenized (separated by whitespace)
//========================
BstDictonary.java
//=========================
public class BstDictionary , V> implements DictionaryInterface
{
private SearchTreeInterface> bst;
private int count;
public BstDictionary()
{
bst = new BinarySearchTree();
}
public V add(K key, V value)
{
Entry newEntry = new Entry<>(key,value);
Entry returnedEntry = bst.add(newEntry);
V result = null;
if(returnedEntry != null)
result = returnedEntry.getValue();
count++;
return result;
}
public V getValue(K key)
{
return bst.get(key);
}
public V remove(K key)
{
Entry findEntry = new Entry<>(key, null);
Entry returnedEntry = bst.remove(findEntry);
V result = null;
if(returnedEntry != null)
result = returnedEntry.getValue();
count--;
return result;
}
public Iterator getKeyIterator()
{
return new KeyIterator();
}
private class KeyIterator implements Iterator
{
Iterator> localIterator;
public KeyIterator()
{
localIterator = bst.getInorderIterator();
}
public boolean hasNext()
{
return localIterator.hasNext();
}
public K next()
{
Entry nextEntry = localIterator.next();
return nextEntry.getKey();
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
private class Entry, T> implements Comparable>
{
private S key;
private T value;
private Entry(S searchKey, T dataValue)
{
key = searchKey;
value = dataValue;
}
public V getValue() {
// TODO Auto-generated method stub
return null;
}
public K getKey() {
return null;
}
public int compareTo(Entry other)
{
return key.compareTo(other.key);
}
} // end Entry
} // end BstDictionary
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
