为什么我的代码显示错误分段错误(核心转储)

时间:2016-07-11 13:03:55

标签: c++ binary-tree

我试图在二叉树中实现preorder遍历。这是我的代码片段。

#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'的节点。

有人可以解释我到底出错的地方以及是否有更好的方法来实施它?

1 个答案:

答案 0 :(得分:0)

看起来你编译时关闭了警告。当我编译它时,我得到以下消息:

$ 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;
                      ^

此外,在Valgrind下运行

==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)

表示您首次访问无效值的确切位置。