Results 1 to 9 of 9
Thread: a BST question
- 05-12-2011, 11:56 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
a BST question
i had to return the smallest in the tree so i did
public Object minimum(){ // returns smallest in tree
return min(this.root);
}
public Object min(BSTNode a){
while (a.left!=null)
min(a.left);
return a.data;
}
the second one finds the smalllest and goes to return but then it jumps back to the loop for an endless loop
any ideas anyone ?
- 05-12-2011, 12:41 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
Make that:
This version just lets the recursion do the work; a purely iterative solution would run as follows:Java Code:public Object min(BSTNode a){ if (a.left!=null) return min(a.left); return a.data; }
Your version tried to combine both methods (and fails as you have noticed).Java Code:public Object min(BSTNode a){ while (a.left!=null) a= a.left; return a.data; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-12-2011, 12:57 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
duh , yea man no wonder it didn't stop
thanks a lot for clarifying it
- 05-12-2011, 01:03 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
ok it's not looping but it's still going to the minimum
goes to the return and then goes back to the recurrsion part
in the end it returns the root of the tree (the one from the start )
- 05-12-2011, 01:16 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
- 05-12-2011, 01:28 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
same as before only with an if , i add a print of the data it goes to the minimun but then goes back to the start and return the root .Java Code:public Object minimum(){ // returns smallest in tree return min(this.root); } public Object min(BSTNode a){ if (a.left!=null) min(a.left); return a.data; }
- 05-12-2011, 01:30 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
bleeeh just figured it out...
it was missing a.data = min(data.left)
:)
- 05-12-2011, 01:55 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
- 05-12-2011, 02:34 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Similar Threads
-
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
question
By berkeley in forum New To JavaReplies: 5Last Post: 06-03-2010, 08:56 PM -
RMI question
By alvandrood in forum New To JavaReplies: 1Last Post: 09-14-2009, 12:36 PM -
JSP Question
By maheshkumarjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 10:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks