#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink,*rlink;
}*root=NULL;
void create();
void node(struct node *q);
void degree(struct node *q);
void leaf(struct node *q);
void interior(struct node *q);
void child(struct node *q);
int no,l,in,ch,p;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**");
printf("\n 1.create a tree \n 2.count num of nodes");
printf("\n 3.degree of tree \n 4.leaf nodes \n 5.interior nodes");
printf("\n 6.childrens and parent \n 7.exit");
printf("\n enter your choice:-\t");
scanf("%d",&n);
switch(n)
{
case 1 : create();
break;
case 2 : node(root);
printf("\n\n total nodes are:- %d",no);
break;
case 3 : degree(root);
break;
case 4 : leaf(root);
printf("\n\n leaf nodes is:- %d",l);
break;
case 5 : interior(root);
printf("\n\n interior nodes are:- %d",in);
break;
case 6 : child(root);
printf("\n\n childrean is:- %d and parents is:- %d",ch,p);
break;
case 7 : exit(0);
}
}
}
void create()
{
struct node *temp,*q;
int x,n,i;
printf("\nEnter how many nodes you want:-");
scanf(" %d",&n);
for(i=0;i<n;i++)
{
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n insufficient memory");
}
else
{
printf("\n enter number:- ");
scanf(" %d",&x);
temp->info=x;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
{
root=temp;
}
else
{
q=root;
while(1)
{
if(temp->info==q->info)
{
printf("\n number is aleardy exist");
break;
}
if(temp->info>q->info)
{
if(q->rlink==NULL)
{
q->rlink=temp;
break;
}
q=q->rlink;
}
if(temp->info<q->info)
{
if(q->llink==NULL)
{
q->llink=temp;
break;
}
q=q->llink;
}
}
}
}
}
}
void node(struct node *q)
{
if(q!=NULL)
{
node(q->llink);
node(q->rlink);
no=no+1;
}
}
void degree(struct node *q)
{
if(q!=NULL)
{
if(q->llink!=NULL&&q->rlink!=NULL)
{
printf("\n\n degree of %d node is:= 2",q->info);
}
if(q->llink!=NULL&&q->rlink==NULL)
{
printf("\n\n degree of %d node is:= 1",q->info);
}
if(q->llink==NULL&&q->rlink!=NULL)
{
printf("\n\n degree of %d node is:= 1",q->info);
}
if(q->llink==NULL&&q->rlink==NULL)
{
printf("\n\n degree of %d node is:= 0",q->info);
}
degree(q->llink);
degree(q->rlink);
}
}
void leaf(struct node *q)
{
if(q!=NULL)
{
leaf(q->llink);
leaf(q->rlink);
if(q->llink==NULL||q->rlink==NULL)
{
l=l+1;
}
}
}
void interior(struct node *q)
{
if(q!=NULL)
{
interior(q->llink);
interior(q->rlink);
if(q->llink!=NULL||q->rlink!=NULL)
{
in=in+1;
}
}
}
void child(struct node *q)
{
if(q!=NULL)
{
child(q->llink);
child(q->rlink);
if(q->llink!=NULL||q->rlink!=NULL)
{
p=p+1;
}
if(q->llink==NULL||q->rlink==NULL)
{
ch=ch+1;
}
}
}
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink,*rlink;
}*root=NULL;
void create();
void node(struct node *q);
void degree(struct node *q);
void leaf(struct node *q);
void interior(struct node *q);
void child(struct node *q);
int no,l,in,ch,p;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**");
printf("\n 1.create a tree \n 2.count num of nodes");
printf("\n 3.degree of tree \n 4.leaf nodes \n 5.interior nodes");
printf("\n 6.childrens and parent \n 7.exit");
printf("\n enter your choice:-\t");
scanf("%d",&n);
switch(n)
{
case 1 : create();
break;
case 2 : node(root);
printf("\n\n total nodes are:- %d",no);
break;
case 3 : degree(root);
break;
case 4 : leaf(root);
printf("\n\n leaf nodes is:- %d",l);
break;
case 5 : interior(root);
printf("\n\n interior nodes are:- %d",in);
break;
case 6 : child(root);
printf("\n\n childrean is:- %d and parents is:- %d",ch,p);
break;
case 7 : exit(0);
}
}
}
void create()
{
struct node *temp,*q;
int x,n,i;
printf("\nEnter how many nodes you want:-");
scanf(" %d",&n);
for(i=0;i<n;i++)
{
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n insufficient memory");
}
else
{
printf("\n enter number:- ");
scanf(" %d",&x);
temp->info=x;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
{
root=temp;
}
else
{
q=root;
while(1)
{
if(temp->info==q->info)
{
printf("\n number is aleardy exist");
break;
}
if(temp->info>q->info)
{
if(q->rlink==NULL)
{
q->rlink=temp;
break;
}
q=q->rlink;
}
if(temp->info<q->info)
{
if(q->llink==NULL)
{
q->llink=temp;
break;
}
q=q->llink;
}
}
}
}
}
}
void node(struct node *q)
{
if(q!=NULL)
{
node(q->llink);
node(q->rlink);
no=no+1;
}
}
void degree(struct node *q)
{
if(q!=NULL)
{
if(q->llink!=NULL&&q->rlink!=NULL)
{
printf("\n\n degree of %d node is:= 2",q->info);
}
if(q->llink!=NULL&&q->rlink==NULL)
{
printf("\n\n degree of %d node is:= 1",q->info);
}
if(q->llink==NULL&&q->rlink!=NULL)
{
printf("\n\n degree of %d node is:= 1",q->info);
}
if(q->llink==NULL&&q->rlink==NULL)
{
printf("\n\n degree of %d node is:= 0",q->info);
}
degree(q->llink);
degree(q->rlink);
}
}
void leaf(struct node *q)
{
if(q!=NULL)
{
leaf(q->llink);
leaf(q->rlink);
if(q->llink==NULL||q->rlink==NULL)
{
l=l+1;
}
}
}
void interior(struct node *q)
{
if(q!=NULL)
{
interior(q->llink);
interior(q->rlink);
if(q->llink!=NULL||q->rlink!=NULL)
{
in=in+1;
}
}
}
void child(struct node *q)
{
if(q!=NULL)
{
child(q->llink);
child(q->rlink);
if(q->llink!=NULL||q->rlink!=NULL)
{
p=p+1;
}
if(q->llink==NULL||q->rlink==NULL)
{
ch=ch+1;
}
}
}