aspose file tools*
The moose likes Java in General and the fly likes Putting Binary Search Tree into an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Putting Binary Search Tree into an array" Watch "Putting Binary Search Tree into an array" New topic
Author

Putting Binary Search Tree into an array

Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
Hi all,

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?





Thanks in advance!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

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.

Henry
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
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:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

Don't worry about the index i, you have other problems to worry about first.

As for the global variable, do you want to initialize it when you instantiate the class? or when the toSortedArray() method is called?

Henry
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
I would like to declare before the class so that it can be used by all methods for that class.
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
I think i figured out the "i" problem:

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

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.

Henry
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
It's giving me a repetition of the "num array" in dos but still incorrect in the "array" part as now it's all a row of root element.

Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
Ok, now i got rid of the for loop so it only prints out the required amount. But the array is displaying the root and the rest zero's...?..hmmm

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

Angela,

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...



Again, don't do it until you understand why...

Henry
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
hmm...am i getting close?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

Originally posted by Angela Truce:
hmm...am i getting close?


YES!!! Much better...

The index is still wrong... to fix that you will have to debug it. Add a ton of println statements to see what is going on with the index.

Henry
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
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...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Putting Binary Search Tree into an array