0% found this document useful (0 votes)
4 views

BST End Sem

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

BST End Sem

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include<stdio.

h>
#include<stdlib.h>

typedef struct treetype


{
struct treetype *left;
int info;
struct treetype *right;
}tree;

void insert(tree **,int);


void inorder(tree *);
void preorder(tree *);
void postorder(tree *);
int deletion(tree *,int);
tree* inordersucc(tree *);
void bigg(tree *);
void small(tree *);
int height(tree *);
void totalnode(tree *,int*);
void onechild(tree *,int*);
void twochild(tree *,int*);
void leafnode(tree *,int*);

int main()
{
tree *root=NULL;
int ch,item,a=0,b=0,c=0,d=0;
do{
printf("enter:--\n1 to insert\n2 to inorder\n3 to preorder\n4 to
postorder\n5 to deletion\n6 to biggest element\n7 to smallest element\n8
to count the total nodes\n9 to count the nodes having one child\n10 to
count the nodes having two child\n11 to count the leaf nodes\n12 to find
height\n:::");
scanf("%d",&ch);

switch (ch)
{
case 1:
printf("enter the value:");
scanf("%d",&item);
insert(&root,item);
break;

case 2:
inorder(root);
break;

case 3:
preorder(root);
break;

case 4:
postorder(root);
break;

case 5:
printf("enter the element to delete:");
scanf("%d",&item);
deletion(root,item);
break;

case 6:
bigg(root);
break;

case 7:
small(root);
break;

case 8:
totalnode(root,&a);
printf("count is %d",a);
break;

case 9:
onechild(root,&b);
printf("count is %d",b);
break;

case 10:
twochild(root,&c);
printf("count is %d",c);
break;

case 11:
leafnode(root,&d);
printf("count is %d",d);
break;
case 12:
printf("height is %d",height(root));
break;

default:
printf("wrong choice\n");
}
printf("enter 1 to continue:");
scanf("%d",&ch);
}while(ch==1);

return 0;
}

void insert(tree **root,int val)


{
if(*root==NULL)
{
tree *temp=NULL;
temp=(tree*)malloc(sizeof(tree));
temp->info=val;
temp->right=temp->left=NULL;
*root=temp;
}
else{
if(val<(*root)->info)
{
insert(&(*root)->left,val);
}
else
{
insert(&(*root)->right,val);
}
}
}

void inorder(tree *root)


{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->info);
inorder(root->right);
}
}

void preorder(tree *root)


{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->left);
preorder(root->right);
}
}

void postorder(tree *root)


{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->info);
}
}

int deletion(tree *root,int val)


{
if(val<root->info)
{
root->left=deletion(root->left,val);
}

else if(val>root->info)
{
root->right=deletion(root->right,val);
}
else{
if(root->left==NULL)
{
tree *temp=NULL;
temp=root->right;
free(root);
return temp;
}

else if(root->right==NULL)
{
tree *temp=NULL;
temp=root->left;
free(root);
return temp;
}
else{
tree *temp=NULL;
temp=inordersucc(root->right);
root->info=temp->info;
root->right=deletion(root->right,temp->info);
}
}
return root;
}

void bigg(tree *root)


{
if(root==NULL)
{
return;
}

else{
if(root!=NULL&&root->right==NULL&&root->left==NULL||root!
=NULL&&root->right==NULL&&root->left!=NULL)
{
printf("%d",root->info);
return;
}
bigg(root->right);
}
}

void small(tree *root)


{
if(root==NULL)
{
return;
}

else{
if(root!=NULL&&root->right==NULL&&root->left==NULL||root!
=NULL&&root->right!=NULL&&root->left==NULL)
{
printf("%d",root->info);
return;
}
small(root->left);
}
}

void totalnode(tree *root,int *a)


{
if(root==NULL)
{
return;
}
else{
(*a)++;
totalnode(root->left,&*a);
totalnode(root->right,&*a);
}
}

tree* inordersucc(tree *root)


{
while(root&&root->left!=NULL)
{
root=root->left;
}
return root;
}
void onechild(tree *root,int *b)
{
if(root==NULL)
{
return;
}
else{
if((root!=NULL&&root->left!=NULL&&root->right==NULL)||(root!
=NULL&&root->left==NULL&&root->right!=NULL))
{
(*b)++;
}
onechild(root->left,&*b);
onechild(root->right,&*b);
}
}
void twochild(tree *root,int *c)
{
if(root==NULL)
{
return;
}
else
{
if(root->left!=NULL&&root->right!=NULL)
{
(*c)++;
}
twochild(root->left,&*c);
twochild(root->right,&*c);
}
}

void leafnode(tree *root,int *d)


{
if(root==NULL)
{
return;
}
else
{
if(root!=NULL&&root->left==NULL&&root->right==NULL)
{
(*d)++;
}
leafnode(root->left,&*d);
leafnode(root->right,&*d);
}
}

int height(tree *root)


{
if(root==NULL)
{
return 0;
}
else{
int dl=height(root->left);
int dr=height(root->right);
if(dl>dr)
{
return(dl+1);
}
else
{
return(dr+1);
}
}
}

You might also like