i have included just insert function in redblacktree class.
there are no compilation errors but it shows segmentation fault on running.
when i am calling insert function it's not even entering into it as it's not printing the cout statement at first line of insert body.
i am using g++ compiler in linux ubuntu.
i am wondering what is wrong.
#include<iostream>
using namespace std;
template <class T>
class redblacktree {
private:
T x;
redblacktree<T> *left, *right, *parent;
int leftsize; // number of nodes in left subtree
int r; // the number of black nodes on any path
// from the current node to a leaf in its subtree
bool color;
redblacktree<T> *root;
public:
redblacktree():root(NULL){}
void insert(T y)
{
cout<<"root->x=";
redblacktree<T> *p,*q;
if(root==NULL)
{
p=new redblacktree<T>;
p->x=y;
p->left=p->right=p->parent=NULL;
p->leftsize=0;
p->r=1;
p->color=false;
cout<<"root->x="<<root->x;
}
else
{
while(p!=NULL)
{
p=root;
if(y>p->x)
p=p->right;
else if(y<p->x)
p=p->left;
}
q=new redblacktree<T>;
q->x=y;
q->left=p->right=NULL;
q->parent=p;
q->leftsize=0;
q->r=0;
q->color=true;
while((q->parent->left==q)&&(q->parent!=NULL))
{
q=q->parent;
q->leftsize+=1;
}
}
}
};
int main()
{
redblacktree<int> rb;
rb.insert(1);
}