Find height, print leaf nodes ,mirror and levelwise display of b-tree

, by Prashant Gunjal


/*2. Create binary tree. Find height of the tree and print leaf nodes. Find mirror image,
print original and mirror image using level-wise printing*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

class node
{
public:
int data;
node * lc,*rc;
node()
{
rc=lc=NULL;
}
};

class queue
{
public:
node * item[20];
int f,r;
queue()
{
f=r=-1;
}
void in(node * pt)
{
if(!full())
{
if(f==-1)
f=r=0;
else
r++;
item[r]=pt;
}
}
node * del()
{
node * pt;
if(!empty())
{
pt=item[f];
if(f==r)
f=r=-1;
else
f++;
}
return pt;
}
int full()
{
if(r==19)
return 1;
else
return 0;
}
int empty()
{
if(f==-1)
return 1;
else
return 0;
}
};
class tree
{
public:
node * root;
node * getnode();
node * create();
int height(node * );
void leaf(node * );
void level();
void mirror_rec(node *);
void mirror();

};
node * tree::getnode()
{
node *temp =new node;
cout<<"\nEnter data : ";
cin>>temp->data;
return temp;
}
node * tree :: create()
{
char ch;
node *pt=getnode();
cout<<"\nIs left present of "<<pt->data<<"(y/n)";
flushall();
cin>>ch;
if(ch=='y')
pt->lc=create();
cout<<"\nIs right present of "<<pt->data<<"(y/n) ";
flushall();
cin>>ch;
if(ch=='y')
pt->rc=create();
return pt;
}

void tree::leaf(node * pt)
{
if(pt!=NULL)
{
if(pt->lc==NULL&&pt->rc==NULL)
cout<<"\t"<<pt->data;
leaf(pt->lc);
leaf(pt->rc);
}
}

void tree::level()
{
queue q;
node *pt,* d=new node;
int l=0,i=2,k;
q.in(d);
q.in(root);
while(!q.empty())
{
pt=q.del();
if(pt==d)
{       if(!q.empty())
{
cout<<"\nLevel : "<<l<<":";
for(k=i;k>0;k--)
cout<<"\t";
if(i!=3)
cout<<"    ";
l++;
i--;
q.in(d);
}
}
else
{       if(pt!=NULL)
cout<<"\t\t"<<pt->data;
else
cout<<"\t\tN";
if(pt!=NULL)
{
if(pt->lc!=NULL||pt->rc!=NULL)
q.in(pt->lc);
if(pt->rc!=NULL||pt->rc!=NULL)
q.in(pt->rc);
}
}
}

}

int tree::height(node * pt)
{
int hl=0,hr=0;
if(pt==NULL)
return 0;
if(pt->lc!=NULL)
hl=height(pt->lc);
if(pt->rc!=NULL)
hr=height(pt->rc);
if(hl>hr)
return (hl+1);
else
return (hr+1);
}

void tree::mirror_rec(node * pt)
{
if(pt!=NULL)
{
node * p=pt->lc;
pt->lc=pt->rc;
pt->rc=p;
mirror_rec(pt->lc);
mirror_rec(pt->rc);
}
}

void tree::mirror()
{
queue q;
node *pt;
q.in(root);
while(!q.empty())
{
pt=q.del();
if(pt!=NULL)
{
node * p=pt->lc;
pt->lc=pt->rc;
pt->rc=p;

if(pt->lc!=NULL)
q.in(pt->lc);
if(pt->rc!=NULL)
q.in(pt->rc);
}
}

}

void main()
{
clrscr();
tree obj;
int ch;
do
{
cout<<"\n1.create\n2.Leaf node\n3.Level wise display\n4.Height";
cout<<"\n5.mirror\n 6>mirror no rec " ;
cout<<"\n7.Exit";
cout<<"\nEnter choice : ";
cin>>ch;
switch(ch)
{
case 1:
obj.root=obj.create();
break;
case 2:
cout<<"\nLeaf nodes are : ";
obj.leaf(obj.root);
break;
case 3:
obj.level();
break;
case 4:
int x=obj.height(obj.root);
cout<<"\n\nHeight = "<<x<<"\n\n";
break;
case 5:
obj.mirror_rec(obj.root);
break;
case 6:
obj.mirror();
break;
}
}while(ch!=7);
}

/* OUTPUT

1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice : 1

Enter data : 5

Is left present of 5(y/n)y

Enter data : 10

Is left present of 10(y/n)y

Enter data : 12

Is left present of 12(y/n)n

Is right present of 12(y/n) n

Is right present of 10(y/n) y

Enter data : 14

Is left present of 14(y/n)n

Is right present of 14(y/n) n

Is right present of 5(y/n) y

Enter data : 11

Is left present of 11(y/n)y

Enter data : 15

Is left present of 15(y/n)n

Is right present of 15(y/n) n

Is right present of 11(y/n) y

Enter data : 20

Is left present of 20(y/n)n

Is right present of 20(y/n)n


1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice : 2

Leaf nodes are :        12      14      15      20
1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice : 3


Level : 0:                              5
Level : 1:                      10              11
Level : 2:              12              14              15              20
1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice : 4


Height = 3


1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice :5

1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice : 3

Level : 0:                              5
Level : 1:                      11              10
Level : 2:              20              15              14              12
1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec6
7.Exit
Enter choice : 6

1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice : 3

Level : 0:                              5
Level : 1:                      10              11
Level : 2:              12              14              15              20
1.create
2.Leaf node
3.Level wise display
4.Height
5.mirror
 6>mirror no rec
7.Exit
Enter choice :
7.Exit
Enter choice :7
*/

0 comments: