Forums Register Login

Binary Search Tree - root node

+Pie Number of slices to send: Send
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!!

+Pie Number of slices to send: Send
Look very closely at these 3 lines (12, 13, 14 in your posted code):


Do you not see 3 problems there?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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,
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send


i will try this code...


+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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....
+Pie Number of slices to send: Send
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.
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1215 times.
Similar Threads
recursive method to balance a binary tree
Homework Trouble: Not understood syntax error, Instance of a generic BST class
Create an object?
how do I get the current node selection in a Jtree?
Binary trees problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:56:17.