0% found this document useful (0 votes)
6 views8 pages

Exp 6

Uploaded by

papu varsha
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)
6 views8 pages

Exp 6

Uploaded by

papu varsha
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/ 8

DATE EX.

NO: 6:
Heap Implementation

AIM:

To write programs for heap implementation .

ALGORITHM:

1. Call the buildMaxHeap() function on the list. Also referred to as heapify(), this

builds a heap from a list in O(n) operations.


2. Swap the first element of the list with the final element. Decrease the

considered range of the list by one.


3. Call the siftDown() function on the list to sift the new first element to its

appropriate index in the heap.


4. Go to step (2) unless the considered range of the list is one element.

5. The buildMaxHeap() operation is run once, and is O(n) in performance. The

siftDown() function is O(log n), and is called n times. Therefore, the


performance of this algorithm is O(n + n log n) = O(n log n).
PROGRAM :
#include <iostream>
#include <cstdlib>
#include <vector>
#include <iterator>
using namespace std ;
class Heap
{
private:
vector <int> heap;
int left(int parent);
int right(int parent);
int parent(int child);
void heapifyup(int index);
void heapifydown(int index);
public:
Heap()
{}
void Insert(int element);
void DeleteMin();
int Extract Min();
void DisplayHeap();
int Size();
};
int Heap::Size()
{
return heap.size();
}
void Heap::Insert(int element)
{
heap.push_back(element);
heapifyup(heap.size() -1);
}
void Heap::DeleteMin()
{
if (heap.size() == 0)37
{
cout<<"Heap is Empty"<<endl;
return;
}
heap[0] = heap.at(heap.size() - 1);
heap.pop_back();
heapifydown(0);
cout<<"Element Deleted"<<endl;
}
int Heap::Extract Min()
{
if (heap.size() == 0)
return -1;
else
return heap.front();
}
void Heap::DisplayHeap()
{
vector <int>::iterator pos = heap.begin();
cout<<"Heap --> ";
while (pos != heap.end())
{
cout<<*pos<<" ";
pos++;
}
cout<<endl;
}
int Heap::left(int parent)
{
int l = 2 * parent + 1;
if(l < heap.size())
return l;
else
return -1;
}
int Heap::right(int parent)
{
int r = 2 * parent + 2;
if(r < heap.size())
return r;
else
return -1;
}
int Heap::parent(int child)
{
int p = (child - 1)/2;
if(child == 0)
return -1;
else
return p;
}
void Heap::heapifyup(int in)
{
if (in >= 0 && parent(in) >= 0 && heap[parent(in)] > heap[in])
{
int temp = heap[in];
heap[in] = heap[parent(in)];
heap[parent(in)] = temp;
heapifyup(parent(in));
}
}
void Heap::heapifydown(int in)
{
int child = left(in);
int child 1 = right(in);
if (child >= 0 && child 1 >= 0 && heap[child] > heap[child 1])
child = child 1;
if (child > 0)
{
int temp = heap[in];
heap[in] = heap[child];
heap[child] = temp;
heapifydown(child);
}
}
int main()
{
Heap h;
while (1)
{
cout<<"------------------ "<<endl;
cout<<"Operations on Heap"<<endl;
cout<<"------------------ "<<endl;
cout<<"1.Insert Element"<<endl;
cout<<"2.Delete Minimum Element"<<endl;
cout<<"3.Extract Minimum Element"<<endl;
cout<<"4.Print Heap"<<endl;
cout<<"5.Exit"<<endl;
int choice, element;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the element to be inserted: ";
cin>>element;
h.Insert(element);
break;
case 2:
h.DeleteMin();
break;
case 3:
cout<<"Minimum Element: ";
if (h.Extract Min() == -1)
cout<<"Heap is Empty"<<endl;
else
cout<<"Minimum Element: "<<h.Extract Min()<<endl;
break;
case 4:
cout<<"Displaying elements of Hwap: ";
h.DisplayHeap();40
break;
case 5:
exit(1);
default:
cout<<"Enter Correct Choice"<<endl;
}
}
return 0;
}

OUTPUT :
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 1
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 2
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 3
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap41
5.Exit
Enter your choice: 1
Enter the element to be inserted: 4
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 5

-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 9
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 4
Displaying elements of Hwap: Heap --> 1 2 3 4 5 9
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 1
Enter the element to be inserted: 7
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 4
Displaying elements of Hwap: Heap --> 1 2 3 4 5 9 7
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 2
Element Deleted
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 4
Displaying elements of Hwap: Heap --> 2 4 3 7 5 9
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice: 3
Minimum Element: Minimum Element: 2
-------------------------
Operations on Heap
-------------------------
1.Insert Element
2.Delete Minimum Element
3.Extract Minimum Element
4.Print Heap
5.Exit
Enter your choice:

RESULT:
Thus the C++ program to Heap tree was written, executed and verified successfully .

You might also like