title: Finding the parent of a node in a Binary tree
body: <p>I am trying to write a method to find the parent of a given node. Here's my method.<br>
  I created a <code>BinaryNode</code> object r which initially refers to root.</p>
  
  <pre><code>    public BinaryNode r=root;
      public BinaryNode parent(BinaryNode p){
      BinaryNode findParent=p;        
          if (isRoot(findParent) || r==null){
                  return null;
          }
          else{
              if(r.left==findParent || r.right==findParent)
                  return r;
              else{
                  if (r.element&lt;findParent.element)
                      return parent(r.right);
                  else
                      return parent(r.left);
              }
          }
      }  
  </code></pre>
  
  <p>THis code  doesn't work properly .I think that's because r is a null object.Because when I do   </p>
  
  <pre><code>if (isRoot(findParent) || r==null){
                  System.out.println(r==null);
                  return null;}  
  </code></pre>
  
  <p><code>r==null</code> evaluates to <code>true</code>.How come that happen because I have inserted nodes as  </p>
  
  <pre><code>public static void main (String args[]){
          BinaryTree t=new BinaryTree();
          t.insert(5);
          t.insert(t.root,4);
          t.insert(t.root,6);
          t.insert(t.root,60);
          t.insert(t.root,25);
          t.insert(t.root,10);  
  </code></pre>
  
  <p>and the root is not null.<br>
  Can some one please point out why that happens and if what I am trying to do in order to find the parent  node is logically correct.</p>