• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting Binary Search Tree into an array

 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to declare before the class so that it can be used by all methods for that class.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i figured out the "i" problem:

 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm...am i getting close?

 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic