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.
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).
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?
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:
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.
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.
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.