aspose file tools*
The moose likes Beginning Java and the fly likes Question about TreeNode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about TreeNode" Watch "Question about TreeNode" New topic
Author

Question about TreeNode

Taylor Woods
Greenhorn

Joined: Mar 19, 2005
Posts: 10
Hello. I'm working on basic skills of Tree now. But I have no idea how to
creat a client for this class to work. Could anyone show me how to write a client for this class?
------------------------------------------
Using this class,
1> I would like to creat a binary search tree with the following set of data : 15, 23, 8, 1, 2, 16

2> I would like to delete the node containg 2

3> I would like to print the value stored in each node with an pre-order traversal
----------------------------------
Here is my class I wrote.


public class TreeNode
{
private int value;
private TreeNode left;
private TreeNode right;

public TreeNode (int num, TreeNode initleft, TreeNode initright)
{
value = num;
left = initleft;
right = initright;
}

public int getValue()
{
return value;
}

public TreeNode getLeft()
{
retrun left;
}

public TreeNode getRight()
{
return right;
}

public void setValue(int newValue)
{
value = newValue;
}

public void setLeft(TreeNode newLeft)
{
left = newLeft;
}

public void setright(TreeNode newRight)
{
right = newRight;
}

public TreeNode insert(int num, TreeNode root)
{
TreeNode newNode = new TreeNode(num, null, null);
if (root = null)
root = newNode;
else
TreeNode lead, tail;
lead = root;
while (lead!=null)
{
tail = lead;
if (num < lead.getValue())
lead = lead.getLeft();
else
lead = lead.getRight();
}
if (num < tail.getValue())
tail.getLeft(newNode);
else
tail.getRight(newNode);
return root;
}

private void preOrder(TreeNode root)
{
if (root!=null)
{
System.out.println(root.getValue());
preOrder(root.getLeft());
preOrder(root.getRight());
}
}

-------------------------------------------------------------------
by the way, I couldn't figure out how to set up a constructor for "delete"
I would appreciate if anyone could help me with this.

Thank you so much
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Welcome to the ranch!

A: When posting code, you should use "code tags." These preserve formatting, and make your code easier to read. The tags are right below the message box.

B:


Question about this section: tail.getLeft(newNode) doesn't make any sense- getLeft doesn't take any arguments. I assume you mean that to be setLeft. Try it with that fix, and see if there are still problems.


I've heard it takes forever to grow a woman from the ground
Taylor Woods
Greenhorn

Joined: Mar 19, 2005
Posts: 10
Thank you for the advice, nick. As you pinpointed, it should have been



But...I still have two problems.

1. I have no idea how to creat a constructor for "delete" option

2. I have no idea how to creat a client using these classes.
I mean...I don't know how to make this run.
I read through several books, but I was not able to find anything
about compiling this "Tree" thing.

If you could give me some help with those problems, I will be truely grateful. Thank you.
[ March 19, 2005: Message edited by: Taylor Woods ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Did you write this TreeNode class yourself? If so you should understand how each method works. To write a client for the class, start with "public static void main(String[] args)". Then in the main() method write the code to accomplish each of the tasks you described above. If you encounter problems, feel free to come back with specific questions. Typically you should post the code you have tried (in this case the main() method). Also include compiler errors and output, if any.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Taylor Woods
Greenhorn

Joined: Mar 19, 2005
Posts: 10
Thank you~ Layne...

Actually, the code I posted was written by follwoing directions in one of
"Java Guide Book"...So I couldn't fully understand the code...

And here is my client I tried to creat...



When I compiled it, I keep getting error messages that says

"reeNode.java:54: not a statement
TreeNode lead, tail;"

and

"TreeNode.java:54: ';' expected
TreeNode lead, tail;"

But I cannot get it fixed...
And, in addition, could you show me how to creat a constructor for
deleting nodes from the tree? I couldn't find any info. about it in any
"Java Guide Books"... Thank you
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi,

When you typed in the TreeNode code from your book, did you type in all of the brackets carefully?

Check particularly the insert method.

There is another error in the insert method also. You have:



which should probably be,

Taylor Woods
Greenhorn

Joined: Mar 19, 2005
Posts: 10
Thanks Horatio...I fixed the code as you showed. But...now, it shows so many errors that says

"TreeNode.java:68: cannot find symbol
symbol : variable tail
location: class TreeNode
if (num < tail.getValue())"

and

"TreeNode.java:69: cannot find symbol
symbol : variable tail
location: class TreeNode
tail.setLeft(newNode);"

and

"TreeNode.java:89: cannot find symbol
symbol : variable root
location: class TreeNode
insert(15, root);" <---this error message for every number I used "insert"

and

"TreeNode.java:97: cannot find symbol
symbol : variable tree
location: class TreeNode
tree.preOrder();"

Error messages are as shown above. Could you show me the way to fix these?

Oh...and it will be great if you can also show me the way to creat a constructor to delete a node from the tree.

Thank you.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi,

So that we can help you, please post the updated code for the TreeNode class. Remember to use the CODE tags (click the 'CODE' button at the bottom of the window and paste your code inbetween the two tags).

Thanks
Taylor Woods
Greenhorn

Joined: Mar 19, 2005
Posts: 10
Oh...I forgot that~
Here is my updated code:

Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

There's really three problems here:

1). tail is undefined.

2). root is undefined

3). tree is undefined.

For the first one, tail simply went out of scope. You declared it within the block

and are trying to refrence it after that close brace. As far as the compiler is concerned, the variable tail no longer exists.

For the other two, you need to define root and tree somewhere; I don't see them declared anywhere....


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi again Taylor,

I think you need to triple check that you have typed in the code exactly as it appears in your book. Copy the exact formatting, bracketing etc.

As Joel points out, some of your errors are due to scope, which is a hint that your bracketing may not be correct.

For example,



I'm sure that a textbook wouldn't have this mixed style of bracketing. It is more likely that there would be brackets around the contents of the if block and the else block.

Are you sure you have typed this in exactly as it is in your book?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about TreeNode