#include #include using namespace std; #include "apstring.h" #include "bitops.h" #include "globals.h" Tree * readTree(ibstream & input) // pre: inorder traversal of tree stored in input // post: read tree and return root { int val; Tree * left; Tree * right; if (input.readbits(1,val)) // read a bit { if (val == 0) // internal node, read subtrees { left = readTree(input); right = readTree(input); return new Tree(0,0,left,right); } else // leaf, read value { input.readbits(BITS_PER_WORD + 1,val); return new Tree(val,0,0,0); } } return 0; // should never reach this if file is ok } int main(int argc, char * argv[]) { apstring filename; if (argc > 1) { filename = argv[1]; if (argc == 3) { setsize(atoi(argv[2])); } } else { cout << "Enter filename: "; cin >> filename; } ibstream input(filename.c_str()); apstring outname = filename + ".unhf"; obstream output(outname.c_str()); Tree * root = readTree(input); Tree * current = root; int val; while (input.readbits(1,val)) // reading a bit succeeds { if (val == 0) // read a zero, go left { current = current->left; } else { current = current->right; } if (current->left == 0 && current->right == 0) // at a leaf? { if (current->info == PSEUDO_EOF) // all done, stop { break; } else // write the char { output.writebits(BITS_PER_WORD, current->info); } current = root; } } return 0; }