【问题标题】:How to store values in struct pointer variable tree (for full tree) in c++如何在c ++中将值存储在struct指针变量树(对于完整树)中
【发布时间】:2014-02-20 14:34:29
【问题描述】:

我是编程和 c++ 方面的新手(我猜即使问题的概念在 c 中也是相同的)。

我正在读取一个文件作为唯一参数,其中包含像“aabbacceaad”这样的字母(我的代码中的符号)来计算频率。 我的代码正确计算频率。我确信这一点。 问题是在我的代码中 Node *tree 指针变量(它是节点类型)。我用它来创建树。但是当我尝试从重复符号计算的频率中创建树时,这个 tree 指针变量只记住for-loop 之外的最后执行频率。 请注意我只需要使用指针而不是数组

我的意思是假设我在输入文件中有符号,例如“aabcddeeebbaa”。它的预期输出是这样的:

0  symbol:a  Freq:4  Left 0  Right 0  Index1
1  symbol:b  Freq:3  Left 0  Right 0  Index2
2  symbol:c  Freq:1  Left 0  Right 0  Index3
3  symbol:d  Freq:2  Left 0  Right 0  Index4
4  symbol:e  Freq:3  Left 0  Right 0  Index-1

但是我的代码输出是这样的:

0  symbol:e  Freq:3  Left:0  Right:0  Next:5 //Last "e" is executed,tree variable forgot a,b,c and d. 
1  symbol:e  Freq:3  Left:0  Right:0  Next:5
2  symbol:e  Freq:3  Left:0  Right:0  Next:5
3  symbol:e  Freq:3  Left:0  Right:0  Next:5
4  symbol:e  Freq:3  Left:0  Right:0  Next:-1

我这样做的完整 C++ 代码是:

#include <iostream> 
#include <stdlib.h> 
#include <fstream> 
#include <cassert> 
#include <vector>

using namespace std;

