The assignment is to complete the implementation of a group of files that create and test a binary search tree. It takes in 2 text files of integers, creates a tree with the first and uses the second to compare to the tree and tell whether of not each integer is in the tree. I am having an issue with the MySearchTree class. I do not know how to read in a file of integers and build a tree using them. Also the preOrder, postOrder, and inOrder methods have already been implemented in BinaryNode class so I do not know what to put in those methods in the MySearchTree class. Can anyone help? Here is each class to give you a better idea of what I am talking about:
import java.util.Scanner;
import java.io.*;
// Implements a binary search tree.
public class MySearchTree implements BSearchTree{
private BinaryNode root;
private int size;
// Create an empty binary search tree.
public MySearchTree()
{
root = null;
}
// Print this tree's elements in pre-order each separated by a space.
public void preOrder()
{
}
// Print this tree's elements in order each separated by a space.
public void inOrder()
{
}
// Print this tree's elements in post-order each separated by a space.
public void postOrder()
{
}
// buildTree - read integers from text file and build this search tree..
public void buildTree(String fileName, String[] args)
{
}
// Returns true if item is found in this tree else returns false.
public boolean find(Comparable c)
{
if(root != null) return root.find(c);
return false;
}
// Returns number of nodes in this search tree.
public int size() { return size; }
}
// Implements a binary node with three modes of depth-first search.
public class BinaryNode
{
protected Comparable element;
protected BinaryNode left;
protected BinaryNode right;
public BinaryNode(Comparable c) { this(c, null, null); }
public BinaryNode(Comparable c, BinaryNode aLeft, BinaryNode aRight)
{
element = c;
left = aLeft;
right = aRight;
}
// getLeft - return left child.
public BinaryNode getLeft() { return left; }
// getRight - return right child.
public BinaryNode getRight() { return right; }
// getElement - return element.
public Comparable getElement() { return element; }
// getLeft - return left child.
public void setLeft(BinaryNode n) { left = n; }
// getRight - return right child.
public void setRight(BinaryNode n) { right = n; }
// getElement - return element.
public void setElement(Comparable c) { element = c; }
// Print this tree's elements in pre-order each separated by a space.
void preOrder()
{
System.out.print( element + " " );
if( left != null ) left.preOrder( );
if( right != null ) right.preOrder( );
}
// Print this tree's elements in order each separated by a space.
void inOrder()
{
if(left != null) left.inOrder( );
System.out.print(element + " ");
if(right != null) right.inOrder( );
}
// Print this tree's elements in post order each separated by a space.
void postOrder()
{
if( left != null ) left.postOrder( );
if( right != null ) right.postOrder( );
System.out.print( element + " " );
}
// Returns true is item is foundin this tree else returns false.
public boolean find(Comparable c)
{
if(c.compareTo(element) == 0)
return true;
else if(c.compareTo(element) < 0) {
if(left == null) return false;
else return left.find(c);
}
else {
if(right == null) return false;
else return right.find(c);
}
}
}
import java.util.Scanner;
import java.io.*;
public class ClientForSearchTree{
// Builds a search tree with the integers in the first input file then
// determines which integers in the second input file are in the search
// tree. Finally prints the search tree using all 3 depth-first traversals.
public static void main(String args[]){
if(args.length != 2){
System.out.println("Usage: MySearchTree file1 file2");
System.exit(1);
}
MySearchTree tree = new MySearchTree();
try{
Scanner sc = new Scanner(new File(args[1]));
tree.buildTree(args[0]);
while(sc.hasNextInt()){
Integer iValue = new Integer(sc.nextInt());
if(tree.find(iValue))
System.out.println(iValue + " is in the tree.");
else
System.out.println(iValue + " is not in the tree.");
}
System.out.println("\nPre-order traversal:");
tree.preOrder();
System.out.println("\n\nIn-order traversal:");
tree.inOrder();
System.out.println("\n\nPost-order traversal:");
tree.postOrder();
System.out.println();
}
catch(Exception e){
System.err.println(e.getMessage());
System.exit(1);
}
}
}
// Defines a public set of methods for a binary search tree.
public interface BSearchTree
{
// Print this tree's elements in pre order each separated by a space.
void preOrder();
// Print this tree's elements in order each separated by a space.
void inOrder();
// Print this tree's elements in post order each separated by a space.
void postOrder();
// buildTree - read integers from text file and build this search tree.
void buildTree(String fileName);
// Returns true is item is found in this tree else returns false.
boolean find(Comparable c);
}