#pragma once
#include <vector>
#include <iostream>
using namespace std;
enum heapType
{
MAXHEAP, MINHEAP
};
template<typename T>
class MyHeap
{
private:
std::vector<T> _vecdate;
int _size;//元素数量
int _capacity;//整个堆的大小
heapType _type;
public:
MyHeap(T data[], int len, heapType type);
MyHeap(int capacity, heapType type);//空堆
//virtual ~MyHeap();
bool push(T date);
bool pop(int index);
void print();
bool full()
{
if (_size >= _capacity)
{
return true;
}
return false;
}
bool empty()
{
return _size == 0;
}
void _swap(int index1, int index2);
private:
void _sink(int index);//元素下沉(删除元素)
void _floating(int index);//元素上浮(添加元素)
};
template <typename T>
MyHeap<T>::MyHeap(T date[], int len, heapType type) :_size(0), _capacity(len), _type(type)
{
_vecdate.resize(_capacity);
for (int i = 0; i < len; i++)
push(date[i]);
}
template<typename T>
MyHeap<T>::MyHeap(int capacity, heapType type) :_size(0), _capacity(capacity), _type(type)
{
_vecdate.resize(_capacity);
}
template<typename T>
bool MyHeap<T>::push(T date)
{
if (full())
{
throw("The heap is full!\n");
return false;
}
else
{
_vecdate[_size] = date;
_size++;
_floating(_size);//调整堆的结构,使得新添加的数据符合堆的形式
return true;
}
}
template<typename T>
bool MyHeap<T>::pop(int index)
{
if (empty())
{
throw("The heap is an empty heap!\n");
return false;
}
else
{
_vecdate[index] = _vecdate[_size - 1];
_size--;
_sink(index + 1);
return true;
}
}
template<typename T>
void MyHeap<T>::print()
{
for (int i = 0; i < _size; i++)
std::cout << _vecdate[i] << " ";
std::cout << std::endl;
return;
}
template<typename T>
void MyHeap<T>::_swap(int index1, int index2)
{
T tmp = _vecdate[index1];
_vecdate[index1] = _vecdate[index2];
_vecdate[index2] = tmp;
return;
}
template<typename T>
void MyHeap<T>::_floating(int index)
{
if (_size == 1)
return;
if (_type == MINHEAP)
{
for (; index > 0; index /= 2)
{
if (_vecdate[index - 1] < _vecdate[index * 0.5 - 1])
_swap(index - 1, index * 0.5 - 1);
else
break;
}
}
else if (_type == MAXHEAP)
{
for (; index > 0; index /= 2)
{
if (_vecdate[index - 1] > _vecdate[index * 0.5 - 1])
_swap(index - 1, index * 0.5 - 1);
else
break;
}
}
return;
}
template<typename T>
void MyHeap<T>::_sink(int index)
{
if (_type == MINHEAP)
{
while (index * 2 <= _size)
{
//左节点
if (_vecdate[index - 1] > _vecdate[index * 2 - 1])
{
_swap(index - 1, index * 2 - 1);
//继续与右节点比较
if (index * 2 + 1 <= _size && _vecdate[index - 1] > _vecdate[index * 2])
{
_swap(index - 1, index * 2);
}
index = index * 2;
}
//右节点
else if (index * 2 + 1 <= _size && _vecdate[index - 1] > _vecdate[index * 2])
{
_swap(index - 1, index * 2);
index = index * 2;
}
else
break;
}
}
else if (_type == MAXHEAP)
{
while (index * 2 <= _size)
{
//左节点
if (_vecdate[index - 1] < _vecdate[index * 2 - 1])
{
_swap(index - 1, index * 2 - 1);
//继续与右节点比较
if (index * 2 + 1 <= _size && _vecdate[index - 1] < _vecdate[index * 2])
{
_swap(index - 1, index * 2);
}
index = index * 2;
}
//右节点
else if (index * 2 + 1 <= _size && _vecdate[index - 1] < _vecdate[index * 2])
{
_swap(index - 1, index * 2);
index = index * 2;
}
else
break;
}
}
else
return;
}