Using a Binary Search Tree
Using the LinkedBinarySearchTree class in the jss2 for chapters 12 and 13, write a program that exercises all of the operations except:
removAllOccurrences
findAgain
replacement
Details
Your program will read instruction from the file: numbers.txt
The file is formatted as follows:
The first line in the file will contain a list of numbers used to create a binary tree on one line.
Each ensuing line will contain one of the following list of commands that correspond to the BST methods
ADD someNumber// Add the number to the BST
REMOVE someNumber// Remove the number from the BST
REMOVEMAX// Remove the largest number
REMOVEMIN// Remove the smallest number
FINDMAX// Find and print the largest number
FINDMIN// Find and print the smallest number
FIND someNumber// Find and print a particular number
PREORDER// Print the tree using preorder traversal. You write this.
POSTORDER// Print the tree using postorder traversal. You write this.
INORDER// Print the tree using inorder traversal. You write this.
Miscellaneous
1.Note that these methods throw some sort of exception. Your program will have to handle them so test well.
2.Annotate and format all output for maximum readability.

I dont want someone to do the program, I just need someone to explain to me what Im supposed to do because my text book does not give good examples. This is what I have so far:

import jss2.*;
import java.util.Scanner;
import java.io.*;


public class LinkedBSearch
{


public static void main(String[] args) throws IOException
{


Scanner fileScan;
fileScan = new Scanner(new File("numbers.txt"));


StringTokenizer st = new StringTokenizer("numbers.txt");


int[] ints = new int[Tokenizer.countTokens()];



while (st.hasMoreTokens())
{


System.out.println(st.nextToken());
}



System.out.println(fileScan);


}
}

The professor showed us the program but didnt really explain it in detail so we wouldnt copy what he did. Can someone help me get on the right track. a jss2 folder was provided with over 20 things in it...so confusing!

Edited 3 Years Ago by happygeek: fixed formatting

Well, the only piece of the code you wrote that will be of any use for that assignment at all is the Scanner (though a BufferedReader would be my own choice for that). The instructions are pretty clearly laid out and does tell you what you need to use from that jss2 folder.

You need to read the file line by line. The first line is the input to create the BST. For the rest of the lines you will need to parse and match the command token to the appropriate action to take on the BST. String.split() will be useful for that part to separate <command> <parameters>.

So time to crack the books and get a start on it. Post specific questions if you run into trouble. Good luck!

I already started the text file with the appropriate information. How do you create the binary search tree?

Well, your instructions tell you to use the LinkedBinarySearchTree - so maybe that is a hint? You have read them, right?

Okay heres what I have and its unorganized cause im trying to figure out the string tokenizer.

import java.util.StringTokenizer;
import java.util.Scanner;
import java.io.*;



