title: Return parent of node in Binary Tree
body: <p>I'm writing a code to return the parent of any node, but I'm getting stuck. I don't want to use any predefined ADTs.</p>
  
  <pre><code>//Assume that nodes are represented by numbers from 1...n where 1=root and even 
  //nos.=left child and odd nos=right child.
  public int parent(Node node){
      if (node % 2 == 0){
         if (root.left==node)
         return root;
      else
         return parent(root.left);
      }
      //same case for right
  }
  </code></pre>
  
  <p>But this program is not working and giving wrong results. My basic algorithm is that the program starts from the <code>root</code> checks if it is on <code>left</code> or on the <code>right</code>. If it's the child or if the <code>node</code> that was queried <code>else</code>, recurses it with the child.</p>