wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Binary Search Tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Binary Search Tree" Watch "Binary Search Tree" New topic
Author

Binary Search Tree

Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
Hi,

I've written part of a ADD method for a binary search tree but it will only let me add up to the 2 nodes down the tree. Does anybody know how I can solve this problem. This in only to add nodes to leftest of the tree so code so far is:




Duplicates are allowed and put in the left subtree, i will implement nodes for the rest of tree once i get this bit done
thanks in advance!
[ April 05, 2006: Message edited by: Angela Truce ]
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Hey Angela,
Just as a matter of clarification, since you will be using the root node of the BST to start your iterations, I think that you have missed out on returning the root node on insertion. Maybe that is the problem, because without returning the root node on insertion, there is no way to set the node at the time that you create the node and insert it into the tree. Just check out that part about returning the current object or something like that. I think that you have missed out on a return statement.


Regards,
Abhishek Dwaraki
Dept of Electrical and Comp Engg
University of Massachusetts, Amherst
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Angela,
Just put in a statement after you are done with the insertion. Think that this should take care of the problem. Just dash off a post about it in any case. Cheers....
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
hi,

where exactly in the code should i put ?

thanks
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
I tried putting after each creation of a new node and got this error for :

Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
hi,
I've modified so that it loops but when i try to execute it, it doesnt do anything (my applet wont load to show the tree).

Can anyone see why?


thanks.
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Hey Angela,
U need to change the return value of the method so that the return value is a NODE value. The method signature should look something like this... and then add the . Add a return statement after you insert and after you check for null and assign root to temp also, i.e in the initial part of the code. For a cross check could you just post the method where you are calling the add method....? It would be helpful to check out the assignment there also.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Binary Search Tree