Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding ChildNodes  RSS feed

 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure and Merry Christmas.
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merry Christmas to you as well!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!