Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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++_Binary Tree_Binary Search Tree - Fatal编程技术网

C++ 二叉搜索树新节点分割错误

C++ 二叉搜索树新节点分割错误,c++,binary-tree,binary-search-tree,C++,Binary Tree,Binary Search Tree,我正在尝试从给定数组创建一个二叉搜索树。以下代码显示分段错误。我试着调试它,发现*newnode函数有问题 #include <iostream> using namespace std; struct node { int data; struct node *left; struct node *right; struct node *parent; }; struct node *newnode(int data) { struct

我正在尝试从给定数组创建一个二叉搜索树。以下代码显示分段错误。我试着调试它,发现*newnode函数有问题

#include <iostream>
using namespace std;

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

struct node *newnode(int data)
{
    struct node *temp;
    temp->data = data;
    temp->right =NULL;
    temp->left = NULL;
    temp->parent =NULL;
    return(temp);
}
#包括
使用名称空间std;
结构节点
{
int数据;
结构节点*左;
结构节点*右;
结构节点*父节点;
};
结构节点*新节点(整型数据)
{
结构节点*temp;
温度->数据=数据;
temp->right=NULL;
temp->left=NULL;
temp->parent=NULL;
返回(临时);
}
上面的函数似乎返回了正确的指针,但下面函数中的似乎不接受它

void Insert(struct node *T,int data)
{
    if(T==NULL)
    {
      T = newnode(data); //The program shows segfault here.
     return;
    }
    if(data>T->data) { Insert(T->right,data); T->right->parent = T; }
    else { Insert(T->left,data); T->left->parent = T; }

    return;
}

void Inorder(struct node *T)
{
    if(T==NULL) return;
    else{

        Inorder(T->left);
        cout<<T->data<<" ";
        Inorder(T->right);
    }
}

int main() {

    int n; cin>>n;
    struct node *tree=NULL;
    for(int i=0;i<n;i++)
    {
        int x; cin>>x;
        Insert(tree,x);
    }

    Inorder(tree);

    return 0;
}
void插入(结构节点*T,int数据)
{
如果(T==NULL)
{
T=newnode(data);//程序在这里显示segfault。
返回;
}
if(data>T->data){Insert(T->right,data);T->right->parent=T;}
else{Insert(T->left,data);T->left->parent=T;}
返回;
}
void索引项(结构节点*T)
{
如果(T==NULL)返回;
否则{
顺序(T->左);
coutn;
结构节点*tree=NULL;
对于(int i=0;i>x;
插入(树,x);
}
有序(树);
返回0;
}

您尚未为临时指针分配内存

struct node *newnode(int data)
{
    struct node *temp = new node;   
    temp->data = data;
    temp->right = NULL;
    temp->left = NULL;
    temp->parent = NULL;
    return temp;
}

您没有在“NeNoNoT”函数中分配内存,使用C++,因此可以为Syt创建一个构造函数。因为它是C++问题,所以我认为使用新的代替MALOC是个好主意。