// destroy a tree, have to recursively destroy the left and right subtrees // void tree_destruct(tnode *node) { if (node == NULL) { return; } tree_destruct(node->left); tree_destruct(node->right); free(node); }
// destroy a node, have to destroy the tree, then the node // void node_destruct(lnode *node) { if (node != NULL) { tree_destruct(node->tree); free(node); } }
int main(int argc, char **argv) { if(argc != 3) { fprintf(stderr, "Incorrect number of arguments\n"); return EXIT_FAILURE; } FILE *infptr = fopen(argv[1], "r"); if(infptr == NULL) { fprintf(stderr, "Cannot open the file\n"); return EXIT_FAILURE; } tnode *huffman = Build_huffman_tree(infptr); if(huffman == NULL) { fprintf(stderr, "Cannot build the tree\n"); return EXIT_FAILURE; } FILE *outfptr = fopen(argv[2], "w"); if(outfptr == NULL) { fprintf(stderr, "Cannot write the file\n"); outfptr = stdout; } int flag = Huffman_decoding(huffman, infptr, outfptr); fclose(infptr); if(flag == 0) { fprintf(stderr, "Can't decode successfully\n"); return EXIT_FAILURE; } if(outfptr != stdout) { fclose(outfptr); } tree_destruct(huffman); return EXIT_SUCCESS; }