I've written this program that will search through a Binary Search Tree to put all nodes into an array. The problem is, it does it fine when I want to print it onto the dos window, but I need to print it to an applet and the statement
is not matching the
part which is the correct output. Can someone figure out how I can make return done match the output?
You are making an assumption that just because the debugging messages list the nodes in order, that you actually correctly put it into an array.
Take a look at your solution again. You will see that at no time have you completely built an array from the tree. The most you put into any array is one node.
thanks, how do I put all of the items into the node array? I tried decalring a new array in the first method and then let the second method return an int so that it can be put into this array but it didnt seem to work. Any suggestions? thanks.
Originally posted by Angela Truce: thanks, how do I put all of the items into the node array? I tried decalring a new array in the first method and then let the second method return an int so that it can be put into this array but it didnt seem to work. Any suggestions? thanks.
Well, you could pass the array down recursively. Or you could work with a global array. Either is fine.
BTW, you may not notice it because it doesn't work yet, but the calculation of the index "i" is not completely correct.
Hi, I tried to implement a global array (i don't understand what you mean by calculation of i...is it wrong at the node[i]=n.getElement() bit???) but it gives me a null pointer exception:
Originally posted by Angela Truce: I would like to declare before the class so that it can be used by all methods for that class.
Okay... I guess we lost track of what we are doing. These are *not* supposed to be global variables. Remember, these happen to be global variables because you couldn't figure out how to pass a local variable in a recursive manner.
Anyway let's forget this whole mess. Let's do it the correct way -- you'll have to learn it anyway.
Go back to the original example. Declare an array in the toSortedArray() method, and then pass it recursively to the inOrder() method.
I seem to be at a lost. It seems that every iteration is worse than the previous one. And I believe the reason for this is that you are making the changes that I am requesting -- but you don't seem to understand why.
Don't do that. If you don't understand "why" with the hints that I am giving you, don't do it.
How do you pass an array around recursively? One way is to use parameters...
it's giving me zero everytime so i added a few guesses that it was reseting "i" everytime inOrder was called. Still can't see what's going on though...