最近,我用C++ 11编写了链表.使用这些概念,我尝试在C++ 11中编写树{一个非常基本的树}实现.但它给了我一个分段错误.我在网上查了一下,发现当程序试图写一个读访问内存或试图访问空闲内存时会发生这种情况,但我无法弄清楚它是如何发生在这里的.请帮忙..
#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
node *left;
node *right;
int key;
};
class tree{
public:
node *root;
tree(){
root->left=NULL;
root->right=NULL;
}
node *createnode(int data){
node *temp=new node;
temp->key=data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
};
int main(){
tree t;
node *root;
root=t.createnode(1);
//root->left=t.createnode(2);
//root->right=t.createnode(3);
//root->left->left=t.createnode(9);
//root->left->right=t.createnode(7);
return 0;
}
Run Code Online (Sandbox Code Playgroud)