9
Ex.No: IMPLEMENTATION OF LEFTIST TREE AIM: To write a java program to implement the leftist heap with insert and delete operations. ALGORITHM: STEP 1: Start the program by defining functions. STEP 2: Keep the minimum pointer to the root element. STEP 3: The insert and delete operations are performed with the help of combining two trees. STEP 4: The insert operations are performed by combining the two leftist trees. STEP 5: The deleteMin() is used to delete the minimum element in the heap. STEP 6: After the insert and delete operations leftist heap elements are displayed. STEP 7: Stop the program.

Leftist

Embed Size (px)

DESCRIPTION

c coding for leftist heap in data structure

Citation preview

Page 1: Leftist

Ex.No: IMPLEMENTATION OF LEFTIST TREE

AIM:

To write a java program to implement the leftist heap with insert and delete operations.

ALGORITHM:

STEP 1: Start the program by defining functions.

STEP 2: Keep the minimum pointer to the root element.

STEP 3: The insert and delete operations are performed with the help of combining two trees.

STEP 4: The insert operations are performed by combining the two leftist trees.

STEP 5: The deleteMin() is used to delete the minimum element in the heap.

STEP 6: After the insert and delete operations leftist heap elements are displayed.

STEP 7: Stop the program.

Page 2: Leftist

CODING:

#include<iostream.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>

class leftist{friend class tree;private:int data,sht;leftist *left, *right;public:leftist() {data=0;left=NULL;right=NULL;}};class tree{private:leftist *root;leftist * merge(leftist *a,leftist *b);void levelorder(leftist *t);public:void create();void insert(int item);void mergetree(leftist *b);void traversal();void delet();};void tree::create(){root=new leftist();root=NULL;}void tree::insert(int item){leftist *temp=new leftist();temp->data=item;temp->sht=1;temp->left=NULL;temp->right=NULL;mergetree(temp);

Page 3: Leftist

}void tree::mergetree(leftist *b){if(root==NULL)root=b;else if(b!=NULL){root=merge(root,b);b=NULL;}}leftist * tree::merge(leftist *a,leftist *b){leftist *tmp;if(a->data>b->data){tmp=a;a=b;b=tmp;}if(a->right==NULL)a->right=b;elsea->right=merge(a->right,b);if((a->left==NULL)||(a->left->sht<a->right->sht)){tmp=a->left;a->left=a->right;a->right=tmp;}if(a->right!=NULL)a->sht=a->right->sht+1;elsea->sht=1;return a;}void tree::traversal(){ levelorder(root);}void tree::levelorder(leftist *t){ if(t==NULL)return;else{ levelorder(t->left); if(t->data) {

Page 4: Leftist

cout<<t->data<<","<<t->sht;if(t->left==NULL)cout<<"\t0";elsecout<<"\t"<<t->left->data;if(t->right==NULL)cout<<"\t0\n";elsecout<<"\t"<<t->right->data<<"\n";}levelorder(t->right);}}void tree::delet(){int del;del=root->data;if(root->right==NULL)root=root->left;elseroot=merge(root->left,root->right);cout<<"Deleted element = "<<del<<endl;}void main(){int num,i,nb,opt;char ch;clrscr();tree tr;cout<<”\t* * * Implementation of Leftist Tree * * *\n”;cout<<"Enter the number of elements to be inserted: ";cin>>num;tr.create();for(i=0;i<num;i++){cout<<"Enter the element :";cin>>nb;tr.insert(nb);}cout<<"Options:\n1.Insert\n2.Display\n3.Delete\n";do{cout<<"Enter the option: ";cin>>opt;switch(opt){case 1:cout<<"Enter the element to be inserted: ";cin>>nb;

Page 5: Leftist

tr.insert(nb);break;case 2:cout<<"Elements in the Leftist Tree are\n";tr.traversal();cout<<endl;break;case 3:tr.delet();break;}printf("Enter y to continue: ");scanf("%c",&ch);}while(ch=='y');}

Page 6: Leftist

OUTPUT:

* * * Implementation of Leftist Tree * * *\Enter the number of elements to be inserted: 5Enter the element: 25Enter the element: 23Enter the element: 45Enter the element: 12Enter the element: 65

Enter1. Insert2. Display3. DeleteEnter the option: 2

Elements in the Leftist Tree are25, 1 0 023, 2 25 4545, 1 0 012, 2 23 6565, 1 0 0

Enter y to continue: yEnter the option: 3

Deleted element = 12Enter y to continue: yEnter the option: 2

Elements in the Leftist Tree are25, 1 0 023, 2 25 4565, 1 0 045, 1 65 0

Enter y to continue: n

Page 7: Leftist

RESULT:

Thus the c++ program for implementing leftist tree was executed successfully.