Forums Register Login

Adding ChildNodes

+Pie Number of slices to send: Send
Hi, i am having difficulties programming a method that adds a value to depth +1 of my current tree.
Say i have a tree

(+)
(+) (/)
() () () () This depth should be (null)

how would i implement a code that adds a value with my getFunction method on the 3rd level of my tree

This is what i have so far.
+Pie Number of slices to send: Send
Forget the code for a moment and get a pen and paper and write down your native language in detail how you would do it if you were constructing the tree from values someone else was passing to you..
Once you have a detailed written description then you can turn that into code.
+Pie Number of slices to send: Send
 

Tony Docherty wrote:Forget the code for a moment and get a pen and paper and write down your native language in detail how you would do it if you were constructing the tree from values someone else was passing to you..
Once you have a detailed written description then you can turn that into code.


I am not constructing a tree by passing a value onto the method, the value is created by the getfuction method and then passed into the node where it is currently null. I have been thinking about this code for a while and i still don't quite understand why it is not working.

The code method does executes the else statement, that means that i is also creating a new child node for me. But, when i output the result it outputs the original tree(as in the original Nodes arguement).



Note: I am trying to add child nodes dynamically
+Pie Number of slices to send: Send
Once you have created the new Node are you adding it to its parent node because there is nothing in the code shown that does this?
+Pie Number of slices to send: Send
 

Tony Docherty wrote:Once you have created the new Node are you adding it to its parent node because there is nothing in the code shown that does this?



Thankyou, i have realised that, once the recursive method is called, on the stack its just returning the value and not adding to the previous .
I am not used to recursive method. How would i go around this problem? Do i have to create some kind of method and call it inside my current method?
+Pie Number of slices to send: Send
 

How would i go around this problem?


This is why I said in my first post stop coding and write out how you would do it yourself.

I can't get a definitive answer on how to fix your code as you haven't clearly explained to me what you are trying to do. Are you wanting to add an new node to every existing leaf node or just one leaf node, what code is calling this method and what value is it passing in? etc Assuming you are adding to every level and you start with the root node you probably want to assign the return value of addChildNode() to the node you are passing in as a parameter ie:
+Pie Number of slices to send: Send
 

Tony Docherty wrote:

How would i go around this problem?


This is why I said in my first post stop coding and write out how you would do it yourself.

I can't get a definitive answer on how to fix your code as you haven't clearly explained to me what you are trying to do. Are you wanting to add an new node to every existing leaf node or just one leaf node, what code is calling this method and what value is it passing in? etc Assuming you are adding to every level and you start with the root node you probably want to assign the return value of addChildNode() to the node you are passing in as a parameter ie:


Thanks for the reply, the problem is solved. However, I am a bit confused .Doesn't the code below assigns the value already when it is called recursively, since n would be what we have inputted.
+Pie Number of slices to send: Send
That assigns a new Node to 'n' but that node isn't linked to any of the other nodes, it is an orphan node. To get it to be part of your data structure you have to assign it to another nodes left or right field.
+Pie Number of slices to send: Send
 

Tony Docherty wrote:That assigns a new Node to 'n' but that node isn't linked to any of the other nodes, it is an orphan node. To get it to be part of your data structure you have to assign it to another nodes left or right field.


Thank you for your replies Tony. Has helped me a lot.
+Pie Number of slices to send: Send
My pleasure and Merry Christmas.
+Pie Number of slices to send: Send
Merry Christmas to you as well!
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 664 times.
Similar Threads
Binary Search Tree
How will you find if a binary tree is fully balanced?
<rich:tree> Problem with queueNodeExpand() to expand a Path to a leaf
sorting a TreeMap
TreeNode ClassCastException?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:31:04.