0% found this document useful (0 votes)
42 views10 pages

Digital Assignment-5: Name: Pratishtha Gaur Reg no:19BCE0104

The document contains pseudocode and C code to implement: 1) A menu driven binary search tree program with functions for insertion, deletion, and traversal (preorder, inorder, postorder). 2) Heap sort algorithm with functions to create a max heap and adjust the heap after removal of the maximum element.

Uploaded by

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

Digital Assignment-5: Name: Pratishtha Gaur Reg no:19BCE0104

The document contains pseudocode and C code to implement: 1) A menu driven binary search tree program with functions for insertion, deletion, and traversal (preorder, inorder, postorder). 2) Heap sort algorithm with functions to create a max heap and adjust the heap after removal of the maximum element.

Uploaded by

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

Digital Assignment-5

Name: Pratishtha Gaur


Reg no:19BCE0104

Que1) Menu driven C program to create binary search tree. Perform insertion and deletion
operations. Display the contents of BST using preorder, inorder and postorder traversal.
Pseudocode:
Insert(element)
{find(ele)
temp->info=item
temp->leftchild=NULL
temp->rightchild=NULL
if(parent=NULL)
root=temp
else
if(item<parent->info)
parent->leftchild=temp
else
parent->rightchild=temp}
delete()
{//case 1 :no child
if(current=parent->leftchild)
parent->leftchild=NULL
else
parent->rightchild=NULL
//case 2 :1 child
struct node *child
if(current->leftchild!=NULL)
child=current->leftchild
else
child=current->rightchild
if(parent=NULL )
root=child
else
if( current=parent->leftchild)
parent->leftchild=child
else
parent->rightchild=child}}
//case 3
struct node *ptr,*ptrsave,*suc,*parsuc
ptrsave=cureent
ptr=current->rightchild;
while(ptr->leftchild!=NULL)
{ptrsave=ptr
ptr=ptr->leftchild}
successor=ptr , parsuc=ptrsave
if(successor->leftchild=NULL and successor->rightchild=NULL)
case_a(parsuc,successor)
else
case_b(parsuc,successor)
if(parent=NULL)
root=successor
else
if(current=parent->leftchild)
parent->leftchild=successor
else
parent->rightchild=successor
successor->leftchild=current->leftchild
successor->rightchild=current->rightchild}
inOrder( root ) {
if (root is not null)
inOrder(root->leftSubtree)
print(root)
inOrder(root->rightSubtree)
}
preOrder( root ) {
if ( root is not null)
print(root)
preOrder(root->leftSubtree)
preOrder(root->rightSubtree)
}
postOrder( root )
{ if (root is not null)
postOrder(root->leftSubtree)
postOrder(root->rightSubtree)
print(root)
}
Code:
# include <stdio.h>
# include <malloc.h>
struct node
{int info;
struct node *lchild;
struct node *rchild;
}*root;
void find(int item,struct node **par,struct node **loc)
{ struct node *ptr,*ptrsave;
if(root==NULL)
{ *loc=NULL; *par=NULL;
return;}
if(item==root->info)
{ *loc=root; *par=NULL;
return;}
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{ if(item==ptr->info)
{ *loc=ptr; *par=ptrsave;
return;}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;}
*loc=NULL; *par=ptrsave;}
void insert(int item)
{ struct node *temp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{ printf("Item already present");
return;}
temp=(struct node *)malloc(sizeof(struct node));
temp->info=item;
temp->lchild=NULL;
temp->rchild=NULL;
if(parent==NULL)
root=temp;
else
if(item<parent->info)
parent->lchild=temp;
else
parent->rchild=temp;}
void case_a(struct node *par,struct node *loc )
{if(par==NULL)
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;}
void case_b(struct node *par,struct node *loc)
{ struct node *child;
if(loc->lchild!=NULL)
child=loc->lchild;
else
child=loc->rchild;
if(par==NULL )
root=child;
else
if( loc==par->lchild)
par->lchild=child;
else
par->rchild=child;}
void case_c(struct node *par,struct node *loc)
{ struct node *ptr,*ptrsave,*suc,*parsuc;
ptrsave=loc; ptr=loc->rchild;
while(ptr->lchild!=NULL)
{ ptrsave=ptr; ptr=ptr->lchild;
}
suc=ptr; parsuc=ptrsave;
if(suc->lchild==NULL && suc->rchild==NULL)
case_a(parsuc,suc);
else
case_b(parsuc,suc);
if(par==NULL)
root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;}
int del(int item)
{ struct node *parent,*location;
if(root==NULL)
{ printf("Tree empty");
return 0;}
find(item,&parent,&location);
if(location==NULL)
{ printf("Item not present in tree");
return 0;}
if(location->lchild==NULL && location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}
int preorder(struct node *ptr)
{ if(root==NULL)
{ printf("Tree is empty");
return 0;}
if(ptr!=NULL)
{ printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);}}
void inorder(struct node *ptr)
{ if(root==NULL)
{ printf("Tree is empty");
return;}
if(ptr!=NULL)
{ inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);}}
void postorder(struct node *ptr)
{ if(root==NULL)
{ printf("Tree is empty");
return;}
if(ptr!=NULL)
{ postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);}}
int main()
{ int ch,ele;
root=NULL;
printf("Menu\n1.Insert\n2.Delete\n3.Inorder Traversal\n4.Preorder Traversal\
n5.Postorder Traversal\n6.Exit");
while(1)
{ printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{ case 1:printf("Enter the number to be inserted : ");
scanf("%d",&ele);
insert(ele); break;
case 2:printf("Enter the number to be deleted : ");
scanf("%d",&ele);
del(ele); break;
case 3:inorder(root); break;
case 4:preorder(root); break;
case 5:postorder(root); break;
case 6: exit(0); break;}}}
Output:

Que 2) Implement C program to perform sorting of n numbers using heap sort technique.
Pseudocode:
create(heap)
{n=heap[0]
for i=n/2 to 1
down_adjust(heap,i)
}
down_adjust(heap[],i)
{let flag=1
n=heap[0]
while(2*i<=n and flag=1)
j=2*i
if(j+1<=n && heap[j+1] > heap[j])
j++
if(heap[i] > heap[j])
flag=0
else
temp=heap[i]
heap[i]=heap[j]
heap[j]=temp
i=j
Code:
#include<stdio.h>
void create(int heap[])
{ int i,n;
n=heap[0];
for(i=n/2;i>=1;i--)
down_adjust(heap,i);}
void down_adjust(int heap[],int i)
{ int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n && flag==1)
{ j=2*i;
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{ temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;}}}
int main()
{ int heap[30],n,i,last,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
heap[0]=n;
create(heap);
while(heap[0] > 1)
{ last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1); }
printf("\nArray after sorting:\n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
return 0;}
Output:

You might also like