| | |
Binary Search Tree
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Solved Threads: 0
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();
}
}
}
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();
}
}
}
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Solved Threads: 0
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?
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?
![]() |
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 |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





