. . . // Constructor example and node definition // Part of the binary_tree.h template class binary_tree { public: binary_tree() {} binary_tree(const T & newdata) { node * n = new node; n->data = newdata; n->lt = n->rt = n->pt = 0; } ~binary_tree() {} T & root() {} void combine(binary_tree & t1, binary_tree & t2) {} iterator begin() const {} iterator end() const {} private: struct node { T data; node * lt, * rt, * pt; }; }; . . . // Using the binary tree // Part of the huffman encode algorithm typedef std::pair node_type; typedef binary_tree tree_type; // Read in the plaintext, // counting the frequency of each character. char b; int n_chars = 0; int character_count[256]; while (in.get(b)) { ++n_chars; ++character_count[(int) b]; } // Make a tree for each character // and push it on the priority queue tree_type::iterator leaf_index[256]; std::priority_queue, pq_comparator> pq; for (i=0; i < 256; ++i) { if (character_count[i] != 0) { tree_type * tree = new tree_type(std::make_pair(character_count[i], (char) i)); pq.push(tree); leaf_index[i] = tree->begin(); } } . . . // Binary operator that defines the ordering of the priority queue struct pq_comparator { bool operator()(tree_type * t1, tree_type * t2) { return t1->root().first > t2->root().first; } }; . . . // Example of how to write the tree to the compressed file // Function should be part of the huffman encode algorithm void write_tree(std::ostream & o, tree_type::iterator i) { if (i.is_leaf()) { o << "."; o.put((*i).second); return; } else { o << "("; write_tree(o, i.left()); o << ")("; write_tree(o, i.right()); o << ")"; } } . . . // Example of how to read the tree from the compressed file // and re-construct the tree // This is a function that should be part of the huffman decode algorithm tree_type * read_tree(std::istream & in) { char b; in.get(b); if (b == '.') { in.get(b); tree_type * t = new tree_type(std::make_pair(0, (unsigned char) b)); return t; } else { tree_type * t1 = read_tree(in); in.get(b); // ')' in.get(b); // '(' tree_type * t2 = read_tree(in); in.get(b); // ')' tree_type * t = new tree_type(std::make_pair(0, (unsigned char) 0)); t->combine(*t1, *t2); delete t1; // This highly depends on your exact tree implementation delete t2; // This highly depends on your exact tree implementation return t; } }