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).
- Need help creating the WordCount class that can handle both tokenized words and numbers.
-
I already have the following interfaces/classes made:
BinaryNode.java
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(); }
TreeInterface.java
public interface TreeInterface { public T getRootData(); public int getHeight(); public int getNumberOfNodes(); public boolean isEmpty(); public void clear(); }
SearchTreeInterface.java
import java.util.Iterator;
public interface SearchTreeInterface>
extends TreeInterface{
public boolean contains(T entry);
public T getEntry(T entry);
publlic T add(T newEntry);
public T remove(T entry);
public Iterator getInorderIterator;
}
BinarySearchTree>
extends BinaryTree
implements SearchTreeInterface{
public BinarySearchTree(){
super();
}
public BinarySearchTree(T rootEntry){
super();
setRootNode(new BinaryNode (rootEntry));
}
public void setTree(T rootData){
throw new UnsupportedOperationException();
}
public void setTree(T rootData, BinaryTreeInterface leftTree, BinaryTreeInterface rightTree)
{
throw new UnsupportedOperationException();
}
//implements contains, getEntry, add and remove here
}
etc.
Description: 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) nput from or this homework, the text file Required I/O F. Last's Word Count your programI/o F. Last is your First initial and Last name. Turn in: 1. Page 1 of this description with your name on it. 2. Compress all the source codes (*.java) into a single zip file with the structure: hw1.zip WordCount.java TreePackage/ BinaryNode.javaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started