I have hard time understanding how does it happen. I mean how does :

    6
   / \
  3   8
 /
2 

I get that program finds the num on the left side whos equal to null so its smallest and it prints out 2. But how does it go back and prints out 3, 6.I probably didnt understand recursion that well, but if someone could please explain this to me.
Here's function :

 void in_order_print(node* p_tree){

     if(p_tree != 0){

         if(p_tree->p_left != 0)
        in_order_print(p_tree->p_left);
        cout << p_tree->num<<" ";
        if(p_tree->p_left != 0)
        in_order_print(p_tree->p_right);


     }


 }

First of all, I think there is a bug in line 8.
I think it should be if(p_tree->p_right != 0)
To understand the code, think about these three lines of the function, starting at the top node(6). If you write it down on paper, it will be easier to keep track on where you are.

if(p_tree->p_left != 0) in_order_print(p_tree->p_left);
cout << p_tree->num<<" ";
if(p_tree->p_right != 0) in_order_print(p_tree->p_right);





  in_order_print(6):
      Because the left node of '6' is not empty, call in_order_print with ( left node of 6 = 3 ).
      in_order_print(3):
          Because the left node of '3' is not empty, call in_order_print with ( left node of 3 = 2).
          in_order_print(2):
               Left node of '2' is empty, so do nothing 
               cout << p_tree->num<<" "; -> print 2
               Right node of '2' is empty, so do nothing.
          end of in_order_print(2)
          cout << p_tree->num<<" "; -> print 3
          Right node of '3' is empty, so do nothing.
      end of in_order_print(3)
      cout << p_tree->num<<" "; -> print 6
      Because the right node of '6' is not empty, call in_order_print with ( right node of 6 = 8).
     ...

end of in_order _print(2), how and why does it print 3?

It does not. in_order_print(2) prints 2.
After that, in_order_print(3) prints 3.

thats what I was looking for. Thank you!

commented: For marking it solved! +11

I think is inorder traversing of tree in which you have to look left node, then parent node and then right node. Travesing of tree is all about recursion.
In your program program got the address of parent node then the program checks the left side of the parent node if there is something in this node then that program call itself and the left node address of the parent node will become the parent node for that call of function. Now concentrate on the calling of function when first time funtion goes to the line where it's calling it self, that funtion will stop there and go to the next function when next is executed then control will go back to that function from where the function was called and then execute the next statements of function. Read carefully I hope you will understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.