Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “分段故障即将发生”;在二叉搜索树中插入#_C++_Data Structures_Binary Search Tree - Fatal编程技术网

C++ “分段故障即将发生”;在二叉搜索树中插入#

C++ “分段故障即将发生”;在二叉搜索树中插入#,c++,data-structures,binary-search-tree,C++,Data Structures,Binary Search Tree,我正在编写一个函数,在BST中插入一个节点,但是得到了分段错误。 /* Node is defined as typedef struct node { int data; node * left; node * right; }node; */ node * findPos(node * tree, int value){ if(tree -> data > value){

我正在编写一个函数,在BST中插入一个节点,但是得到了分段错误。

    /*
    Node is defined as 

   typedef struct node
   {
     int data;
     node * left;
     node * right;
  }node;

   */

    node * findPos(node * tree, int value){
       if(tree -> data > value){
             return findPos(tree -> left, value);
        }
       else if (tree -> data < value){
            return findPos(tree -> right, value);
       }

        return tree;
   }

   node * addNode(int value){

       struct node * temp =(struct node *)malloc(sizeof(struct node));
       temp->data = value;
       temp->left = NULL;
       temp -> right = NULL;
       return temp;
    }
    node * insert(node * root, int value)
    {
       node * ptr = root;

       if(ptr == NULL)
          return addNode(value);

        else if(ptr -> data > value){
            ptr->left = findPos(ptr -> left, value);
}

      else if(ptr -> data < value){
          ptr->right = findPos(ptr -> right, value);
       }  


       return root;
    }
/*
节点定义为
类型定义结构节点
{
int数据;
节点*左;
节点*右;
}节点;
*/
node*findPos(node*tree,int值){
如果(树->数据>值){
返回findPos(树->左,值);
}
else if(树->数据<值){
返回findPos(树->右,值);
}
回归树;
}
节点*addNode(int值){
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=值;
temp->left=NULL;
temp->right=NULL;
返回温度;
}
节点*插入(节点*根,int值)
{
node*ptr=root;
如果(ptr==NULL)
返回addNode(值);
否则如果(ptr->数据>值){
ptr->left=findPos(ptr->left,value);
}
否则如果(ptr->数据<值){
ptr->right=findPos(ptr->right,value);
}  
返回根;
}
我无法理解我试图访问的非法内存是哪个导致了此错误。 请帮我做这个。
提前感谢:)

这里有两个问题:

  • findPos应该处理树为空的情况
  • insert不应递归调用findPos。相反,它应该递归地调用insert。类似这样的内容(尚未测试):

    节点*插入(节点*根,int值)
    {
    node*ptr=root;
    如果(ptr==NULL)
    返回addNode(值);
    否则如果(ptr->数据>值){
    返回插入(ptr->左,值);
    }否则如果(ptr->数据<值){
    返回插入(ptr->右,值);
    }否则
    返回根;
    }
    

  • 谢谢大家的帮助!!! 让程序运行起来

       /*
        Node is defined as 
    
      typedef struct node
      {
         int data;
         node * left;
         node * right;
      }node;
    
    */

    node*addNode(int值){
    结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
    温度->数据=值;
    temp->left=NULL;
    temp->right=NULL;
    返回温度;
    }
    节点*插入(节点*根,int值)
    {
    node*ptr=root;
    如果(ptr==NULL)
    返回addNode(值);
    否则如果(ptr->数据>值){
    ptr->left=插入(ptr->left,值);
    }
    否则如果(ptr->数据<值){
    ptr->right=插入(ptr->right,值);
    }  
    返回根;
    }
    
    这是一个学习如何使用调试器的好机会,调试器将让您运行程序,直到出现分段错误,然后检查变量值等,以诊断哪里出了问题。感谢Jim的跟进,因为我没有在findPos()中处理该案例,所以我收到了错误消息该树可以为空。但逻辑仍然不正确,正在尝试查找,但尚未得到任何结果。@Sandepsing-您可以正确地看到,
    findPos
    不处理
    value
    大于或小于所有
    数据
    值的情况。它将继续跟随指针,直到它碰到墙。空部分我得到了它,我在我的评论中也提到了。但是逻辑有一些问题,你的代码我测试过,它不工作。谢谢你的跟进。
       /*
        Node is defined as 
    
      typedef struct node
      {
         int data;
         node * left;
         node * right;
      }node;
    
       node * addNode(int value){
    
        struct node * temp =(struct node *)malloc(sizeof(struct node));
        temp->data = value;
        temp->left = NULL;
        temp -> right = NULL;
        return temp;
      }
      node * insert(node * root, int value)
      {
          node * ptr = root;
    
         if(ptr == NULL)
           return addNode(value);
    
         else if(ptr -> data > value){
           ptr->left =  insert(ptr -> left, value);
         }
    
         else if(ptr -> data < value){
            ptr->right =  insert(ptr -> right, value);
         }  
    
    
        return root;
       }