class Huffman {
    public: int data_size,
    length;
    Huffman(char * argv);
    ~Huffman() {};
    vector < char > storesym;
    vector < int > storefreq;
    struct Node
    {
        int value;
        int freq, next;
        short flag;
        unsigned char symbol;
        struct Node * left, * right;
    };
    Node * tree;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////       CONSTRUCTOR definition        ////////////////////////////////////////////////////////////////////////////////
Huffman::Huffman(char * argv) 
{
    char c;
    int count = 0;
    int flag[256]; //this flag is to know if the alphabet is already counted or not.If counted that i set it to "1" other wise it is "0".Please see below in my code
    int j = 0;
    FILE * input_file;
    int  *symbolarray;
    symbolarray=new int[30];
    char i, n;
    input_file = fopen(argv, "rb");
    c = fgetc(input_file);
    //////////////////////////////////////////////////////////////////////////////   From here to down i read the alphabbets from file taken as sole argument ///////////////////////////////////////
    while (c != EOF && c != '\n' && c != '\r')
    {
        symbolarray[count] = c;
        count++;
        c = fgetc(input_file);
    }
    fclose(input_file);

    for (i = 0; i < count; i++)
        flag[i] = {0 };
    int fcount1 = 0;
    for (i = 0; i < count; i++)
    {
        if (flag[i] == 0)
        {
            for (j = i; j < count; j++) 
            {
                if (symbolarray[i] == symbolarray[j]&& flag[j] == 0) 
                {
                    fcount1++;
                    flag[j] = 1; //**I am setting flag to 1 those alphabets to 1 so that they will not be counted again on next iteration**
                }
            }
            storesym.push_back(symbolarray[i]);
            storefreq.push_back(fcount1);
        }
        fcount1 = 0;
    }
    cout << endl;
    //////////////////////////////////////////////////////////////////////////  ERROR PRONE PART STARTS NOW  /////////////////////////////////////////////////////

    for (i = 0; i < storesym.size(); i++) 
    {
        tree = new Node;  // the problem is here this tree pointer don't store the values for all alphabets, it just remembers the last executed alphabet after this for loop.
        tree -> left = NULL;
        tree  ->right = NULL;
        tree -> symbol = storesym[i];
        tree  -> freq = storefreq[i];
        tree -> flag = 0;
        tree -> next = i + 1;
        cout<<"check1 : "<<tree -> symbol<<endl;
    } 
    ////////////////////////////////////////////////////////////////////  eror PRONE PART ENDS HERE  ///////////////////////////////////////////////////////////////////////////////////
    cout << "Here is the data read from file" << endl;
    n = storesym.size() - 1;
    for (int i = 0; i < storesym.size(); i++)
    {
        if (n == i)
        {
            tree  -> next = -1;
            cout << i << "  symbol:" << tree  -> symbol << "  Freq:" << tree  ->freq << "  Left:" << tree  -> left << "  Right:" << tree  -> right << "  Next:" << tree  -> next << endl;
            break;
        } else 
        {
            cout << i << "  symbol:" << tree  -> symbol << "  Freq:" << tree -> freq << "  Left:" << tree  -> left << "  Right:" << tree  ->right << "  Next:" << tree  -> next << endl;
        }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char * * argv)
 {
    int freq[256] = {0};
    if (argc < 2) {
        cout << "Ohh.. Sorry , you forgot to provide the Input File please" << endl;
        return (0);
    }
    Huffman Object1(argv[1]);
    return (0);
}

**请帮助我如何记住所有的“a、b、c、d 和 e”(不仅仅是最后一个“e”)?我知道只有指针可以做一些事情。

【问题讨论】:

  • 在调试器中逐行逐行执行代码。这通常很有帮助。
  • @JoachimPileborg 好的,谢谢(1)我在 Notepad++ 中工作,你应该使用哪个调试器,请给我任何名字,我有空的想法? (2) 我认为有一些与指针有关的逻辑错误,对吗?
  • @JoachimPileborg 好吧,我尝试在 for 循环中使用 cout 符号进行调试,我发现所有字母都被读取但只有最后一个字母保留在内存中在 for 循环之外。你能帮我解决这个问题吗? THnaks
  • How to Compile and Debug C++ in Notepad++ using Turbo C++ Compiler (答案基本上是——得到一个(合适的?)IDE)
  • @Dukeling 感谢您的建议,但我想这是与指针相关的逻辑问题,您知道它可能是什么吗?

标签: c++ algorithm pointers tree huffman-code


【解决方案1】:

这一点

while (c != EOF && c != '\n' && c != '\r')
{
    tree[count].symbol = c;
    count++;
    c = fgetc(input_file);
}

取消引用未初始化的指针tree
这是一个很大的禁忌,意味着您的程序在形式上是未定义的。
你只是运气不好,它没有崩溃。

你又在往下一点。

然后你在这个循环中分配:

for (i = 0; i < storesym.size(); i++) 
{
    tree = new Node;  // the problem is here this tree pointer don't store the values for all alphabets, it just remembers the last executed alphabet after this for loop.
    tree -> left = NULL;
    tree  ->right = NULL;
    tree -> symbol = storesym[i];
    tree  -> freq = storefreq[i];
    tree -> flag = 0;
    tree -> next = i + 1;
    cout<<"check1 : "<<tree -> symbol<<endl;
} 

它重复分配一个 Node 并指向tree
换句话说,它不是在构建一棵树,您需要重写它才能真正做到。
无论您学习什么课程材料,最近都应该涵盖树木。

我能给出的最好建议是:重新开始,多阅读,在测试之前不要写太多代码。

【讨论】:

  • 我刚刚更改了代码并删除了第一个问题(您怀疑它会导致程序崩溃)。对于分配部分我需要帮助,请您帮帮我,以便我能够将您的答案标记为已解决,谢谢
  • @user234839 这是您打开书本和学习的地方。如果您在学校,请与您的同学和老师交谈。
  • 实际上我已经使用数组完成了,如果它是一个数组,我可以完成 tree[i].symbol=storesym[i];tree[i].left=tree[i]。对=NULL;在 for 循环内。但不知道如何用指针做等价。所以我只是需要帮助。(这是问题的主题)。所以,我实际上只需要使用指针来做同样的事情(我自己有数组)。
猜你喜欢
  • 2016-06-02
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多