Binary Search Tree -Insertion

The problem is in this program , lnext and rnext(subtrees) are not getting correct values.They continue to be NULL even after insertion!

//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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//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);
      }
};

void 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;
}


You were missing a few brackets and this will make it more legible for others to read.
Last edited on
Thanx mcrist,but the program is still not giving my desired output,ie Minmum element should be 1 and not 3!
Topic archived. No new replies allowed.