[英]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:
该计划将:
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.