min_heap.h
#ifndef MIN_HEAP_H
#define MIN_HEAP_H
#include <vector>
using namespace std;
template <class T>
class min_heap
{
private:
vector<T> v;
int Parent (int index);
int LeftChild (int index);
int RightChild (int index);
void Heapify (vector<T> vec, int index);
void Build_Heap();
public:
min_heap ();
void Insert (T value);
T ExtractMin ();
T Minimum ();
void PrintHeap ();
};
#include "min_heap.cpp"
#endif // MIN_HEAP_H
#include "min_heap.h"
#include <iostream>
using namespace std;
template <class T>
min_heap<T>::min_heap ()
{
v.resize(0);
}
template <class T>
int min_heap<T>::Parent (int index)
{
if (index%2 == 0)
return (index - 1)/2;
else
return index / 2;
}
template <class T>
int min_heap<T>::LeftChild (int index)
{
return 2 * index + 1;
}
template <class T>
int min_heap<T>::RightChild (int index)
{
return 2 * index + 2;
}
template <class T>
void min_heap<T>::Heapify (vector<T> vec, int index)
{
int left = LeftChild (index);
int right = RightChild (index);
int smallest;
T temp;
if (left <= vec.size() && vec[left] < vec[index])
smallest = left;
else
smallest = index;
if (right <= vec.size() && vec[right] < vec[smallest])
smallest = right;
if (smallest != index)
{
temp = vec[index];
vec[index] = vec[smallest];
vec[smallest] = temp;
Heapify(vec, smallest);
}
}
template <class T>
void min_heap<T>::Build_Heap()
{
for (int i = (v.size()/2 - 1); i >= 0; i--)
Heapify(v, i);
}
template <class T>
void min_heap<T>::Insert (T value)
{
v.push_back(value);
int i = v.size() - 1;
while (i > 0 && v[Parent(i)] < value)
{
v[i] = v[Parent(i)];
i = Parent(i);
}
v[i] = value;
}
template <class T>
T min_heap<T>::ExtractMin ()
{
if (v.size() < 1)
cout << "Heap underflow" << endl;
else
{
int smallest = v[0];
v[0] = v[v.size() - 1];
v.pop_back();
Heapify(v, 0);
return smallest;
}
}
template <class T>
T min_heap<T>::Minimum ()
{
}
错误如下:
...min_heap.cpp|6|error: redefinition of 'min_heap<T>::min_heap()'|
...min_heap.cpp|6|error: 'min_heap<T>::min_heap()' previously declared here|
一共20个错误都是这样的
开心每一天1111
撒科打诨
相关分类