简体   繁体   English

如何从文件读取霍夫曼树频率

[英]How to read huffman tree frequency from a file

I have to create a huffman tree whose alphabtes(symbols, i mean "aaabbacaccba" etc.) must be given as Input.txt file at sole argument. 我必须创建一个霍夫曼树,其alphabtes(符号,我的意思是“ aaabbacaccba”等)必须作为Input.txt文件的唯一参数提供。

I then have to create the frequency corresponding to each alphabtes. 然后,我必须创建与每个字母对应的频率。 I have problem in createing frequencies. 我在创建频率时遇到问题。 I want something like this: suppose if i have to read this in input.txt: "aabcccadde" . 我想要这样的东西:假设我必须在input.txt中阅读此内容: "aabcccadde" Then my code must print like this: 然后我的代码必须这样打印:

sym:a, freq:3
sym:b, freq:1
sym:c, freq:3
sym:d, freq:2
sym:e, freq:1

My code for this is working correctly: see below: 我的代码可以正常工作:请参见下文:

   while (c != EOF && c != '\n' && c != '\r') 
   {
    Object1.data[count].symbol = c;
    if(Object1.data[count].symbol =='a')
    {
      save_a++;
    }
    if(Object1.data[count].symbol =='b')
    {
      save_b++;
    }
    if(Object1.data[count].symbol =='c')
    {
      save_c++;
    }
    if(Object1.data[count].symbol =='d')
    {
      save_d++;
    }
    if(Object1.data[count].symbol =='e')
    {
      save_e++;
    }
    count++;
    c = fgetc(input_file);
   }
    cout<< "save_a : "<<save_a <<endl<< "save_b : "<<save_b <<endl<< "save_c: "<<save_c<<endl<< "save_d : "<<save_d <<endl<< "save_e: "<<save_e<<endl;

so the output is: 所以输出是:

save_a : 5
save_b : 3
save_c: 2
save_d : 2
save_e: 3  

But the problem is when user again changes the frequency to "pqabbaddqpc". 但是问题是当用户再次将频率更改为“ pqabbaddqpc”时。 The user here added two more alphabets, where i have only counter to read the count of a,b,c,d and e only. 用户在此处添加了另外两个字母,其中我只有一个计数器只能读取a,b,c,d和e的计数。 I mean i want to create some dynamic kind of system where the user can enter any alphabets to calculate the frequencies. 我的意思是我想创建一种动态的系统,用户可以在其中输入任何字母来计算频率。

Is it possible to do achieve this ? 有可能做到这一点吗?
Any help please ? 有什么帮助吗?

Its simple if you map each alphabet according to its integer value to table which maintains its count . 如果您根据其整数值将每个字母映射到保持其计数的表,将非常简单。

int freq[256];
char c;
while((c=fgetc(fp))!=EOF) {

  freq[(unsigned int)c]++;;

}

Here as there are only 256 possible characters so there is no memory overhead as such and then you can easily extract the character present in input as they will have freq > 0 在这里,由于只有256 possible characters因此没有内存开销,然后您可以轻松提取输入中存在的字符,因为它们的freq > 0

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

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