plz check why it gets in an infinite loop
Not able to print in iterative version
@kingshuk441
Your implementation is completely wrong. First of all, the iterative version for preorder, inorder and postorder are completely separate and different from each other. These are three different codes and must not be merged together. All 3 follow completely different approaches.
In your code the second and third if-else will never hit and thus only the first if will keep on executing. After some time it will hit a point when all the left nodes will be traversed and it will get stuck there.
I advise you to read about the iterative approaches of all three traversals and implement them separately. They simply cannot be implemented together in iterative approach.
cant it be implemented similar to this ? i think it can be implemented using this method and in my case there is some error needs to be rectified.
@kingshuk441
Check out the actual implementations of iterative versions of these traversals and you will notice the difference.
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.
how you said it cant be implemented see the code now it is working fine . You should give some effort by giving a try atleast and not only giving gfg link for every answer.If i have to see answer from gfg then why TA support i have taken?
plz tell why in this code we cant use inbuild Pair in C++ but we have to use a custom class which acts as pair . if yes then tell how to use if not then tell why ?
why it is working when we are allocating memory in heap and using pointers and not when we are making static allocation of memory?
plz check why this is not working?
@kingshuk441
Line 64
Create a reference object instead of copy
pair<node *, int> &t = s.top();
why sir can you explain
@kingshuk441
In your previous code, you have used this statement multiple times
top->second++;
Basically you are incrementing the second count of your node in the stack without popping it. This works since you are referring to it by pointer so the change actually gets reflected in the node.
In the new code, you made a copy object of the stack’s top node. Now when you do
top.second++;
This change is done in the copy object and the actual stack top remains unaffected.
When we create a reference object however, all changes are reflected back.
is this the only way or we do something else and how i can get that where to use reference var?
can you tell any other way by not using reference and make changes?
@kingshuk441
These are the only two ways.
Pointer method and reference variable method. No other way to make changes in the actual data.
Use reference variable whenever you want to make the changes in the actual object and do not intend to make a copy of it.
Think of reference variable as an alias. Its basically assigning a new name to the same variable.
GOT IT
THANKS FOR YOUR HELP