简体   繁体   English

从最小堆 C++ 创建霍夫曼代码树

[英]Create Huffman Code Tree from min-heap C++

Say you have a C++ program that must read in text from a given .txt file.假设您有一个 C++ 程序,它必须从给定的 .txt 文件中读取文本。 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树的每个内部节点都会有标签 I:xxx,其中 xxx 是 int 标签,叶子有 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该程序最终构造了一个表,其中包含存储为字符串的每个字符的编码(基于 ASCII,而不是真正的 .bin 版本)

I suppose I would store each node within a huffman class like this:我想我会将每个节点存储在一个 huffman 类中,如下所示:

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!任何帮助将非常感激!

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.现在遍历字符串并增加与它们的 ASCII 值相对应的数组元素。 (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') (您可能需要减去初始 ASCII 值的某个基数,因此您开始计算 'a' 的第一个数组元素)

This part can also be done rather easily in C, as it uses chars and ints as equivalents.这部分也可以很容易地在 C 中完成,因为它使用字符和整数作为等价物。

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 :)祝你好运和快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM