• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Putting Binary Search Tree into an array

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i figured out the "i" problem:

 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm...am i getting close?

 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic