title: BinarySearchTree not adding elements to tree
body: <p>Why is it that if I put </p>
  
  <pre><code>tree.addElement(10, tree.root);
  </code></pre>
  
  <p>it works, but if I do it again as in</p>
  
  <pre><code>tree.addElement(20, tree.root);
  </code></pre>
  
  <p>it doesn't work? I just want to add elements to the tree. What is wrong with my method? The error the compiler gives me is simply:</p>
  
  <p>at <code>LinkedBinarySearchTree.addElement(LinkedBinarySearchTree.java:68)</code></p>
  
  <pre><code>public void addElement(T element, BinaryTreeNode node) 
  {
      if (!(element instanceof Comparable))
          throw new NonComparableElementException("LinkedBinarySearchTree");
  
      Comparable&lt;T&gt; comparableElement = (Comparable&lt;T&gt;)element;
  
      if (isEmpty())
          root = new BinaryTreeNode&lt;T&gt;(element);
      else 
      {
          if (comparableElement.compareTo(root.getElement()) &lt; 0)
          {
              if (root.getLeft() == null) 
                  this.getRootNode().setLeft(new BinaryTreeNode&lt;T&gt;(element));
              else
                  addElement(element, root.getLeft());
          }
          else
          {
              if (root.getRight() == null) 
                  this.getRootNode().setRight(new BinaryTreeNode&lt;T&gt;(element));
              else
                  addElement(element, root.getRight());
          }
      }
      modCount++;
  
  }
  </code></pre>
  
  <p>Here is the rest of the code:</p>
  
  <pre><code> public class BinaryTreeNode&lt;T&gt;
  {
  protected T element;
  protected BinaryTreeNode&lt;T&gt; left, right;
  
  /**
   * Creates a new tree node with the specified data.
   *
   * @param obj the element that will become a part of the new tree node
   */
  public BinaryTreeNode(T obj) 
  {
      element = obj;
      left = null;
      right = null;
  }
  
  /**
   * Creates a new tree node with the specified data.
   *
   * @param obj the element that will become a part of the new tree node
   * @param left the tree that will be the left subtree of this node
   * @param right the tree that will be the right subtree of this node
   */
  public BinaryTreeNode(T obj, LinkedBinaryTree&lt;T&gt; left, LinkedBinaryTree&lt;T&gt; right) 
  {
      element = obj;
      if (left == null)
          this.left = null;
      else
          this.left = left.getRootNode();
  
      if (right == null)
          this.right = null;
      else
          this.right = right.getRootNode();
  }
  
  /**
   * Returns the number of non-null children of this node.
   *
   * @return the integer number of non-null children of this node 
   */
  public int numChildren() 
  {
      int children = 0;
  
      if (left != null)
          children = 1 + left.numChildren();
  
      if (right != null)
          children = children + 1 + right.numChildren();
  
      return children;
  }
  
  /**
   * Return true if this node is a leaf and false otherwise.
   *
   * @return true if this node is a leaf and false otherwise
   */
  public boolean isLeaf() 
  {
      return (numChildren() == 0);
  }
  
  /**
   * Return the element at this node.
   *
   * @return the element stored at this node
   */
  public T getElement() 
  {
      return element;
  }
  
  /**
   * Return the right child of this node.
   *
   * @return the right child of this node
   */
  public BinaryTreeNode&lt;T&gt; getRight() 
  {
      return right;
  }
  
  /**
   * Sets the right child of this node.
   *
   * @param node the right child of this node
   */
  public void setRight(BinaryTreeNode&lt;T&gt; node) 
  {
      right = node;
  }
  
  /**
   * Return the left child of this node.
   *
   * @return the left child of the node
   */
  public BinaryTreeNode&lt;T&gt; getLeft() 
  {
      return left;
  }
  
  /**
   * Sets the left child of this node.
   *
   * @param node the left child of this node
   */
  public void setLeft(BinaryTreeNode&lt;T&gt; node) 
  {
      left = node;
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>* Creates an empty binary search tree.
   */
  public LinkedBinarySearchTree() 
  {
      super();
  }
  
  /**
   * Creates a binary search with the specified element as its root.
   *
   * @param element the element that will be the root of the new binary
   *        search tree
   */
  public LinkedBinarySearchTree(T element) 
  {
      super(element);
  
      if (!(element instanceof Comparable))
          throw new NonComparableElementException("LinkedBinarySearchTree");
  }
  
  /**
   * Adds the specified object to the binary search tree in the
   * appropriate position according to its natural order.  Note that
   * equal elements are added to the right.
   *
   * @param element the element to be added to the binary search tree
   * @return 
   */
  public void addElement(T element, BinaryTreeNode node) 
  {
      if (!(element instanceof Comparable))
          throw new NonComparableElementException("LinkedBinarySearchTree");
  
      Comparable&lt;T&gt; comparableElement = (Comparable&lt;T&gt;)element;
  
      if (isEmpty())
          root = new BinaryTreeNode&lt;T&gt;(element);
      else 
      {
          if (comparableElement.compareTo(root.getElement()) &lt; 0)
          {
              if (root.getLeft() == null) 
                  this.getRootNode().setLeft(new BinaryTreeNode&lt;T&gt;(element));
              else
                  addElement(element, root.getLeft());
          }
          else
          {
              if (root.getRight() == null) 
                  this.getRootNode().setRight(new BinaryTreeNode&lt;T&gt;(element));
              else
                  addElement(element, root.getRight());
          }
      }
      modCount++;
  
  }
  
  /**
   * Adds the specified object to the binary search tree in the
   * appropriate position according to its natural order.  Note that
   * equal elements are added to the right.
   *
   * @param element the element to be added to the binary search tree
   */
  public  LinkedBinarySearchTree(T element, BinaryTreeNode&lt;T&gt; node) 
  {
      Comparable&lt;T&gt; comparableElement = (Comparable&lt;T&gt;)element;
  
      if (comparableElement.compareTo(node.getElement()) &lt; 0)
      {
          if (node.getLeft() == null) 
              node.setLeft(new BinaryTreeNode&lt;T&gt;(element));
          else
              addElement(element, node.getLeft());
      }
      else
      {
          if (node.getRight() == null) 
              node.setRight(new BinaryTreeNode&lt;T&gt;(element));
          else
              addElement(element, node.getRight());
      }
  }
  </code></pre>