I have written this code after studying from Introduction to Algorithm. I am unable to find out what is the problem with the code. I have written the code for heapsort
and it runs well. heap_extract_max()
will return the maximum value. heap_increase_key()
increase the priority of an element. Here I have written program for priority queue using singly linked list which runs well.
#include <iostream>
#include <vector>
#include <algorithm>
template<typename T>
void max_heapify(std::vector<T>& array, size_t index)
{
size_t largest;
size_t left = (2*index) + 1;
size_t right = left + 1;
if(left < array.size() && array[left] > array[index])
largest = left;
else
largest = index;
if(right < array.size() && array[right] > array[largest])
largest = right;
if(largest != index)
{
int tmp = array[index];
array[index] = array[largest];
array[largest] = tmp;
max_heapify(array, largest);
}
}
template<typename T>
void build_max_heap(std::vector<T>& array)
{
for(size_t i = (array.size()/2) - 1; i >= 0; i--)
max_heapify(array, i);
}
template<typename T>
T heap_maximum(std::vector<T>& array)
{
return array[0];
}
template<typename T>
T heap_extract_max(std::vector<T>& array)
{
if(array.size() < 0)
{
std::cerr << "Heap Underflow \n";
}
else
{
T max = array[0];
array[0] = array[array.size() - 1];
//array.size() = array.size() - 1;
max_heapify(array, 1);
return max;
}
}
template<typename T>
void heap_increase_key(std::vector<T>& array, size_t index, T value)
{
if(value < array[index])
{
std::cerr <<"New value is smaller than the current value\n";
return;
}
else
{
array[index] = value;
while(index > 0 && array[(index/2) - 1] < array[index])
{
std::swap(array[index], array[(index/2) - 1]);
index = (index/2) - 1;
}
}
}
template<typename T>
void max_heap_insert(std::vector<T>& array, T value)
{
array[array.size()] = -1;
heap_increase_key(array, array.size(), value);
}
int main()
{
std::vector<int> v({1, 2, 6, 3, 7});
build_max_heap(v);
std::cout << heap_extract_max(v)<<"\n";
for(size_t i = 0; i < v.size(); i++)
{
std::cout << v[i] << " ";
}
std::cout << "\n";
}
It is not showing any output. I am writing commands
$ g++ -std=c++11 priorityqueue.cpp -o priorityqueue
$ ./priorityqueue
Aucun commentaire:
Enregistrer un commentaire