• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Binary Search Tree

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abhishek Dwaraki
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

where exactly in the code should i put ?

thanks
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried putting after each creation of a new node and got this error for :

 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic