//PROGRAM FOR COUNTING NUMBER OF NODES IN BINARY SEARCH TREE
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
}*root,*t;
void creation();
void search(int n);
void insertion();
void deletion();
void display(struct node*);
void count(struct node*);
static int c=0;
void main()
{
int ch;
clrscr();
do
{
printf("\n\tPROGRAM FOR BINARY SEARCH TREE\n");
printf("\nENTER YOUR CHOICE\n");
printf("\n1.CREATION\n2.INSERTION\n3.DELETION\n4.DISPLAY\n5.COUNT THE NUMBER OF NODES\n6.EXIT");
scanf("%d",&ch);
switch(ch)
{
case 1:creation();
display(t);
break;
case 2:insertion();
t=root;
display(t);
break;
case 3:deletion();
t=root;
display(t);
break;
case 4:t=root;
display(t);
break;
case 5:count(t);
printf("\nTHE TOTAL NUMBER OF NODES IS %d\n",c);
break;
case 6:printf("\nTHANKYOU");
break;
default:printf("\nINVALID ENTRY\n");
}
}while(ch!=5);
getch();
}
void creation()
{
root=(struct node*)malloc(sizeof(struct node));
t=root;
printf("\nENTER THE DATA TO BE INSERTED AT ROOT NODE\n");
scanf("%d",&root->data);
root->left=NULL;
root->right=NULL;
printf("\nROOT NODE CREATED\nDISPLAYING ROOT NODE\n");
}
void display(struct node *t)
{
while(t!=NULL)
{
printf("|%d->%h->%h|\t",t->data,t->left,t->right);
display(t->left);
display(t->right);
}
}
void insertion()
{
int key;
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
if(root==NULL)
printf("\nTREE EMPTY\nPLEASE CREATE TREE FIRST!");
else
{
printf("\nENTER THE DATA AFTER WHICH INSERTION IS TO BE MADE\n");
scanf("%d",&key);
printf("\nENTER THE DATA TO BE INSERTED\n");
scanf("%d",&new->data);
}
}
void count(struct node *t)
{
while(t!=NULL)
{
c++;
display(t->left);
display(t->right);
}
}
No comments:
Post a Comment