title: Understanding stack unwinding in recursion (tree traversal)
body: <p>I am writing a program to traverse a binary search tree.Here's my code:</p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>public class Main {
  
  public static void main(String[] args) {
   BinaryTree binaryTree = new BinaryTree();
  binaryTree.add(50);
  binaryTree.add(40);
  binaryTree.add(39);
  binaryTree.add(42);
  binaryTree.add(41);
  binaryTree.add(43);
  binaryTree.add(55);
  binaryTree.add(65);
  binaryTree.add(60);
  binaryTree.inOrderTraversal(binaryTree.root);
  } 
  }
  </code></pre>
  
  <p><strong>Node.java</strong></p>
  
  <pre><code> public class Node {
   int data;
   Node left;
   Node right;
   Node parent;
  
  
  public Node(int d)
  {
  data = d;
  left = null;
  right = null;
  }
  }
  </code></pre>
  
  <p><strong>BinaryTree.java</strong></p>
  
  <pre><code>public class BinaryTree {
  Node root = null;
  public void add(int d)
  {
  Node newNode =  new Node(d);
  if(root!=null)
  {
  
  
      Node futureParent = root;
      while(true)
      {
      if(newNode.data &lt; futureParent.data)      //going left
      {
          if(futureParent.left == null)
          {
              futureParent.left = newNode;
              newNode.parent = futureParent;
              break;
          }
          futureParent = futureParent.left;
  
      }
      else
      {
          if(futureParent.right == null)
          {
              futureParent.right = newNode;
              newNode.parent = futureParent;
              break;
          }
          futureParent = futureParent.right;
      }
  
      }
  
  }
  else
  {
      root = newNode;
  }
  }
  public void inOrderTraversal(Node node)
  {
  if(node!=null)
  {
  inOrderTraversal(node.left);
  System.out.println(node.data);
  inOrderTraversal(node.right);
  }
  }
  }
  </code></pre>
  
  <p>I understand the addition process perfectly but I have trouble understanding the traversal.
  Now, the tree I am working with, for better reference is this:</p>
  
  <p><img src="https://i.stack.imgur.com/PM6V2.png" alt="BinaryTree"></p>
  
  <p>The first statement in the <code>inOrderTraversal()</code> function visits 50,40 then 39 and finally hits null making the if condition false after which 39 is printed and is searched for a right child.After this the first statement stops executing and the stack unwinds for the 2nd and 3rd statements(<code>inOrderTraversal(node.right)</code> and <code>print(node.data)</code>) which leads to printing 40 and traversing to 41 which is the part I dont understand, i.e. how does the compiler restart statement 1 (<code>inOrderTraversal(node.left)</code>) after it has stopped executing as soon as there is fresh stuff in the stack.</p>