【问题标题】:Huffman Code - Segmentation Fault 11霍夫曼代码 - 分段错误 11
【发布时间】:2012-11-15 01:42:10
【问题描述】:

好的,我尝试自己制作 Hauffman 代码,但当我尝试使用递归方法为每个字母打印相应的代码时遇到问题。

(此时我已经创建了一个 BinaryTree 并且 root 不是 NULL) 每个节点都有一个值,如果它是叶子,它也有一个字母,所以我开始逐个节点地沿着树节点向下,但似乎在递归方法中节点只是忘记了他是谁或者我不知道,我有尝试了很多东西。 :S

递归方法是createCode(Node* actual, string actualCode);

代码如下:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <queue>
using namespace std;

#define MAX_VALUE 256
int frequencies[MAX_VALUE] = {0};

struct Node {
    int value;
    char letter;
    struct Node *Left, *Right;
}*root=NULL, *temp, *left_temp, *right_temp;

struct CompareNode : public std::binary_function<Node*, Node*, bool> {
    bool operator()(const Node* lhs, const Node* rhs) const {
        if (lhs->value == rhs->value)
            return lhs->letter > rhs->letter;
        else
            return lhs->value > rhs->value;
    }
};

void createCode(Node* actual,string actualCode) {
    if (actual->Left == NULL && actual->Right == NULL) {
        cout << "For: " << actual->letter << " is " << actualCode << endl;
    }
    else {
        if (actual->Left) {
            createCode(actual->Left, actualCode + "0");
        }
        if (actual->Right) {
            createCode(actual->Right, actualCode + "1");
        }
    }
}

void createTree() {
    priority_queue<Node*, vector<Node*>, CompareNode> que;

    for (int x = 0; x < MAX_VALUE; x++) {
        if (frequencies[x] > 0) {
            temp = new Node;
            temp->value = frequencies[x];
            temp->letter = char(x);
            que.push(temp);
        }
    }

    while (que.size() > 1) {
        temp = new Node();
        temp->Left = que.top();
        que.pop();
        temp->Right = que.top();
        que.pop();
        temp->value = temp->Left->value + temp->Right->value;
        temp->letter = NULL;
        que.push(temp);
    }

    root = que.top();
    que.pop();
}

void fillArray(int argc, char *argv[]) {
    string line;
    const char* ptr;

    ifstream myFile(argv[argc - 1]);
    if (myFile.is_open()) {
        while (myFile.good()) {
            getline(myFile,line);

            if (line.length() != 0) {
                ptr = &line.at(0);

                while (*ptr != '\0')
                    ++frequencies[*ptr++];
            }
        }
    }
    else
        cout << "The file could not be open.";
}

int main(int argc, char *argv[]) {
    fillArray(argc, argv);

    createTree();

    createCode(root, "");
    return 0;
}

这是示例树(我试图发布图像,但因为我是新手,所以我不能):

Binary Tree Image

这是输出:

For: a is 0
For:   is 10
Segmentation fault: 11

请帮忙:(

【问题讨论】:

  • 很可能,actual-&gt;Leftactual-&gt;Right 指向已损坏或不存在的 Node。您可以使用valgrind 之类的工具来跟踪问题。 (也许您分配了一个Node 并且没有将LeftRight 设置为NULL?)
  • 如果一个节点只有一个子节点(这不应该在霍夫曼树中发生),createChild 在尝试访问另一个子节点时也会崩溃。
  • @JanDvorak:很好。赠品将是,在这种情况下,崩溃将发生在 createCodeactual 将是 NULL
  • @DavidSchwartz 太糟糕了,我们没有堆栈跟踪。
  • 我在这里错过了什么?看起来 root 设置为 0,然后在初始化之前使用(无论如何,该赋值都会导致错误)

标签: c++ recursion segmentation-fault binary-tree huffman-code


【解决方案1】:

在创建推入队列的Nodes 时,您需要初始化LeftRight 指向NULL 的指针:

if (frequencies[x] > 0) {
    temp = new Node;
    temp->Left = NULL;
    temp->Right = NULL;
    temp->value = frequencies[x];
    temp->letter = char(x);
    que.push(temp);
}

如果没有显式初始化,这些指针具有不确定的值,而且通常不是 NULL,因此在 createCode 中,您正在取消引用无效指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2017-04-18
    相关资源
    最近更新 更多