1

Say you have a C++ program that must read in text from a given .txt file. The program will:

  • compute number of occurrences of each char in the file, then each unique char (and its frequency) will be stored as a new tree node
  • The program then builds a min-heap containing these nodes, then the Huffman Code Tree is built using this min-heap.
  • Traversals (pre-order and in-order) will be written to output file. Each internal node of tree will have label I:xxx, where xxx is the int label, and leaves have L:xxx
  • The program finally constructs a table that contains the encoding (ASCII based, not true .bin version) of each character stored as a string

I suppose I would store each node within a huffman class like this:

struct CharNode {

      char value;
      int frequency;
      bool internal;
      int label;
      CharNode *parent;
      CharNode *left_child;
      CharNode *right_child;
};

I am not exactly sure where to proceed. Any help would be much appreciated!

bama_programmer
  • 185
  • 1
  • 1
  • 11

1 Answers1

1

Start by using an array, that is long enough to have one element for every char that should be recognized by your application. Now go over the string an increment the array elements corresponding to their ASCII values. (You might need to subtract a certain base amount of the initial ASCII value, so you start counting on the first array element for 'a')

This part can also be done rather easily in C, as it uses chars and ints as equivalents.

Once done you have all characters an their number of occurrences. Now you can iterate through the array once an start building your tree nodes. And the use your heap algorithms on that tree.

Or just sort the array an proceed to build your tree from there on out.

Good Luck and happy coding :)

Nils
  • 111
  • 1
  • 6
  • I am very confused on how to build a min heap from this array, and how this would even translate into a huffman tree :( – bama_programmer Nov 13 '15 at 10:41
  • make one node out of each array element and put it into a structure that is ordered by the num of ocurrences. Create a new node, make the two smalles nodes it's children and set the number of this node to be the sum of both it's children. Remove the child nodes from the structure, reinsert this new node into it, sort it again and repeat until you have just one node left. It will/should be the root of your huffman tree – Nils Nov 13 '15 at 11:18