title: Binary trees, returning the next node in a preorder traversal
body: <p>I have an assignment and I need some help with a method.</p>
  
  <p>So I have a tree like this:</p>
  
  <pre><code>                A
                /   \
               B     C
             /   \ /   \
            D    E F     G
          /   \
         H      I
       /   \
     J       K
  </code></pre>
  
  <p>and my method is:</p>
  
  <pre><code>public BinaryTree preorderNext(BinaryTree t, BinaryTree v, BinaryTree prev) {       
  
      prev = t;
  
      if(t.getLeft() != null) {
          preorderNext(t.getLeft(), v, prev);
      }
  
      if(t.getRight() != null) {
          preorderNext(t.getRight(), v, prev);
      }
  
  
      if(prev == v) {
          return t;
      }
  
      return null;
  }
  </code></pre>
  
  <p>The lecturer had given a simple implementation of the tree. The class is called BinaryTree and if you want to make a node link to it then you specify what the right and left child BinaryTree node are.</p>
  
  <p>A node has two links (one to the left and the other to the right child) and there is no link to the head.</p>
  
  <p>So with the current method I am able to successful do a preorder traversal, I tested by writing the print statements of what the element stored at the node is.</p>
  
  <p>But when I run the tests, it tells me that the next preorder node from A is B, and the next preorder node from K throws a null exception but it should be I?</p>
  
  <p>Any ideas of where I am going wrong? The variable prev should hold a reference to the last node visited so if it equals to node v (which is the node I specify, to return the node after v), shouldn't I get the next node?</p>