插入二叉搜索树时出现分段错误
Segmentation fault while inserting in Binary search tree
我正在尝试用 C++ 构建一个基本的二叉搜索树,运行 遇到了一些问题,特别是当我尝试通过函数插入一个节点并读取时,它给了我分段错误。但是当我手动插入相同的节点结构时,它工作得很好。
BST插入的代码如下,很可能是罪魁祸首:
void BST::insert(Node* temproot,int val){
// std::cout << root->value <<std::endl;
if(!temproot){
Node* newNode = new Node;
newNode->value = val;
temproot = newNode;
std::cout << "Added Node with Value: " << val << std::endl;
return;
}
if(val<(temproot->value)){
std::cout << "LEFT" << std::endl;
insert(temproot->left, val);
}else{
std::cout << "RIGHT" << std::endl;
insert(temproot->right, val);
}
}
节点结构如下所示:
struct Node{
int value;
Node* left = nullptr, *right = nullptr;
};
BST class 如下所示:
class BST{
public:
Node* root= new Node;
BST(int val){
root->value = val;
}
void insert(Node*,int val);
void insertStart(int vasl){
Node* temproot = root;
insert(temproot, vasl);
}
void print(Node*);
void _print(){
print(root);
}
};
当我尝试按如下方式打印时,出现分段错误:
void BST::print(Node* temp){
std::cout << temp->value << std::endl;
temp = temp->left;
std::cout << (temp->value) << std::endl;
}
我对 C++ 有点陌生,几天来一直在努力定位它。有人可以帮我弄清楚我在这里做错了什么吗?
基本上,SEGFAULT 来自 print
函数,应该如下所示:
void BST::print(Node* temp){
if (nullptr == temp) {
return;
}
print(temp->left);
std::cout << temp->value << std::endl;
print(temp->right);
}
您的 insert
函数应如下所示:
void BST::insert(Node *&temproot,int val){
if(nullptr == temproot){
Node* newNode = new Node;
newNode->value = val;
temproot = newNode;
return;
}
if(val < (temproot->value)){
insert(temproot->left, val);
}else{
insert(temproot->right, val);
}
}
看看live
该函数处理传递给它的指向节点的指针的副本。因此更改副本不会影响原始指针。
您必须通过引用将指向节点的指针传递给函数。
函数声明可以如下所示
void insert(Node* &temproot,int val);
并这样称呼它
void insertStart(int vasl){
insert( root, vasl );
}
没有中间指针。
并且您应该将此函数声明为 class 的静态静态函数。
并通过nullptr初始化数据成员root
例如
class BST{
public:
Node* root = nullptr;
BST(int val){
insert( root, val );
}
void insertStart(int vasl){
insert( root, vasl);
}
void print(Node*);
void _print(){
print(root);
}
private:
static void insert(Node* &,int val);
};
我正在尝试用 C++ 构建一个基本的二叉搜索树,运行 遇到了一些问题,特别是当我尝试通过函数插入一个节点并读取时,它给了我分段错误。但是当我手动插入相同的节点结构时,它工作得很好。 BST插入的代码如下,很可能是罪魁祸首:
void BST::insert(Node* temproot,int val){
// std::cout << root->value <<std::endl;
if(!temproot){
Node* newNode = new Node;
newNode->value = val;
temproot = newNode;
std::cout << "Added Node with Value: " << val << std::endl;
return;
}
if(val<(temproot->value)){
std::cout << "LEFT" << std::endl;
insert(temproot->left, val);
}else{
std::cout << "RIGHT" << std::endl;
insert(temproot->right, val);
}
}
节点结构如下所示:
struct Node{
int value;
Node* left = nullptr, *right = nullptr;
};
BST class 如下所示:
class BST{
public:
Node* root= new Node;
BST(int val){
root->value = val;
}
void insert(Node*,int val);
void insertStart(int vasl){
Node* temproot = root;
insert(temproot, vasl);
}
void print(Node*);
void _print(){
print(root);
}
};
当我尝试按如下方式打印时,出现分段错误:
void BST::print(Node* temp){
std::cout << temp->value << std::endl;
temp = temp->left;
std::cout << (temp->value) << std::endl;
}
我对 C++ 有点陌生,几天来一直在努力定位它。有人可以帮我弄清楚我在这里做错了什么吗?
基本上,SEGFAULT 来自 print
函数,应该如下所示:
void BST::print(Node* temp){
if (nullptr == temp) {
return;
}
print(temp->left);
std::cout << temp->value << std::endl;
print(temp->right);
}
您的 insert
函数应如下所示:
void BST::insert(Node *&temproot,int val){
if(nullptr == temproot){
Node* newNode = new Node;
newNode->value = val;
temproot = newNode;
return;
}
if(val < (temproot->value)){
insert(temproot->left, val);
}else{
insert(temproot->right, val);
}
}
看看live
该函数处理传递给它的指向节点的指针的副本。因此更改副本不会影响原始指针。
您必须通过引用将指向节点的指针传递给函数。 函数声明可以如下所示
void insert(Node* &temproot,int val);
并这样称呼它
void insertStart(int vasl){
insert( root, vasl );
}
没有中间指针。
并且您应该将此函数声明为 class 的静态静态函数。
并通过nullptr初始化数据成员root
例如
class BST{
public:
Node* root = nullptr;
BST(int val){
insert( root, val );
}
void insertStart(int vasl){
insert( root, vasl);
}
void print(Node*);
void _print(){
print(root);
}
private:
static void insert(Node* &,int val);
};