santensuru / adaptive-huffman

Adaptive Huffman implement using c++
12 stars 10 forks source link

A bit confused about the delete_tree function #4

Open wsfy15 opened 5 years ago

wsfy15 commented 5 years ago
void delete_tree(node **tree) {
    if ((*tree)->left != NULL) {
        delete_tree(&(*tree)->left);
    }

    if ((*tree)->right != NULL) {
        delete_tree(&(*tree)->right);
    }

       // At this point,  (*tree)->left = NULL && (*tree)->right == NULL.  Right?
    if ((*tree) != NULL) {
        if ((*tree)->parent != NULL && (*tree)->left == NULL) {
            (*tree)->parent->left = NULL;
            free(*tree);
        }
        else if ((*tree)->parent != NULL && (*tree)->right == NULL) {
                        //  no chance enter here

            (*tree)->parent->right = NULL;
            free(*tree);
        }
        else if ((*tree)->right == NULL && (*tree)->left == NULL) {
            free(*tree);
            (*tree) = NULL;
        }
    }

    return;
}

I think that (*tree)->parent->right = NULL; has no chance to execute. I wonder if you can explain it to me. Thank you!