Recently, I coded linked list with C++11. Using those concepts, I tried to code tree{a very basic tree} implementation in C++11. But it is giving me a Segmentation fault. I checked online and found out that it happens when the program tries to write a read access memory or tries to access free memory, but I can't figure out how it's happening here. Please help..
#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;
}
tree()you access variableroot, but you didn't initialize it.