Question: Java Programming Implement the ADT dictionary by using a binary search tree . Use the ADT dictionary in a word count program. Your class WordCount

Java Programming

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. This text file will contain words or numbers that are tokenized (separated by whitespace). Finish the implementation of BSTDictionary and create the WordCount program. The text file and source code is below. If there is extraneous methods from the interfaces just make the implementation throw operationnotsupportedexception.

public class BinaryNode { private T data; private BinaryNode leftChild, rightChild; public BinaryNode() { this(null); } public BinaryNode(T data) { this(data, null, null); } public BinaryNode(T data, BinaryNode left, BinaryNode right) { this.data = data; leftChild = left; setRightChild(right); } public T getData() { return data; } public void setData(T data) { this.data = data; } public BinaryNode getLeftChild(){ return leftChild; } public void setLeftChild(BinaryNode left) { leftChild = left; }

public BinaryNode getRightChild() { return rightChild; }

public void setRightChild(BinaryNode right) { this.rightChild = right; } public boolean hasLeftChild() { return leftChild != null; } public boolean hasRightChild() { return rightChild != null; } public boolean isLeaf() { return (!hasLeftChild() && !hasRightChild()); } public BinaryNode copy(){ BinaryNode newRoot = new BinaryNode(data); if(leftChild != null) { newRoot.setLeftChild(leftChild.copy()); } if(rightChild != null) { newRoot.setRightChild(rightChild.copy()); } return newRoot; } public int getHeight() { return getHeight(this); } private int getHeight(BinaryNode node) { int height = 0; if(node != null) { height = 1 + Math.max(getHeight(node.getLeftChild()), getHeight(node.getRightChild())); } return height; } public int getNumberOfNodes() { return getNumberOfNodes(this); } private int getNumberOfNodes(BinaryNode node) { int rightNumber = 0; int leftNumber = 0; if(leftChild != null) { leftNumber = getNumberOfNodes(node.getLeftChild()); } if(rightChild != null) { rightNumber = getNumberOfNodes(node.getRightChild()); } return 1 + leftNumber + rightNumber; } }

BinaryTree.java

import java.util.Iterator; import java.util.Stack;

public class BinaryTree implements BinaryTreeInterface {

private BinaryNode root;

public BinaryTree() { root = null; }

public BinaryTree(T rootData) { root = new BinaryNode(rootData); }

public BinaryTree(T rootData, BinaryTree leftTree, BinaryTree rightTree) { privateSetTree(rootData, leftTree, rightTree); }

public void setTree(T rootData) { root = new BinaryNode(rootData); }

public void setTree(T rootData, BinaryTree left, BinaryTree right) { privateSetTree(rootData, left, right); }

private void privateSetTree(T rootData, BinaryTree left, BinaryTree right) { root = new BinaryNode<>(rootData);

if ((left != null) && (!left.isEmpty())) { root.setLeftChild(left.root.copy()); } if ((right != null) && (!right.isEmpty())) { root.setRightChild(right.root.copy()); } }

public T getRootData() { return root.getData(); }

public int getHeight() { return root.getHeight(); }

public int getNumberOfNodes() { return root.getNumberOfNodes(); }

public boolean isEmpty() { return root == null; }

public void clear() { root = null; }

protected BinaryNode getRootNode() { return root; }

public Iterator getPreorderIterator() { throw new UnsupportedOperationException("Preorder not supported."); }

public Iterator getInorderIterator() { throw new UnsupportedOperationException("Inorder not supported."); }

public Iterator getPostorderIterator() { return new PostorderIterator(); }

public Iterator getLevelorderIterator() { throw new UnsupportedOperationException("Level Order not supported."); }

private class PostorderIterator implements Iterator {

private Stack> nodeStack; private BinaryNode current;

public PostorderIterator() { nodeStack = new Stack<>(); current = root; populateStack(current); } private void populateStack(BinaryNode node){ nodeStack.add(node); if(node.hasRightChild()){ populateStack(node.getRightChild()); } if(node.hasLeftChild()){ populateStack(node.getLeftChild()); } }

public boolean hasNext() { return !nodeStack.isEmpty(); }

public T next() { return nodeStack.pop().getData(); }

}

}

BinaryTreeInterface.java

public interface BinaryTreeInterface extends TreeInterface, TreeIteratorInterface{ public void setTree(T rootData); public void setTree(T rootData, BinaryTree left, BinaryTree right); }

TreeIteratorInterface.java

import java.util.Iterator;

public interface TreeIteratorInterface { public Iterator getPreorderIterator(); public Iterator getInorderIterator(); public Iterator getPostorderIterator(); public Iterator getLevelorderIterator(); }

DictionaryInterface.java

import java.util.Iterator;

public interface DictionaryInterface{

public V add(K key, V value);

public V remove(K key);

public V getValue(K key);

public boolean contains(K key);

public Iterator getKeyIterator();

public boolean isEmpty();

public int getSize();

public void clear();

}

BSTDictionary.java

import java.util.Iterator;

public class BSTDictionary,V>

implements DictionaryInterface{

private SearchTreeInterface> bst;

public BSTDictionary(){

bst = new BinarySearchTree<>();

}

private class Entry,T>

implements Comparable>

{

private S key;

private T value;

private Entry(S searchKey, T dataValue){

key = searchKey;

value = dataValue;

}

public int compareTo(Entry other){

return key.compareTo(other.key);

}

// Entry should also define the method equals, to String, getKey, getValue, and setValue. no setKey is provided

}

@Override

public V add(K key, V value) {

// TODO Auto-generated method stub

return null;

}

@Override

public V remove(K key) {

// TODO Auto-generated method stub

return null;

}

@Override

public V getValue(K key) {

// TODO Auto-generated method stub

return null;

}

@Override

public boolean contains(K key) {

// TODO Auto-generated method stub

return false;

}

@Override

public Iterator getKeyIterator() {

// TODO Auto-generated method stub

return null;

}

@Override

public boolean isEmpty() {

// TODO Auto-generated method stub

return false;

}

@Override

public int getSize() {

// TODO Auto-generated method stub

return 0;

}

@Override

public void clear() {

// TODO Auto-generated method stub

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!