You are on page 1of 24

Assignment 10

1. Write a program to implement a binary search tree having the following functions -
Creation, Traversal (Inorder, Preorder, Postorder), Search, Find minimum element, Find
maximum element.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *lptr;
struct node *rptr;
}*root=NULL;

struct stack
{
int data;
struct stack *next;
}*top=NULL;

void create(int n)
{
struct node *new,*parent,*ptr;
new=(struct node *)malloc(sizeof(struct node));
new->data=n;
new->lptr=new->rptr=NULL;
if(root==NULL)
{
root=new;
}
else
{
ptr=root;
parent=NULL;
while(ptr!=NULL)
{
parent=ptr;
if(n<ptr->data)
{
ptr=ptr->lptr;
}
else
{
ptr=ptr->rptr;
}
}
if(n<parent->data)
{
parent->lptr=new;
}
else
{
parent->rptr=new;
}
}
}

void preorder(struct node *t)


{
if(t!=NULL)
{
printf("%d ",t->data);
preorder(t->lptr);
preorder(t->rptr);
}
}

void inorder(struct node *t)


{
if(t!=NULL)
{
inorder(t->lptr);
printf("%d ",t->data);
inorder(t->rptr);
}
}

void postorder(struct node *t)


{
if(t!=NULL)
{
postorder(t->lptr);
postorder(t->rptr);
printf("%d ",t->data);
}
}

void search(int k)
{
struct node *t=root;
if(k==root->data)
{
printf("%d is present in the tree\n",k);
return;
}
else
{
while(t!=NULL)
{
if(k==t->data)
{
printf("%d is present in the tree\n",k);
return;
}
else if(k>t->data)
{
t=t->rptr;
}
else
{
t=t->lptr;
}
}
}
printf("%d is not present in the tree\n",k);
}

void smallest(struct node *t)


{
struct node *ptr=t;
while(ptr->lptr!=NULL)
{
ptr=ptr->lptr;
}
printf("%d ",ptr->data);
}

void largest(struct node *t)


{
struct node *ptr=t;
while(ptr->rptr!=NULL)
{
ptr=ptr->rptr;
}
printf("%d ",ptr->data);
}

int main()
{
int c,ch,n;
do
{
printf("1. Create tree\n2. Preorder traversal\n3. Inorder traversal\n4. Postorder
traversal\n");
printf("5. Search an element\n6. Find smallest element\n7. Find largest element\n8.
Exit\n");
printf("Enter the choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter a number: ");
scanf("%d",&n);
create(n);
break;

case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;

case 5:
printf("Enter a number to search: ");
scanf("%d",&n);
search(n);
break;

case 6: smallest(root);
break;
case 7: largest(root);
break;
case 8: exit(0);
default: printf("Enter a number between 1 and 8\n");
}
printf("\nDo you wish to continue (0 or 1): ");
scanf("%d",&ch);
}while(ch==1);
return 0;
}
2. Add functions to above program for (Home work) -
(i) Deletion
(ii) Count number of leaf nodes
(iii) Calculate the height

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *lptr;
struct node *rptr;
}*root=NULL;
struct stack
{
int data;
struct stack *next;
}*top=NULL;

void create(int n)
{
struct node *new,*parent,*ptr;
new=(struct node *)malloc(sizeof(struct node));
new->data=n;
new->lptr=new->rptr=NULL;
if(root==NULL)
{
root=new;
}
else
{
ptr=root;
parent=NULL;
while(ptr!=NULL)
{
parent=ptr;
if(n<ptr->data)
{
ptr=ptr->lptr;
}
else
{
ptr=ptr->rptr;
}
}
if(n<parent->data)
{
parent->lptr=new;
}
else
{
parent->rptr=new;
}
}
}

void preorder(struct node *t)


{
if(t!=NULL)
{
printf("%d ",t->data);
preorder(t->lptr);
preorder(t->rptr);
}
}

void inorder(struct node *t)


{
if(t!=NULL)
{
inorder(t->lptr);
printf("%d ",t->data);
inorder(t->rptr);
}
}

void postorder(struct node *t)


{
if(t!=NULL)
{
postorder(t->lptr);
postorder(t->rptr);
printf("%d ",t->data);
}
}

void search(int k)
{
struct node *t=root;
if(k==root->data)
{
printf("%d is present in the tree\n",k);
return;
}
else
{
while(t!=NULL)
{
if(k==t->data)
{
printf("%d is present in the tree\n",k);
return;
}
else if(k>t->data)
{
t=t->rptr;
}
else
{
t=t->lptr;
}
}
}
printf("%d is not present in the tree\n",k);
}

void smallest(struct node *t)


{
struct node *ptr=t;
while(ptr->lptr!=NULL)
{
ptr=ptr->lptr;
}
printf("%d ",ptr->data);
}

