Exemplo n.º 1
0
//
//  Remove largest item in BST subtree rooted at ptr
//  Returns pointer to modified subtree.
//  Pointer to max node found is stored in reference parameter max_pptr
//
tnode *bst_remove_max(tnode *ptr, tnode **max_pptr) {
   tnode *left ;

   // NULL pointer is an error condition
   if (ptr == NULL) {
      fprintf(stderr, "Can't remove max node from NULL pointer!\n") ;
      exit(1) ;
   }

   // If right subtree exists, max is in right subtree
   if (ptr->right != NULL) {

      ptr->right = bst_remove_max(ptr->right, max_pptr) ;
      ptr->size-- ;

      return ptr ;   // root of modified subtree is current node


   } else {   //  no right subtree

      // if no right subtree, then current node is max

      left = ptr->left ;    // save

      ptr->left = NULL ;    // clean up current node
      ptr->right = NULL ;
      ptr->size = 1 ;

      *max_pptr = ptr ;	    // max is current node

      return left ;         // modified subtree is left subtree

   }
}
Exemplo n.º 2
0
// write definition for bst_remove_max here
void bst_remove_max(btNode* &root, int &rootData)
{
    if (root->right == 0) 
    {
        rootData = root->data;
        btNode* oldRoot = root;
        root = root->left;
        delete oldRoot;
    }
    else
        bst_remove_max(root->right, rootData);
}
Exemplo n.º 3
0
void bst_remove_max(binary_tree_node*& root_ptr, int& removed)
// Precondition: root_ptr is a root pointer of a non-empty binary
// search tree.
// Postcondition: The largest item in the binary search tree has been
// removed, and root_ptr now points to the root of the new (smaller)
// binary search tree. The reference parameter, removed, has been set
// to a copy of the removed item.
{
    if (root_ptr->right != nullptr)
        bst_remove_max(root_ptr->right, removed);

    else
    {
        removed = root_ptr->data;
        binary_tree_node* old_root_ptr = root_ptr;
        root_ptr = root_ptr->left;
        delete old_root_ptr;
        old_root_ptr = nullptr;
    }

}
Exemplo n.º 4
0
// write definition for bst_remove here
bool bst_remove(btNode* &root, int remInt)
{   
    if (root == 0) // tree empty or doesn't contain the remInt
        return false;

    if (remInt < root->data) // left child
        bst_remove(root->left, remInt);
    else if (remInt > root->data) // right child
        bst_remove(root->right, remInt);
    else if (remInt == root->data)
    {
        if(root->left == 0 && root->right == 0) // no children
        {
            btNode* oldRoot = root;
            root = 0;
            delete oldRoot;
        }
        else if (root->left == 0 || root->right == 0)
        {
            if (root->right == 0) // left child only
            {
                btNode* oldRoot = root;
                root = root->left;
                delete oldRoot;
            }
            else // right child only
            {
                btNode* oldRoot = root;
                root = root->right;
                delete oldRoot;
            }
        }
        else // two children
            bst_remove_max(root->left, root->data);
        return true; // node successfully removed
    }
}
Exemplo n.º 5
0
bool bst_remove(binary_tree_node*& root_ptr, const int& target)
// Precondition: root_ptr is a root pointer of a binary search tree
// or may be nullptr for an empty tree).
// Postcondition: If target was in the tree, then one copy of target
// has been removed, and root_ptr now points to the root of the new
// (smaller) binary search tree. In this case the function returns true.
// If target was not in the tree, then the tree is unchanged (and the
// function returns false).
{
    if (root_ptr == nullptr) return false;
    if (target < root_ptr->data)
        return bst_remove(root_ptr->left, target);
    if (target > root_ptr->data)
        return bst_remove(root_ptr->right, target);
    if (root_ptr->left == nullptr)
    {
        binary_tree_node* old_root = root_ptr;
        root_ptr = root_ptr->right;
        delete old_root_ptr;
        return true;
    }
    bst_remove_max(root_ptr->left, root_ptr->data);
    return true;
}
Exemplo n.º 6
0
tnode *bst_remove(tnode *ptr, int key, int *pfound) {

   tnode *max_node_ptr, *temp_ptr ;

   if (ptr == NULL) {   // not in NULL tree
      *pfound = 0 ;
      return NULL ;
   }

   if (ptr->data < key) {   // too big, remove from right

      ptr->right = bst_remove(ptr->right, key, pfound) ;
      if (*pfound) ptr->size-- ;
      return ptr ;

   } else if (key < ptr->data) { // too small, remove from left

      ptr->left = bst_remove(ptr->left, key, pfound) ;
      if (*pfound) ptr->size-- ;
      return ptr ;

   } else {   // ptr->data == key

       // just right, remove current node

      *pfound = 1 ;

       // removing current node is broken down into cases
       // depending on whether current node has any subtrees.


       // Case 1: no right subtree
       // left subtree becomes root of modified subtree
       //
      if (ptr->right == NULL) {

         temp_ptr = ptr->left ;
         ptr->left = NULL ;     // paranoid
         ptr->right = NULL ;
         free(ptr) ;
         return temp_ptr ;

      // Case 2: no left subtree
      // right subtree becomes root of modified subtree
      //
      } else if (ptr->left == NULL) {

         temp_ptr = ptr->right ;
         ptr->left = NULL ;     // paranoid
         ptr->right = NULL ;
         free(ptr) ;
         return temp_ptr ;

      // Case 3: has both subtrees
      // Remove max item from left subtree and make
      // that the new root of current subtree
      //
      } else {  // neither child is NULL

         // remove max item from left subtree
         ptr->left = bst_remove_max(ptr->left, &max_node_ptr) ;

         // max item takes over as root.
         max_node_ptr->left = ptr->left ;
         max_node_ptr->right = ptr->right ;
         max_node_ptr->size = ptr->size - 1 ;

         // Remove current item
         ptr->left = NULL ;   // paranoid
         ptr->right = NULL ;
         free(ptr) ;

         return max_node_ptr ;
      }

   }

}