heap构建

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值