New Member FAQ | Forums | Earn Revenue | Posting Guidelines | Help Topics


ResourcesPractice TestsAsk ExpertsQuestion PapersJobsStudy in UKUniversitiesCollegesCoursesSchoolsTraining


My Profile

Awards & Gifts


Active Members
TodayLast 7 Daysmore...





C program Binary Search Tree


Posted Date:     Total Responses: 0    Posted By: pankaj agarwala   Member Level: Silver   Points/Cash: 5   


C program to INSERT an element, Inorder traversing,Count no of leaf node,find level of a particular element,find the subnode,print element of same LEVEL,




#include
#include
#include

int nol=0,non=0;
struct tree
{
int no;
struct tree *l,*r;
};
struct tree *root=NULL;


struct tree * search(int val,int *found)
{
struct tree *p=root,*par=NULL;
while(p!=NULL)
{
if(val==p->no)
{
*found=1;
break;
}
else if(val>p->no)
{
par=p;
p=p->r;
}
else
{
par=p;
p=p->l;
}
}
return par;
}



void insert(int val)
{
int f=0;
struct tree *n,*parent;
n=(struct tree*)malloc(sizeof(struct tree));
n->no=val;
n->l=n->r=NULL;
if (root==NULL)
{
root=n;
return;
}
parent=search(val ,&f);
if(f==1)
{
printf("\n\n\n DUPlicate number");
free(n);
return;
}

else if(val>parent->no)
parent->r=n;
else
parent->l=n;
}


void countnoofleaf(struct tree *p)
{

if(p!=NULL)
{
if((p->l==NULL)&&(p->r==NULL))
nol++;
else
countnoofleaf(p->l);
countnoofleaf(p->r);

}
}


int level(struct tree *p,int val)
{
int f=0;
search(val,&f);
if(f==1)
{
while(val!=p->no)
{
if(val>p->no)
{
p=p->r;
nol++;
}
else
{
p=p->l;
nol++;
}
}
return nol;
}
else
{
printf("\n\nThe Number u entered is not Available");
return -1;
}
}



void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->l);
printf(" %d",p->no);
inorder(p->r);
}
}

void countnode(struct tree *p )

{
if(p!=NULL)
{
if(p->l!=NULL)
{
non++;
countnode(p->l);
}
if(p->r!=NULL)
{non++;
countnode(p->r);
}
}
}



void countsubnode(struct tree *p,int val)
{
int f=0;
search(val,&f);
if(f==1)
{
while(val!=p->no)
{
if(val>p->no)
{
p=p->r;
}
else
p=p->l;
}
countnode(p);
}
}


void plevelno(struct tree *p,int lvl)
{ int i=0;
if(p!=NULL)
{
i=level(root,p->no);
nol=0;

if(i==lvl)
printf("\n %d",p->no);
else
plevelno(p->l,lvl);
plevelno(p->r,lvl);
}
}











void main()
{
clrscr();
int val,ch,i;
do
{
printf("\n\n\n 1 To Insert");
printf("\n 2 To INORDER traversing of TREE");
printf("\n 3 To Count no of leaf");
printf("\n 4 To Find The LEVEL of a particular NUMber");
printf("\n 5 To find the subnode");
printf("\n 6 To print element of the same LEVEL");
printf("\n Enter ur choice");
scanf("%d",&ch);

switch(ch)
{
case 1:printf("\n\n ENter NUMBER");
scanf("%d",&val);
insert( val);
break;
case 2: inorder(root);
break;
case 3: countnoofleaf(root);
printf("\n\n Number of Leaf is %d",nol);
nol=0;
break;
case 4: printf("\n\n Enter the number");
scanf("%d",&val);
i=level(root,val);
if(i!=-1)
printf("\n\n The level of %d is %d",val,i);
nol=0;
break;
case 5: printf("\n\n Enter the NUMBER");
scanf("%d",&val);
countsubnode(root,val);
printf("\n\nNumber of subnode is %d",non);
non=0;
break;
case 6: printf("\n\n Enter the LEVEL no.");
scanf("%d",&val);
plevelno(root,val);
break;
case 0: break;
default:printf("\n\nInvalid Choice");
}
}while(ch!=0) ;
getch();
}



Project Feedbacks


No feedbacks found. Be the first to respond...

Post Feedback
You must Sign In to post a feedback.
Next Project: Assembly language program for Binary search
Previous Project: Assembly language program for Bubble sort

Return to Project Index

Post New Project


Related Projects

Subscribe to Email
  • Get Jobs by Email
  • Forum posts by Email
  • Articles by Email



  • website counter

    Online Membersbenson gomez
    Ashish Kumar
    Iti Tyagi
    Sakshi Sood
    dks
    Arvind
    siva
    sathishkumar
    Shiyas
    Kamlesh. M. Dhurve
    Saraswathi
    More...



    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    AdSense Revenue Sharing sites   Advertise   Talk to Tony John
    ISC Technologies, Kochi - India
    2006 - 2012 All Rights Reserved.