Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char * removeSmallest ( bst_node ** root ){
- //check if root is null
- if ((*root) == NULL) {
- return NULL;
- }
- //check if root is smallest element in tree
- if ((*root)->left == NULL){
- //create char to hold smallest value which is in root
- char *value = (*root)->data;
- //create a new root to replace root since current root is smallest element in tree
- bst_node * newRoot = (*root)->right;
- free(*root);
- if(newRoot == NULL){
- *root = NULL;
- }
- *root = newRoot;
- return value;
- }
- //create node to mark current location
- bst_node* current = *root;
- //while current has a left node, keep traversing left
- while(current->left != NULL && current->left->left != NULL) {
- current = current->left;
- }
- //keep tack of smallest node to remove
- bst_node * smallest = current->left;
- char *value = smallest->data;
- current->left = smallest->right;
- free(smallest);
- smallest = NULL;
- //return smallest value
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement