38C. Removing the Smallest Value from a Binary Search Tree

Suppose that you want to remove the smallest value from a nonempty binary search tree. That is equivalent to removing the leftmost node, which you have already seen.

But we need a little more here: removeSmallest must return the value that was in the node that was removed. Since that cannot be done when there are no nodes, we require at least one node.

  //====================================================
  //               removeSmallest
  //====================================================
  // removeSmallest(T) removes the smallest value from
  // binary search tree T and returns the value that
  // was removed.
  //
  // Requirement: T must not be an empty tree.
  //====================================================

  int removeSmallest(Tree& T)
  {
    if(T->left == NULL)
    {
      int  result = T->item;
      Tree p      = T;

      T = T->right;
      delete p;
      return result;
    }
    else 
    {
      return removeSmallest(T->left);
    }
  }