Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Binary Search Tree Construction from txt file help

807601Apr 24 2008
Hi, first post but ive browsed intermitently when looking for help with other problems. This one has me stumped.

I need to construct a BST of TreeNodes based on a txt file.

so far i have the following TreeNode class:

private class TreeNode {
private String data;

private TreeNode left;

private TreeNode right;

public TreeNode(String theData) {
data = theData;
left = null;
right = null;
}

public TreeNode(String strData, TreeNode leftNode,
TreeNode rightNode) {
data = strData;
left = leftNode;
right = rightNode;
}
}

and here is my BST constructor that im working on plus the instance variables:

private TreeNode root;

private TreeNode current;

private Scanner s;

private String fileName;

private String token;


public Tree(String name) {

fileName = name;

try {

s = new Scanner(new File(fileName));

}

catch (FileNotFoundException e) {

e.printStackTrace();

}
if (s.hasNextLine())
token = s.nextLine();
current = root;
root = build();
s.close();

}

here is the build class pseudocode im using to set up the tree, it is supposed to be reccursive but im having trouble with it. it is specifically meant to take a txt file that contains lines of questions and answers that simulate the game 20Questions.

private TreeNode build() {
if (!s.hasNextLine())
return null;

if(current == root){
root = new TreeNode(token)
s.nextLine
//there is a space between each line that has a question or answer, this is meant to skip it, otherwise i try and compare a blank
line and i get index out of bounds error
token = s.nextLine
current = root.left
}

skip the next line if current != root, token = s.nextLine

if(token.charAt(token.length()-1) != '?') { //if token is not a question, create new node and return null
(no questions come after an answer is found)
current = new TreeNode(token);
return null;
}
else (current token is a ?, so build off it to the left)
TreeNode left = build();
TreeNode right = build();
return new TreeNode(token, current.left, current.right);
}
}

i am really lost on what to do, i keep thinking i figured it out then i still am creating either nothing or just the first node. im pretty sure im messing up the reccursive part and that im not advancing the node that each reccursion starts from, but i cant figure out how to do that

an example of the text file that is used as input is:

//start
Is it an animal?

Does it live on land?

Does is walk?

dog

snake

fish

Is it made of metal?

car

tree
//end

the first line after a ? is the answer to yes, the next is answer to no
the above tree should be made like this
++++tree
+++Is it made of metal?
++++car
Is it an animal?
++++fish
++Does it live on land?
++++++snake
++++Does it walk?
++++++dog

the above tree starts from left and goes right, down is answering yes, up is no
hopefully that makes sense, im sure i managed to make this question confusing. ill try and clear up any questions as best i can
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 22 2008
Added on Apr 24 2008
0 comments
386 views