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!

Create Tree from Preordered data

807599Apr 25 2007 — edited Apr 25 2007
Here is a quick summary of what I'm trying to do:
I have a file with formatted like this

Does it have legs?
Dog
Fish

and the resulting tree for it should look like this
	Does it have legs?
	/	\
     Dog        Fish
and I'm trying to write a function that takes in a Scanner object that should already be tied to the data file and create a tree from this data that is already listed in Preorder fashion. I know tha recurison is probably the best way to go about this but everything that I have written hasn't worked. Also, I cant really figure out a way to use recursion when the function is taking in a Scanner I dont know how it would work.

Any help would be greatly appreciated.

Here is what I have now:
	public BinaryTree<String> readTree(Scanner data)
	{
		BinaryTree<String> temp = new BinaryTree<String>();
		
		temp.attachLeft(data.next());
		temp.attachRight(data.next());
		
		return temp;
	}
I know this function wont go through the whoel file, but I've tried many loops but I cant figure out how to get it to work so I'm convinced that I need to figure out how to make it recursive

(yes I know that everything that can be done through recursion can be done with a loop)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2007
Added on Apr 25 2007
15 comments
270 views