#help!! with inserting multiple values in a tree using recursion via constructor

1 messages · Page 1 of 1 (latest)

frosty dove
#

So this is the code , I've inserted manually the 3 values and haven't used recursion.. idl do know how to traverse using recursion but I'm unbeknownst about using constructor along with recursion to insert multiple values... If anyone could help please

potent parrotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

meager kestrel
#

Define constructor for inserting multiple values at once! ‘’’ node(int x, node* next): data(x), next(next) {}

#

My favourite is define a structure then keep on adding using this constructor!

bleak ferry
#

If the next post in this thread isn’t a photo with the thumb partially covering the screen, I’ll be disappointed

frosty dove
meager kestrel
#

You just have to define your constructor in such a way that it takes first parameter as data, second parameter as left node (a pointer to your class or structure) and third parameter as your right node!

#

I’m travelling so can’t help you with much resource! Happy coding bro! 😁

hearty hawk
#

There's a better way to do this, using initializer lists, the setup code becomes much smaller requiring way less new TreeNode commands:

Also, I'm using templates here to make this a more generic tree which can hold any type, but the design pattern is applicable without templates as well, you just need to make a constructor that takes two TreeNode & for left and right, which then get constructed in place

frosty dove
#

I get it now thanks a lot for your help

#

!solved

potent parrotBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity