SHARE
TWEET

Untitled

a guest Feb 26th, 2020 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char * removeSmallest (  bst_node ** root ){
  2.     //check if root is null
  3.     if ((*root) == NULL) {
  4.         return NULL;
  5.     }
  6.  
  7.     //check if root is smallest element in tree
  8.     if ((*root)->left == NULL){
  9.         //create char to hold smallest value which is in root
  10.         char *value = (*root)->data;
  11.         //create a new root to replace root since current root is smallest element in tree
  12.         bst_node * newRoot = (*root)->right;
  13.         free(*root);
  14.         if(newRoot == NULL){
  15.             *root = NULL;
  16.         }
  17.         *root = newRoot;
  18.         return value;
  19.     }
  20.  
  21.     //create node to mark current location
  22.     bst_node* current = *root;
  23.     //while current has a left node, keep traversing left
  24.     while(current->left != NULL && current->left->left != NULL) {
  25.         current = current->left;
  26.     }
  27.    
  28.     //keep tack of smallest node to remove
  29.     bst_node * smallest = current->left;
  30.     char *value = smallest->data;
  31.     current->left = smallest->right;
  32.  
  33.     free(smallest);
  34.     smallest = NULL;
  35.  
  36.     //return smallest value
  37.     return value;
  38.  
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top