Results 1 to 9 of 9
Thread: a BST question
- 05-12-2011, 12:56 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 46
- 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, 01:41 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Make that:
Java Code:public Object min(BSTNode a){ if (a.left!=null) return min(a.left); return a.data; }
Java Code:public Object min(BSTNode a){ while (a.left!=null) a= a.left; return a.data; }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-12-2011, 01:57 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 46
- Rep Power
- 0
duh , yea man no wonder it didn't stop
thanks a lot for clarifying it
- 05-12-2011, 02:03 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 46
- 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, 02:16 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-12-2011, 02:28 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 46
- Rep Power
- 0
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, 02:30 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 46
- Rep Power
- 0
bleeeh just figured it out...
it was missing a.data = min(data.left)
:)
- 05-12-2011, 02:55 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-12-2011, 03:34 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 01:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 09:49 AM -
question
By berkeley in forum New To JavaReplies: 5Last Post: 06-03-2010, 09:56 PM -
RMI question
By alvandrood in forum New To JavaReplies: 1Last Post: 09-14-2009, 01:36 PM -
JSP Question
By maheshkumarjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 11:51 AM
Bookmarks