| | |
Binary Search Tree
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Solved Threads: 0
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!
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!
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!
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!
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Solved Threads: 0
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?
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?
BufferedReader: String.split() Those should give you an easy place to start.
java Syntax (Toggle Plain Text)
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(); }
java Syntax (Toggle Plain Text)
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); }
![]() |
Similar Threads
- deleting a Binary search tree (C++)
- Reading a file into a binary search tree (C++)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- Insertion in a binary search tree (C++)
Other Threads in the Java Forum
- Previous Thread: Help In Java
- Next Thread: Reversing a String which is inputed.
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






