为什么我的代码显示错误分段错误(核心转储)
Why is my code showing error Segmentation fault(core dumped)
我试图在二叉树中实现预序遍历。这是我的代码片段。
#include <iostream>
using namespace std;
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root= NULL;
struct node *inorder_search(struct node *node, int val){
inorder_search(node->left,val);
if(node->data==val) return node;
inorder_search(node->right,val);
}
void insert(int data1, int data2, char subtree){
struct node *current;
struct node *temp1;
struct node *temp2;
temp1->data= data1;
temp1->left= NULL;
temp1->right= NULL;
temp2->data= data2;
temp2->left= NULL;
temp2->right= NULL;
if(root==NULL){
root= temp1;
if(subtree=='R'){
root->right= temp2;
return;
}
else{
root->left= temp2;
return;
}
}
else{
current= inorder_search(root,data1);
if(subtree=='R'){
current->right= temp2;
return;
}
else{
current->left= temp2;
return;
}
}
}
void preorder_traversal(struct node *node){
if(node==NULL) return;
cout<<node->data<<" ";
preorder_traversal(root->left);
preorder_traversal(root->right);
}
int main(void){
int edges;//edges= no. of edges
cout<<"Enter the number of edges: ";
cin>>edges;
int i;
int relations= edges*3;
int *arr= new int[relations+1]; //since input is in the form 1 2 R 1 2 L where R= right subtree, L= left subtree
for(i=0; i<relations;i++) cin>>arr[i];
for(i=0;i<relations;i+=3) insert(arr[i], arr[i+1], arr[i+2]);
preorder_traversal(root);
}
代码适用于以下输入类型
1 2 L 1 3 R
函数insert(),我正在尝试为以数组形式获取的输入创建一个树。函数 inorder_search() 用于搜索要向其左侧或右侧子树添加数据的特定节点,该节点将返回到 insert() 函数。例如,在 1 2 L 1 3 R 中,"1"是节点,其中"2"和"3"分别是左子树和右子树。所以我在 inorder_search() 函数中搜索"1",并返回在 insert() 函数中相应地插入"2"或"3"的节点。
有人可以解释一下我到底哪里出了问题,是否有更好的方法来实施它?
看起来您在关闭警告的情况下编译。 当我编译它时,我收到以下消息:
$ g++ -std=c++14 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Weffc++ 38307710.cpp -o 38307710
38307710.cpp: In function ‘node* inorder_search(node*, int)’:
38307710.cpp:16:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
38307710.cpp: In function ‘void insert(int, int, char)’:
38307710.cpp:23:22: warning: ‘temp1’ is used uninitialized in this function [-Wuninitialized]
temp1->data= data1;
^
38307710.cpp:27:22: warning: ‘temp2’ is used uninitialized in this function [-Wuninitialized]
temp2->data= data2;
^
此外,在瓦尔格林德的带领下跑步会给
==5113== Use of uninitialised value of size 8
==5113== at 0x40093C: insert(int, int, char) (38307710.cpp:23)
==5113== by 0x400B77: main (38307710.cpp:72)
==5113==
==5113== Invalid write of size 4
==5113== at 0x40093C: insert(int, int, char) (38307710.cpp:23)
==5113== by 0x400B77: main (38307710.cpp:72)
==5113== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5113==
==5113==
==5113== Process terminating with default action of signal 11 (SIGSEGV)
==5113== Access not within mapped region at address 0x0
==5113== at 0x40093C: insert(int, int, char) (38307710.cpp:23)
==5113== by 0x400B77: main (38307710.cpp:72)
这精确地指示您首次访问无效值的位置。
相关文章:
- C++映射分割错误(核心转储)
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 分段错误(核心转储)但无法弄清楚
- 分段错误(核心转储) - 使用 SavedModel 的 Tensorflow C++ API 进行推断
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 合并排序:分段错误核心转储
- C++:分段错误(核心转储)
- 从 pybind11 构建导入模块时出现"分段错误(核心转储)"
- qt C++命令行代码中的分段错误(核心转储)
- C++ atof - 分段错误(核心转储)
- 为什么我的代码包含错误分段错误(核心转储)?
- 运行编译的 a.out 后出现分段错误(核心转储)错误
- 运行我的代码时出现分段错误(核心转储)问题
- C++代码的分段错误(核心转储)
- 为什么我在执行main之前就出现分段错误(核心转储)
- OpenCV 分段错误(核心转储)在使用 cv::Mat::at 时
- 分段错误(核心转储) 重定向输入
- C++字符串中的分段错误(核心转储)
- 树实现给出分段错误(核心转储)错误 c++ 11