Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary Search Tree - root node

 
Christopher Donovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i'm very new to java/programming in general.
I writing the code below and was wondering if when i add the first value - root, to the tree
does it remain as the root value, i not sure if that make sense, but when i run the main class
the root value is printed as a right value- ooh i don't understand??? but some help or any
code improvements would be great, thanks!!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look very closely at these 3 lines (12, 13, 14 in your posted code):


Do you not see 3 problems there?
 
Christopher Donovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look very closely at these 3 lines (12, 13, 14 in your posted code):




Do you not see 3 problems there?


I cant see three mistakes, maybe if i explain what it is i'm doing:







I know that this doesn't answer the question i was asked but is this methodology incorrect
 
Christopher Donovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taught about it and one possible mistake is that i have not pass a node argument,
so i maybe comparing newNode.data against itself each time,
i have also set the newNode.left to null and then i am checking to see if it is null.

- - to be quite honest i am totally confused and lost we are currently working on
recursion and i feel like i am going backwards.
I don't want answers but maybe just an explanation i am using big java, and java essentials (books) so
maybe just point me in the direction of a good book for data structures.

thanks,
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Donovan wrote:


No, that's not what's happening. Read the code more carefully.





Yes, that's what you're doing, but it's pointless, since you know it will always be null, because you set it to null just a few lines previous.




No, that's not what you're doing.

 
Christopher Donovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i will try this code...


 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Donovan wrote: . . . i will try this code...
No, you won’t.

That looks like somebody who is guessing; You can take 1000000 guesses, and one of them will probably give a correct result. Or you can design your code and the first attempt will be correct.
As Fred always says, turn your computer off.
As I always say, pencil paper and eraser. The last‑named is the most important
Draw a diagram of your tree (prune it so you only have three nodes. Look at their values. Can any of those nodes not have a value? Can any of the nodes have null branches? Now write down very simply, how you intend to do your task.

Now you can convert those instructions to code quite simply.
 
Christopher Donovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Christopher Donovan wrote: . . . i will try this code...
No, you won’t.

That looks like somebody who is guessing; You can take 1000000 guesses, and one of them will probably give a correct result. Or you can design your code and the first attempt will be correct.
As Fred always says, turn your computer off.
As I always say, pencil paper and eraser. The last‑named is the most important
Draw a diagram of your tree (prune it so you only have three nodes. Look at their values. Can any of those nodes not have a value? Can any of the nodes have null branches? Now write down very simply, how you intend to do your task.

Now you can convert those instructions to code quite simply.


This was the best advice that I have been giving, thanks... (Sorry that it took so long to get back)...
I have being planning, designing and testing code before writing,
does take me a little bit longer to write programs,
but I feel like I am learning rather thing just hacking at a piece code until i think it works.

So to any newbies take Campbell Ritchie's advice, design, draw, write until you know what it is you want to do....
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

You took really interesting task.

I just want to add that in binary search tree every left child is less than parent and every right child is greater than parent.
This enables to search very quickly binary search tree.
To determine which note is less or greater you need some comparison method. You may use compareTo method of Comparable interface or custom Comparator implementation.
Also note that the first added node is root node.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic