10)Write a C program to read n interger and store them in binary tree structure and count the following and display it.


#include<stdio.h>
#include<math.h>

int a=0,b=0;
static int a,leaf,intc,rootc;
struct Btree
{
struct Btree *lchild;
int data;
struct Btree *rchild;
};
typedef struct Btree NODE;
NODE *root;
void count(NODE *h)
{ a++;
if(h==root)
rootc++;
else if(h->lchild==NULL && h->rchild==NULL)
leaf++;
else if(h->lchild!=NULL || h->rchild!=NULL)
intc++;
return 0;
}

NODE *getnode()
{
NODE *temp;
temp=(NODE*)malloc(sizeof(NODE));
printf(“\n\n Enter the data : “);
scanf(“%d”,&temp->data);
temp->lchild=NULL;
temp->rchild=NULL;
return(temp);
}

NODE *create()
{
NODE *temp,*ptr;
char ch;
root=NULL;
do
{
temp=getnode();
if(root==NULL)
root=temp;
else
{
ptr=root;
while(ptr!=NULL)
{
if(ptr->data>temp->data)
{
if(ptr->lchild==NULL)
{
ptr->lchild=temp;
break;
}
else
ptr=ptr->lchild;
}
else
{
if(ptr->rchild==NULL)
{
ptr->rchild=temp;
break;
}
else
ptr=ptr->rchild;
}
}//while
} //else
printf(“\n Add More (Y/N)? : “);
ch=getche();
}while(ch==’Y’ || ch==’y’);
return(root);
}

void inorder(NODE *h)
{
// count(h);
if(h!=NULL)
{
inorder(h->lchild);
count(h);
printf(“\t%d”,h->data);
inorder(h->rchild);
}
}
main()
{
NODE *header;
clrscr();
header=create();
printf(“\n”);
inorder(header);
printf(“\n The total no. of nodes are : %d”,a);
printf(“\n\n The total no. of leaf nodes are : %d”,leaf);
printf(“\n no of interior niode %d”,intc);
printf(“\nroot=%d”,rootc);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s