创建二进制树时意外的segfault
Unexpected segfault when creating a binary tree
我有以下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以检查根和左右孩子之间的链接时,我会得到意外的segfault。创建新树的构造函数似乎是问题所在,但我不知道为什么,因为我是通过参考来传递这两个参数并使用&amp;操作员。另外,每次我使用Treecode(String s,int f(构造函数创建一棵新树时,我确保左和右节点为null,所以我也不确定为什么print_tree(...(的基本案例为何失败。
编辑:这是主要和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;
}
main.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:
我已经尝试添加一个分配运算符,但我也得到了segfault:
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
好吧,我将标记为解决。感谢您在评论中指出的深层副本。我的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;
}
多亏了评论,我提出了这个
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;
}
相关文章:
- C++中带有List类的迭代器Segfault
- 在C++中对T*类型执行std::move的意外行为
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 使用Vulkan hpp vk::enumerateInstanceVersion()会导致segfault
- 处理除以零会导致<csignal>意外行为
- vscode下的Arduino代码出现意外编译错误
- 使用++运算符会导致意外的结果
- 套接字读取后,我在缓冲区中看到意外输入
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 使用vscode调试时,GDB意外退出
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 尝试将字符串/字符转换为整数会产生意外结果
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- C++标头错误 C2238 意外标记";"
- C++中意外的多头值
- vector.size() 在比较中意外工作
- 使用 malloc() 时出现意外大小
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 多线程程序中出现意外的内存泄漏
- 创建二进制树时意外的segfault