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

#Include : Struct Int Struct Void Struct If

The document describes code for implementing a leftist heap data structure in C. It includes functions for inserting nodes, merging two heaps, deleting nodes, printing the heap, and finding the distance of a node. Main tests the functions by creating a menu-driven program that allows building heaps through insertion and merging, printing the heaps, and deleting nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

#Include : Struct Int Struct Void Struct If

The document describes code for implementing a leftist heap data structure in C. It includes functions for inserting nodes, merging two heaps, deleting nodes, printing the heap, and finding the distance of a node. Main tests the functions by creating a menu-driven program that allows building heaps through insertion and merging, printing the heaps, and deleting nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

M.

Suriya krishna
2013103531
G-Batch,CSE
LEFTIST TREE(HEAP)
#include <stdio.h>
struct node
{
int data,dist;
struct node *right,*left,*prnt;
}*root,*temp,*root3,*root4;
void print(struct node *p)
{
if(p!=NULL)
{
print(p->left);
printf("\t%d",p->data);
print(p->right);
}
}
int distance(struct node *m)
{
if(m==NULL)
{
return(-1);
}
else
{
return(m->dist);
}
}
struct node * merge(struct node *a,struct node *b)
{
if(a==NULL)
return b;
if(b==NULL)
return a;
if(b->data>a->data)
{
temp=b;
b=a;
a=temp;
}
a->right=merge(a->right,b);
if(distance(a->right)>distance(a->left))
{
temp=a->right;
a->right=a->left;
a->left=temp;
}
if(a->right==NULL)
a->dist=0;
else
a->dist=1+(a->right->dist);
return(a);
}
struct node * deletion(struct node * root)
{
printf("deleted element is %d",root->data);
root=merge(root->right,root->left);
}
struct node * insert(struct node *root1)
{
int val;
struct node *newnode,*x;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->right=newnode->left=newnode->prnt=NULL;
newnode->dist=0;
printf("\nenter value");
scanf("%d",&val);
newnode->data=val;
root1=merge(root1,newnode);
printf("root element is %d\ninorder traversal of tree is:",root->
data);
print(root1);
return(root1);
}
void meld()
{
int val;
printf("\n1st tree:");
root3=(struct node *)malloc(sizeof(struct node));
root3->right=root3->left=NULL;
root3->dist=0;
printf("\nenter 1st data");
scanf("%d",&val);
root3->data=val;
while(1)
{
printf("\npress 1 to insert:");
scanf("%d",&val);
if(val)
{
root3=insert(root3);
}
else
break;
}
printf("\n2nd tree:");
root4=(struct node *)malloc(sizeof(struct node));
root4->right=root4->left=NULL;
root4->dist=0;
printf("\nenter 1st data");
scanf("%d",&val);
root4->data=val;
while(1)
{
printf("\npress 1 to insert");
scanf("%d",&val);
if(val)
{
root4=insert(root4);
}
else
break;
}
printf("\n 1st tree");
print(root3);
printf("\n 2nd tree");
print(root4);
root3=merge(root3,root4);
printf("\nroot element is: %d\n",root3->data);
print(root3);
}
int main()
{
int fl=0,val;
printf("\nenter root node:");
scanf("%d",&val);
root=(struct node *)malloc(sizeof(struct node));
root->right=root->left=root->prnt=NULL;
root->dist=0;
root->data=val;
while(1)
{
printf("\nenter your choice\n 1.insert 2.print 3.delete
4.meld 5.exit\n");
scanf("%d",&val);
switch(val)
{
case 1:
root=insert(root);
break;
case 2:
printf("\n");
print(root);
break;
case 3:
root=deletion(root);
break;
case 4:
meld();
break;
case 5:
fl=1;
break;
default:
break;
}
if(fl==1)
break;
}
}

You might also like