Building binary tree using operator overloading

I have tried building a tree using levelorder build function and tried to print the tree but its showing error pls correct it.

@ujj1804_1156aee80205d0bf check the corrected code https://ide.codingblocks.com/s/657596

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Thank you very much sir , i had some doubts

  1. as ‘root’ is a pointer of type node* then how are we able to enter the data by doing cin>>root as root is a pointer and not a simple variable?

  2. Why have we passed root as reference in the levelbuild function and operator>>() function?

@ujj1804_1156aee80205d0bf,
a) we r using operator overloading that’s helping to overload the operator >> to take input as we are using the same function inside >>

b) if not passed by ref then it will not get updated in the main function. read about pass by reference on net if u want to explore more

Do you mean that as inside istream&operator>>(istream & , node*&root) as we have passed *root so using overloading we can directly cin the value of root even if its a pointer ?

@ujj1804_1156aee80205d0bf, inside that basically u have passes the same input taking bfs function that’s helping actually

Sir i think the doubt is clear it works because cin>>root invokes the bfs function and there we have done int d, cin>>d due to which we have entered the numeric value , please confirm by replying.

Thanking you.

1 Like

@ujj1804_1156aee80205d0bf cool!

1 Like

Sir , i am not able to build this tree using the above code kindly tell why and how to build and print this tree.

ok sorry this is same , ill look… code i corrected na what u want exactly?

1 Like

Sir i want to print this tree in output(levelorder) but what should be the input to print this tree as some nodes dont have child.

@ujj1804_1156aee80205d0bf,
1 2 3 4 5 -1 -1 6 7 8 9 -1 -1 10 11 -1 -1 -1 -1 -1 -1 -1 -1

1 Like

Sir i am very grateful to you for replying me in such a short time , i was stuck in this for hours , doubt clear…

1 Like

@ujj1804_1156aee80205d0bf no issues :slight_smile:

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.