When you do find a match, you are not allowing this result to return up your recursive tree. You need to check the result of the call to right.find() and, only if it is false, should you call left.find(). If right.find returns true, you should return true straight away, otherwise you should return the result of left.find().
Well, at first glance, I see three categories of major problems here. First of all, sometimes you say something like "if (node.equals(x))" when in fact you mean "if (node.x.equals(x))". I think the best way to deal with this is just not to use equals() -- instead, give bnode a "contains" method, which makes the intent clearer, and then perhaps have contains() throw an exception if called with a bnode as an argument -- this will let you find programming problems quickly.
The second kind of problem is that you call functions recursively, but ignore the results. Your find() method will call find() on each of its subtrees, ignore their return values, then return false unconditionally. find() has to check whether the recursive call(s) succeed and if so, return true!
Thirdly, this is a pretty crazy tree in that it stores things randomly in one subtree or another. The performance of find() is therefore linear in the number of items in the tree, instead of logarithmic, since the tree has no idea where to find an object! The left/right decision should be based on something repeatable -- whether the return value of x.hashCode() is even or odd, for example.
the main problem occurs in the find method of the btree. The search is actually implemented at the bnode level and "find" in the bnode searches the entire tree for the node, and thus returns a value. It would be sufficient to call bnode find method in btree find method and return that value and that would work.
The actual problem here is that the elements are added into the B Tree but there is a simple logical error in the implementation.
Thanks hope this helps
Joined: Feb 01, 2008
what simple logical error???
i have written a search function than when it finds the object it returns true and there the method stops
if it wont find anything it will return false in the end
what the problem with that???
in this case my object is in the root so in the first IF in the btree class it was supposed to return true
a comparision is being made between the bnode and an object. which can never be equal. The point is that bnode contains an object of type "data" that should be used for comparing.
So it should be something like
The else part calls the method find from bnode which returns "true" if the element is present in the complete tree. The error here is that you are not taking care of the value returned from the find method of bnode. So the above code just returns false if the element is not present in the root. if you had something like return root.find(x); then it would have worked.
Originally posted by donaldth smithts: why it wont be reached is it because bnode will give you all the options?? ??[ March 04, 2008: Message edited by: donaldth smithts ]
No, but because the rpeceding code has a "return" in an "else" block. Follow the execution with pencil and paper and see what happens should the "if" condition NOT be fulfilled. You get the "else." Whatever follows the "return" cannot ever be executed. Hence the compiler error.
The same goes for throwing exceptions, or looping without a break: This is because this code can never ever be reached. Now of course the compiler could have ignored the code and give a warning, but Sun has decided to make it an error instead.