public class LinkedBSearch
{


public static void main(String[] args) throws IOException
{



Scanner fileScan;
String aNumber;



//create an empty Tree
LinkedBinarySearchTree<Integer> myBST = new LinkedBinarySearchTree<Integer>();
System.out.println("I did not receive any help from anyone except for Mr. Willis and the assigned tutor");
System.out.println("Succesfully created nothing...an empty tree");


// create a Scanner object for reading a file
fileScan = new Scanner(new File("numbers.txt"));


try
{
while(input != null )
{
StringTokenizer st = new StringTokenizer(input);


while (fileScan.hasNext())
{
aNumber= fileScan.next();


while(st.hasMoreTokens())
{


if(aNumber.equals


System.out.println(st.nextToken());
}



System.out.println(fileScan);


}
}

what im trying to figure out is after I have declared the linked BST and declared the stringTokenizer how do I implement the the information being read from the file into the tokenizer?

Edited 3 Years Ago by happygeek: fixed formatting

My professor used the stringTokenizer in the program he had did, never saw the String.split() also same thing with the BufferReader(I have seen it and read it in books but my professors never used it thats why I use the scanner class unless you could give me an example of how to use them?

BufferedReader:

try {
    BufferedReader reader = new BufferedReader( new FileReader("yourFile.txt") );
    String line = reader.readLine();
    while (line !=null){
        // do your thing

        line = reader.readLine();
    }
} catch(Exception ex) {
    ex.printStackTrace();
}

String.split()

String lineOfText = "Here is a line of text.";
// splitting on a whitespace character, you could use other delimiters
String[] words = lineOfText.split("\\s");
for(String word : words) {
    System.out.println(word);
}

Those should give you an easy place to start.

Okay I made another class to test the first example you gave me. It compiles with no errors but it doesnt print the first line of my text file?

I would like to say thanks for being patient with me cause Im new at this! This is what I have done and this comes out with two errors

public static void main(String[] args) throws IOException
{


try
{


BufferedReader reader = new BufferedReader( new FileReader("numbers.txt") );


String line = reader.readLine();


while ((line = reader.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line);


// count how many tokens inside st object
System.out.println("tokens count: " + st.countTokens());


while(st.hasMoreElements())
{


String token = st.nextElement().toString();
System.out.println("token = " + token);


}



// do your thing


line = reader.readLine();


}


}
catch(Exception ex)
{


ex.printStackTrace();


}
}
}

Edited 3 Years Ago by happygeek: fixed formatting

this is how my text file reads:
65 47 87 48 11 89 27 6 4 3 2 7 33 68 78 57 83 27 6 78 11 75 9 10 19 53 2
addElement
removeElement
removeMax
removeMin
findMax
findMin
find

ps the preOrder
PostOrder
inOrder are not working right so the professor is now takin them out. We also have a jss2 file within our folder

Hi I have been working out the kinks in my program and I have progressed a little more. Here is what I have:

import java.util.StringTokenizer;
import java.util.Scanner;
import jss2.*;
import java.io.*;


public class LinkedBSearch
{

public static void main(String[] args) throws IOException
{


Scanner fileScan;
String aNumber;


//create an empty Tree
LinkedBinarySearchTree<Integer> myBST = new LinkedBinarySearchTree<Integer> ();
System.out.println("I did not receive any help from anyone except for Mr. Willis and the assigned tutor");
System.out.println("Succesfully created nothing...an empty tree");

// create a Scanner object for reading a file
fileScan = new Scanner(new File("numbers.txt"));

try
{
//Create a new Scanner object which will read the data from the
// file passed in. To check if there are more line to read from it
// we check by calling the fileScan.next() method. We then
// read line one by one till all lines are read.


int aToken;
String tokenLine = fileScan.nextLine();

StringTokenizer st = new StringTokenizer(tokenLine);
System.out.println ("********* " + tokenLine);


while(st.hasMoreTokens())
{

aToken = Integer.parseInt (st.nextToken());
System.out.println ("$$$ " + aToken);
//add one element at a time to the tree.
if (!tokenLine.equals(""))
{
myBST.addElement(aToken);
}
else
{
System.out.println("\nCannot add empty data!");
}


}

// Print the tree.
System.out.println(myBST);


while (fileScan.hasNext())
{
String line = fileScan.next();
System.out.println(line);
}

}
catch (Exception e)
{
e.printStackTrace();
}
}
}

jGRASP exec: java LinkedBSearch

I did not receive any help from anyone except for Mr. Willis and the assigned tutor
Succesfully created nothing...an empty tree
********* 65 47 87 48 11 89 27 6 4 3 2 7 33 68 78 57 83 27 6 78 11 75 9 10 19 53 2
$$$ 65
$$$ 47
$$$ 87
$$$ 48
$$$ 11
$$$ 89
$$$ 27
$$$ 6
$$$ 4
$$$ 3
$$$ 2
$$$ 7
$$$ 33
$$$ 68
$$$ 78
$$$ 57
$$$ 83
$$$ 27
$$$ 6
$$$ 78
$$$ 11
$$$ 75
$$$ 9
$$$ 10
$$$ 19
$$$ 53
$$$ 2
65
47
11
6
4
3
2
2
7
6
9
10
27
11
19
33
27
48
57
53
87
68
78
75
83
78
89

addElement
removeElement
removeMax
removeMin
findMax
findMin
find

----jGRASP: operation complete.
Im reading the numbers and command from a txt file
This compiles without any error but what im trying to figure out is how do I add each node into the myBST. Also how to get the commands not to print out?

This question has already been answered. Start a new discussion instead.