my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Binary Search Tree - root node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Binary Search Tree - root node " Watch "Binary Search Tree - root node " New topic
Author

Binary Search Tree - root node

Christopher Donovan
Greenhorn

Joined: Nov 03, 2012
Posts: 6
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

Look very closely at these 3 lines (12, 13, 14 in your posted code):


Do you not see 3 problems there?
Christopher Donovan
Greenhorn

Joined: Nov 03, 2012
Posts: 6
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

Joined: Nov 03, 2012
Posts: 6
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 03, 2012
Posts: 6


i will try this code...


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Nov 03, 2012
Posts: 6
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 Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

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.


True person is moral, false is right!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary Search Tree - root node