Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views5 pages

BinarySearchTree Code

The document contains a C program that implements a Binary Search Tree (BST) with various functionalities such as creating the tree, traversing it in different orders (inorder, preorder, postorder), finding the minimum and maximum values, counting leaf and non-leaf nodes, searching for elements, and deleting nodes. It provides a menu-driven interface for user interaction to perform these operations. The program includes functions for node creation, insertion, and traversal, as well as for counting and deleting nodes.

Uploaded by

tejaspk11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

BinarySearchTree Code

The document contains a C program that implements a Binary Search Tree (BST) with various functionalities such as creating the tree, traversing it in different orders (inorder, preorder, postorder), finding the minimum and maximum values, counting leaf and non-leaf nodes, searching for elements, and deleting nodes. It provides a menu-driven interface for user interaction to perform these operations. The program includes functions for node creation, insertion, and traversal, as well as for counting and deleting nodes.

Uploaded by

tejaspk11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>
#include<stdlib.h>

typedef struct Node


{
int data;
struct Node *l,*r;
}node;

int main()
{
node *T,*parent;
int *flag,y,minimum,maximum,leaf,nonleaf,c,x;
node* create(node*);
void in_disp(node*);
void pre_disp(node*);
void post_disp(node*);
int min(node*);
int max(node*);
node* search(node*,int,int*);
int countleaf(node*);
int countnonleaf(node*);
node* del(node*,int);
printf("\n\nMENU\n\n");
printf("\n1. Create a BST \n2. Indorder traversal\n3. Preorder traversal\n4.
Postorder traversal\n5. Minimum of a tree\n6. Maximum of a tree\n7.Minimum of a
tree\n8.Count leaf nodes\n9. Count non-leaf nodes\n 10.Deletion\n 11.Exit\n");
do
{
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
T=create(T);
break;
case 2:
printf("\nInorder traversal:");
in_disp(T);
break;
case 3:
printf("\nPreorder traversal:");
pre_disp(T);
break;
case 4:
printf("\nPostorder traversal:");
post_disp(T);
break;
case 5:
minimum=min(T);
printf("\nMinimum=%d",minimum);
break;
case 6:
maximum=max(T);
printf("\nMaximum=%d",maximum);
break;
case 7:
leaf=countleaf(T);
printf("\nNo. of leaf nodes=%d",leaf);
break;
case 8:
nonleaf=countnonleaf(T);
printf("\nNo. of non leaf nodes=%d",nonleaf);
break;
case 9:
printf("\nEnter the element to be searched:");
scanf("%d",&y);
parent=search(T,y,&flag);

if(flag==1)
printf("Element found");
else
printf("Element not found");
break;
case 10:
printf("Enter the node to be deleted:");
scanf("%d",&x);
T=del(T,x);
break;
case 11:
exit(0);
default:
printf("Invalid choice entered");

}
}
while(1);
}

node* createnode(int a)
{
node* newnode;
newnode=(node*)malloc(sizeof(node));
newnode->data=a;
newnode->l=NULL;
newnode->r=NULL;
return newnode;
}

node *insert(node *T,int x)


{
node *p,*q,*pre;
node* createnode(int);
p=createnode(x);
if(T==NULL)
T=p;
else
{
q=T;
while(q!=NULL)
{
if(x<q->data)
{
pre=q;
q=q->l;
}
else
if(x>q->data)
{
pre=q;
q=q->r;
}
}
if(x<pre->data)
pre->l=p;
else
pre->r=p;
}
return T;
}

node* create(node *T)


{
int x;
T=NULL;
printf("Enter the value of node:");
scanf("%d",&x);
while(x!=99)
{
node *insert(node*,int);
T=insert(T,x);
scanf("%d",&x);
}
return T;
}

node* search(node *T,int x,int *flag)


{
node *p,*par;
*flag=0;
p=T;
while((p!=NULL)&&(*flag==0))
{
if(x<p->data)
{
par=p;
p=p->l;
}
else
if(x>p->data)
{
par=p;
p=p->r;
}
else
*flag=1;
}
return par;
}

void in_disp(node *T)


{
if(T!=NULL)
{
in_disp(T->l);
printf("%d ",T->data);
in_disp(T->r);
}
}

void pre_disp(node *T)


{
if(T!=NULL)
{
printf("%d ",T->data);
in_disp(T->l);
in_disp(T->r);
}
}
void post_disp(node *T)
{
if(T!=NULL)
{
in_disp(T->l);
in_disp(T->r);
printf("%d ",T->data);
}
}

int min(node *T)


{
while(T->l!=NULL)
T=T->l;
return T->data;
}

int max(node *T)


{
while(T->r!=NULL)
T=T->r;
return T->data;
}

int countleaf(node *T)


{
int c;
if(T==NULL)
c=0;
else
if((T->l==NULL)&&(T->r==NULL))
c=1;
else
c=countleaf(T->l)+countleaf(T->r);
return c;
}

int countnonleaf(node *T)


{
int c;
if((T==NULL)||((T->l==NULL)&&(T->r==NULL)))
c=0;
else
c=countnonleaf(T->l)+countnonleaf(T->r)+1;
return c;
}
node* del(node *T,int x)
{
node *parent,*cur,*q,*ps,*s;
int flag;
parent=search(T,x,&flag);
if(flag==0)
printf("Element not found");
else
{
if(x==T->data)
cur=T;
else
if(x<parent->data)
cur=parent->l;
else
cur=parent->r;

if((cur->l==NULL)||(cur->r==NULL))
{
if(cur->l==NULL)
q=cur->r;
else
q=cur->l;

if(x==T->data)
T=q;
else
if(x<parent->data)
parent->l=q;
else
parent->r=q;
}
else
{
ps=NULL;
s=cur->r;
while(s->l!=NULL)
{
ps=s;
s=s->l;
}
cur->data=s->data;
if(ps!=NULL)
ps->l=s->r;
else
cur->r=s->r;
cur=s;
}
free(cur);
}
return T;
}

You might also like