title: Deleting a node from a Binary Search Tree
body: <p>My Binary Search Tree program doesn't seem to be deleting anything when I call the deleteNode method. The BST is built perfectly, its just deleting the node part that doesn't work. I call it from my main like this: </p>
  
  <pre><code>System.out.println("Please enter a number you would like to delete from the tree");
      temp = reader.nextLine();
      try {
          int numTemp = Integer.parseInt(temp);
          TreeNode treeTemp = bst.deleteNode(numTemp, bst.getRoot());
          bst.setRoot(treeTemp);
      }
      catch(Throwable e){
          System.err.println(e);
      }
      bst.printInBST(bst.getRoot());
  </code></pre>
  
  <p>In my BinarySearchTree class I implement my deleteNode methods as following:</p>
  
  <pre><code>public TreeNode deleteNode(int x, TreeNode temp){
      if(temp != null){
          if(x &gt; (int)((Integer)temp.getValue())){
              temp.setLeft(deleteNode(new Integer(x), temp.getLeft()));
          }
          else if(x &lt; (int)((Integer)temp.getValue())){
              temp.setRight(deleteNode(new Integer(x), temp.getRight()));
          }
          else if(temp.getLeft() != null &amp; temp.getRight() != null){
              TreeNode temp2 = new TreeNode(temp.getRight().getValue());
              while(temp2.getLeft() != null){
                  temp2 = temp2.getLeft();
              }
              temp = temp2;
              temp.setRight(remove(temp.getRight()));
          }
      }
      return temp;
  }
  public TreeNode remove(TreeNode temp){
          if(temp.getLeft() != null){
              temp.setLeft(remove(temp.getLeft()));
              return temp;
          }
          else {
              return temp.getRight();
          }
  }
  </code></pre>