void largest(struct node *t)


{
struct node *ptr=t;
while(ptr->rptr!=NULL)
{
ptr=ptr->rptr;
}
printf("%d ",ptr->data);
}

void delete_0(int value)


{
struct node *parent=NULL,*ptr=root;
if(ptr==NULL)
{
printf("\nUnderflow\n");
}
else if(ptr->data==value)
{
root=NULL;
}
else
{
while(ptr->data!=value && ptr!=NULL)
{
parent=ptr;
if(value<ptr->data)
{
ptr=ptr->lptr;
}
else
{
ptr=ptr->rptr;
}
}
if(value<parent->data)
{
parent->lptr=NULL;
}
else
{
parent->rptr=NULL;
}
free(ptr);
}
}

void delete_1(int value)


{
struct node *parent=NULL,*ptr=root;
if(ptr==NULL)
{
printf("\nUnderflow\n");
}
else if(ptr->data==value && parent==NULL)
{
struct node *t;
if(ptr->lptr!=NULL)
{
t=ptr->lptr;
}
else
{
t=ptr->rptr;
root=t;
}
}
else
{
while(ptr->data!=value && ptr!=NULL)
{
parent=ptr;
if(value<ptr->data)
{
ptr=ptr->lptr;
}
else
{
ptr=ptr->rptr;
}
}
if(ptr==NULL)
{
printf("\nNo element found\n");
return;
}
else
{
struct node *t;
if(ptr->lptr!=NULL)
{
t=ptr->lptr;
}
else
{
t=ptr->rptr;
}
if(t->data<parent->data)
{
parent->lptr=t;
}
else
{
parent->rptr=t;
}
}
}
}

void delete_2(int value)


{
struct node *parent=NULL,*ptr=root;
if(ptr==NULL)
{
printf("\nUnderflow\n");
}
else
{
while(ptr->data!=value && ptr!=NULL)
{
parent=ptr;
if(value<ptr->data)
{
ptr=ptr->lptr;
}
else
{
ptr=ptr->rptr;
}
}
if(ptr==NULL)
{
printf("\nNo element found\n");
return;
}
else
{
struct node *t;
int temp;
if(ptr->lptr!=NULL)
{
t=ptr->lptr;
while(t->rptr!=NULL)
{
t=t->rptr;
}
temp=t->data;
}
else
{
t=ptr->rptr;
while(t->lptr!=NULL)
{
t=t->lptr;
}
temp=t->data;
}
if(t->lptr==NULL && t->rptr==NULL)
{
delete_0(t->data);
}
else
{
delete_1(t->data);
}
ptr->data=temp;
}
}
}

void delete(int value)


{
struct node *ptr=root;
while(ptr->data!=value && ptr!=NULL)
{
if(value<ptr->data)
{
ptr=ptr->lptr;
}
else
{
ptr=ptr->rptr;
}
}
if(ptr->lptr==NULL && ptr->rptr==NULL)
{
delete_0(value);
}
else if(ptr->lptr!=NULL && ptr->rptr!=NULL)
{
delete_2(value);
}
else
{
delete_1(value);
}
}

static int c=0;


void count(struct node *t)
{
if(t==NULL)
{
return;
}
else
{
if(t->lptr==NULL && t->rptr==NULL)
{
c++;
}
count(t->lptr);
count(t->rptr);
}
}

int height(struct node *t)


{
if(t==NULL)
{
return -1;
}
else
{
int lh=height(t->lptr);
int rh=height(t->rptr);
if(lh>rh)
{
return lh+1;
}
else
{
return rh+1;
}
}
}

int main()
{
int c,ch,n,item,h;
do
{
printf("Menu\n1. Create tree\n2. Preorder traversal\n3. Inorder traversal\n4.
Postorder traversal");
printf("\n5. Search an element\n6. Find smallest element\n7. Find largest
element\n8. Delete");
printf("\n9. Count nodes\n10. Calculate height\n11. Exit\nEnter the choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter a number: ");
scanf("%d",&n);
create(n);
break;

case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;

case 5:
printf("Enter a number to search: ");
scanf("%d",&n);
search(n);
break;

case 6: smallest(root);
break;
case 7: largest(root);
break;

case 8:
printf("Enter element:- ");
scanf("%d",&item);
delete(item);
break;

case 9:
count(root);
printf("\nNo of leaf node:- %d\n",c);
c=0;
break;
case 10:
h=height(root);
if(h==-1)
{
printf("\nNo tree is present\n");
}
else
{
printf("\nHeight is:- %d\n",h);
}
break;

case 11: exit(0);


default: printf("Enter a number between 1 and 11\n");
}
printf("\nDo you wish to continue (0 or 1): ");
scanf("%d",&ch);
}while(ch==1);
return 0;
}
Type your text
NIGAR HUSSAIN
2028065

You might also like