I am having trouble finding the real trouble in my program to insert into a Binary Search tree.
//BST
#include<iostream>
using namespace std;
class bst
{
public:
int data;
bst *lnext,*rnext,*root,*prev;
public:
bst()
{
lnext=rnext=root=NULL;
}
bst* addBST(bst *p)
{
if(root==NULL)//if tree is empty
{
root=p;
cout<<"\nRoot Inserted!";
return root;
}
else
{
p= insert(root,p);
return p;
}
}
bst* insert(bst *xroot, bst *newnode)
{
if(xroot==NULL)
{
xroot = newnode;
cout<<"\nnode inserted!";
return newnode;
}
if(newnode->data < xroot->data)
{
cout<<xroot->data;
cout<<"\nLEss!";
return insert(xroot->lnext,newnode);
}
else
{
cout<<"\nMore!";
return insert(xroot->rnext,newnode);
}
}
bst* minnode(bst *wroot)
{
if(wroot->lnext==NULL)
return wroot;
else
return minnode(wroot->lnext);
}
};
main()
{
bst obj,*p,*p2,*p3,*temp;
p= new bst();
p2= new bst();
p3 = new bst();
int data;
p->data=3;
p= obj.addBST(p);
p2->data=1;
p2= obj.addBST(p2);
p3->data=5;
p3= obj.addBST(p3);
temp=obj.minnode(obj.root);
cout<<"Minimum element is ::"<<temp->data;
system("pause");
return 0;
}
The problem is with lnext and rnext values. Please help me rectify the problem