【问题标题】:Unexpected segfault when creating a binary tree创建二叉树时出现意外的段错误
【发布时间】:2019-09-23 06:30:27
【问题描述】:

我有一个 BinaryTree 类的以下表示:

#include "treecode.hh"
Treecode::Treecode(){}

Treecode::Treecode(string s, int f) {
    this->s = s;
    this->freq = f;
    this->left = NULL;
    this->right = NULL;
}

Treecode::Treecode(Taula_freq taula_f) {
    priority_queue<Treecode, vector<Treecode>, Compare_trees> q;
    for (auto a: taula_f.get_taula()) {
        Treecode myTree = Treecode(a.first, a.second);
        q.push(myTree);
    }
    Treecode res;
    while (q.size() > 1) {
        Treecode a = q.top();
        q.pop();
        Treecode b = q.top();
        q.pop();
        res = Treecode(a,b);
        q.push(res);
    }
    res = q.top();
    this->freq = res.freq;
    this->s = res.s;
    this->left = (res.left);
    this->right = (res.right);
}


Treecode::Treecode(Treecode& a, Treecode& b) {
    this->left = &b;
    this->right = &a;
    this->freq = a.freq + b.freq;
    this->s = a.s + b.s;
}

int Treecode::get_freq() {
    return this->freq;
}

string Treecode::get_s() {
    return this->s;
}

void Treecode::print_tree(const Treecode& a) {
    cout << a.s << " " << a.freq << endl;
    if (a.left != NULL)
        print_tree(*a.left);
    if (a.right != NULL)
        print_tree(*a.right);
}

这就是hh

#ifndef TREECODE_HH
#define TREECODE_HH
#include "taula_freq.hh"
#include <queue>
using namespace std;


class Treecode{
private:
    int freq;
    string s;
    Treecode* left;
    Treecode* right;

public:
    Treecode();
    Treecode(string s, int f);
    Treecode(Taula_freq taula);
    Treecode(Treecode& a, Treecode& b);
    int get_freq();
    string get_s();
    void print_tree(const Treecode &a);
};


class Compare_trees {
public:
         bool operator() (Treecode a, Treecode b) {
            if (a.get_freq() < b.get_freq()) return true;
            else if (b.get_freq() < a.get_freq()) return false;
            else if (a.get_s() < b.get_s()) return true;
            else return false;
         } 
 };



#endif

所以这里的问题是根节点是根据规范正确生成的,但是当我调用函数 print_tree 来检查根节点和左右子节点之间的链接时,我得到了一个意外的段错误。 用于创建提供两棵不同树的新树的构造函数似乎是问题所在,但我不知道为什么,因为我通过引用传递两个参数并使用 & 运算符正确访问内存位置(我认为)。另外,每次我使用 Treecode(string s, int f) 构造函数创建一个新树时,我都会确保左右节点为 NULL,所以我也不确定为什么 print_tree(...) 的基本情况是失败。

编辑:这里是 main 和 taula_freq 类

taula_freq.hh

#ifndef TAULA_FREQ_HH
#define TAULA_FREQ_HH

#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

class Taula_freq {

private:
    map<string, int> taula;

public:
    Taula_freq();
    Taula_freq(string c, int n);
    //Per fer-lo una mica mes net
    //Taula_freq(vector<pair<string,int> >);

    void add_freq(string c, int n);

    void print_taula();
    int get_freq(string s);
    map<string, int> get_taula();

};

#endif

taula_freq.cc

#include "taula_freq.hh"

Taula_freq::Taula_freq(){}

Taula_freq::Taula_freq(string c, int n) {
    this->taula[c] = n;
}

void Taula_freq::add_freq(string c, int n) {
    this->taula[c] = n;
}

void Taula_freq::print_taula() {
    for (auto a : this->taula) {
        cout << "(" << a.first << ", " << a.second << ")" << endl;
    }
}

int Taula_freq::get_freq(string s) {
    if (this->taula.find(s) == this->taula.end())
        return -1;
    else return this->taula[s];
}

map<string, int> Taula_freq::get_taula() {
    return this->taula;
}

ma​​in.cc

#include "taula_freq.hh"
#include "treecode.hh"
using namespace std;

int main() {
    Taula_freq t = Taula_freq("a", 30);
    t.add_freq("b", 12);
    t.add_freq("c", 18);
    t.add_freq("d", 15);
    t.add_freq("e", 25);
    Treecode tree = Treecode(t);
    tree.print_tree(tree);
}

编辑 2:

我已经尝试如下添加赋值运算符,但我也得到了段错误:

Treecode& Treecode::operator= (const Treecode& other){
    if (this != &other) { 
        this->s = other.s;
        this->freq = other.freq;
        this->left = other.left;
        this->right = other.right;
        }
        return *this;
    }

编辑 3

好的,我会将其标记为已解决。感谢您指出 cmets 中的深浅副本。我的 c++ 真的生锈了……

Treecode& Treecode::operator=(const Treecode& other){
    if (this != &other) { 
        this->s = other.s;
        this->freq = other.freq;
        this->left = new Treecode();
        this->right = new Treecode();
        if (other.right != NULL) {
            this->right->freq = other.right->freq;
            this->right->s = other.right->s;
            if (other.right->left != NULL)
                this->right->left = other.right->left;
            if (other.right->right != NULL)
                this->right->right = other.right->right;
        }
        if (other.left != NULL) {
            this->left->freq = other.left->freq;
            this->left->s = other.left->s;
            if (other.left->left != NULL)
                this->left->left = other.left->left;
            if (other.left->right != NULL)
                this->left->right = other.left->right;
        }

        }
        return *this;
    }

【问题讨论】:

  • 请发布minimal reproducible example,包括创建树的main函数。为什么print_tree 是成员函数?为什么要在组合构造函数中创建res,而不是直接分配sfreq
  • @πάνταῥεῖ 我用赋值运算符编辑了问题,但段错误仍然存​​在。谢谢你的推荐。
  • @Norhther 了解 deepshallow 复制。
  • 您添加了一个与默认分配相同的分配。增加一个是不够的;它也需要做正确的事。

标签: c++ pointers tree binary-tree


【解决方案1】:

感谢cmets,我想出了这个

Treecode& Treecode::operator=(const Treecode& other){
    if (this != &other) { 
        this->s = other.s;
        this->freq = other.freq;
        this->left = new Treecode();
        this->right = new Treecode();
        if (other.right != NULL) {
            this->right->freq = other.right->freq;
            this->right->s = other.right->s;
            if (other.right->left != NULL)
                this->right->left = other.right->left;
            if (other.right->right != NULL)
                this->right->right = other.right->right;
        }
        if (other.left != NULL) {
            this->left->freq = other.left->freq;
            this->left->s = other.left->s;
            if (other.left->left != NULL)
                this->left->left = other.left->left;
            if (other.left->right != NULL)
                this->left->right = other.left->right;
        }

        }
        return *this;
    }

【讨论】:

    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多