|
|
#1 |
|
In Runtime
Join Date: Jan 2007
Posts: 129
|
This is my Binary search tree search method
Code:
public Node SearchBST(int number)
{
Node T = Root;
if (T == null)//If tree is empty
{
return null;
}
else
while(T != null){
if(number == T.data){
return T;
//Key found
}
else if(number < T.data){//Checks if the key is smaller than the current T
T = T.left;
}
else//Checks if the key is larger than the current T
T = T.right;
}
if(T == null)
return null;
//Key not found
}
__________________
http://i185.photobucket.com/albums/x203/dbzkidkev/Henriettanotext.gif i wonder why i cant put images on my signature?????!!!!! |
|
|
|
|
|
#2 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
Apart from various missing brackets, the logic looks sound to me.
One thing though, T is a REALLY bad name to use for a variable in Java - not just because it's a capital letter, but because it's the de-facto letter used for a generic type. It's also got nothing to do with Node. I'd call it "node", or at the very least "n" - definitely not T!
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|