Binary Search Tree

Thread Solved

Join Date: Apr 2008
Posts: 15
Reputation: dimples09 is an unknown quantity at this point 
Solved Threads: 0
dimples09 dimples09 is offline Offline
Newbie Poster

Binary Search Tree

 
0
  #1
Apr 18th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Binary Search Tree

 
0
  #2
Apr 18th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 15
Reputation: dimples09 is an unknown quantity at this point 
Solved Threads: 0
dimples09 dimples09 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #3
Apr 20th, 2008
I already started the text file with the appropriate information. How do you create the binary search tree?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Binary Search Tree

 
0
  #4
Apr 20th, 2008
Well, your instructions tell you to use the LinkedBinarySearchTree - so maybe that is a hint? You have read them, right?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 15
Reputation: dimples09 is an unknown quantity at this point 
Solved Threads: 0
dimples09 dimples09 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #5
Apr 21st, 2008
Yes I read them. I have added some more things and will post it later
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 15
Reputation: dimples09 is an unknown quantity at this point 
Solved Threads: 0
dimples09 dimples09 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #6
Apr 21st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Binary Search Tree

 
0
  #7
Apr 21st, 2008
Don't use StringTokenizer, use String.split().
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 15
Reputation: dimples09 is an unknown quantity at this point 
Solved Threads: 0
dimples09 dimples09 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #8
Apr 21st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Binary Search Tree

 
0
  #9
Apr 21st, 2008
BufferedReader:
  1. try {
  2. BufferedReader reader = new BufferedReader( new FileReader("yourFile.txt") );
  3. String line = reader.readLine();
  4. while (line !=null){
  5. // do your thing
  6.  
  7. line = reader.readLine();
  8. }
  9. } catch(Exception ex) {
  10. ex.printStackTrace();
  11. }
String.split()
  1. String lineOfText = "Here is a line of text.";
  2. // splitting on a whitespace character, you could use other delimiters
  3. String[] words = lineOfText.split("\\s");
  4. for(String word : words) {
  5. System.out.println(word);
  6. }
Those should give you an easy place to start.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 15
Reputation: dimples09 is an unknown quantity at this point 
Solved Threads: 0
dimples09 dimples09 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #10
Apr 22nd, 2008
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC