Lab8/avl_bag_lab8.c
/*
3. Rewrite avl_remove from scratch.
*/
3. Rewrite avl_remove from scratch.
3 . Rewrite avl_remove from scratch .
bool avl_remove2_helper(avl_node_t *node, avl_node_t *parent){
    if (!node) return false;
    else if (node->right == NULL && node->left == NULL) {
        if (parent->elem > node->elem) {
            parent->left = NULL;
        } else {
            parent->right = NULL;
        }
        free(node);
    } else if (node->right == NULL) {
        if (parent->elem > node->elem) {
            parent->left = node->left;
        } else {
            parent->right = node->left;
        }
        free(node);
    } else if (node->left == NULL) {
        if (parent->elem > node->elem) {
            parent->left = node->right;
        } else {
            parent->right = node->right;
        }
        free(node);
    } else {
        avl_node_t *cur = node->right;
        while(cur->left != NULL) {
            cur = cur->left;
        }
        if (parent->elem > node->elem) {
            parent->left = cur;
        } else {
            parent->right = cur;
        }
        free(node);
    }
    return true;
}
bool avl_remove2_helper ( avl_node_t * node , avl_node_t * parent ) { if ( ! node ) return false ; else if ( node -> right == NULL && node -> left == NULL ) { if ( parent -> elem > node -> elem ) { parent -> left = NULL ; } else { parent -> right = NULL ; } free ( node ) ; } else if ( node -> right == NULL ) { if ( parent -> elem > node -> elem ) { parent -> left = node -> left ; } else { parent -> right = node -> left ; } free ( node ) ; } else if ( node -> left == NULL ) { if ( parent -> elem > node -> elem ) { parent -> left = node -> right ; } else { parent -> right = node -> right ; } free ( node ) ; } else { avl_node_t * cur = node -> right ; while ( cur -> left != NULL ) { cur = cur -> left ; } if ( parent -> elem > node -> elem ) { parent -> left = cur ; } else { parent -> right = cur ; } free ( node ) ; } return true ; }
3.
3 .
// TODO: need to update height //  can use avl traverse function and pass update_height as parameter