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

 

### 的概念 是一种特殊的树形数据结构,在这种结构中,根节点的关键字是所有节点关键字的最大值或者最小值。如果父节点总是大于等于子节点,则称为最大;反之则为最小[^1]。 ### 的操作 对于而言,基本操作包括插入新元素、删除任意位置上的元素以及调整使得其满足定义条件等。这些操作通常可以在 O(log n) 的时间复杂度下完成。当向中添加一个新的键值时,会先将其放置于最底层的第一个可用位置处,随后通过比较并交换该结点与其双亲直到整个序列重新成为合法的为止。同样地,在移除某个特定项之后也需要执行类似的修复过程来维持原有特性不变。 ### 应用场景 #### 优先队列 基于可以高效地构建起支持动态更新最高/低权重项目的容器——即所谓的“优先级队列”。由于每次仅需访问顶端即可获得全局最优解,并且维护成本较低廉(增删改查均为对数级别),因此非常适合处理实时性强的任务分配场合[^3]。 #### 排序算法 借助于上述提到的数据组织形式还可以设计出一类性能优良而又易于理解掌握的方法论体系之一:“排序法”,这是一种不依赖额外间资源就能达到稳定性的内部分类手段。具体做法是在初始化阶段建立初始状态下的大顶,接着不断取出当前剩余部分里的首位成员放到最终有序数组里去,再把剩下的继续构造成新的小规模版本重复此流程直至结束。 #### 调度机制 操作系统内核层面也广泛采用了此类抽象模型来进行进程间抢占式CPU使用权的竞争管理等工作。比如Linux kernel就实现了多种不同类型的等待队列用来协调各个线程之间的协作关系,其中不乏运用到了红黑树加链表混合体这样的高级形态作为底层支撑组件的一部分。 ```c // C语言实现最大一个简单例子 #include <stdio.h> #define MAX_HEAP_SIZE 100 int heap[MAX_HEAP_SIZE]; int size = 0; void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } void max_heapify_up(int index){ while(index > 0 && heap[(index - 1)/2] < heap[index]){ swap(&heap[(index - 1)/2], &heap[index]); index = (index - 1)/2; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值