大根堆小根堆

偷学的罒ω罒,非常好用的模版,分享一下。学过堆排应该很容易就看懂了,看不懂学一下堆排,不好懂的地方我也写了注释

小根堆

template<typename T>
class smallest_heap {
private:
	//建堆
	T heap[10001];
	int len;
public:
	smallest_heap();
	void push(T const&);
	void pop();
	T top();
	int size();
	bool empty();
};
template<typename T>
smallest_heap<T>::smallest_heap() {
	len = 0;
	memset(heap, 0, sizeof(heap));
}
template<typename T>
void smallest_heap<T>::push(T const& n) {
	//将元素添加到堆尾
	heap[++len] = n;
	int son = len, father = son / 2;
	//由于是建立小根堆,当子节点小于父节点时,且父节点要大于等于最小下标
	while (heap[son] < heap[father] && father >= 1) {
		//交换
		std::swap(heap[son], heap[father]);
		//不断往上交换
		son = father, father = son / 2;
	}
}
//删除堆顶元素
template<typename T>
void smallest_heap<T>::pop() {
	//交换首元素和尾元素
	std::swap(heap[1], heap[len]);
	//将最后一个元素置0
	heap[len--] = 0;
	int father = 1, son = 2;
	//当子元素下标不超过最后一个
	while (son <= len) {
		//这一行用于找到当前节点的左右孩子中值较小的那一个,将其索引赋给 son。
		if (son<len && heap[son]>heap[son + 1])son++;
		//有序堆,只需要找到一个heap[son]>heap[father]的节点,否则就往上交换
		if (heap[father] > heap[son]) {
			std::swap(heap[father], heap[son]);
			father = son, son = father * 2;
		}
		else break;
	}
}
template<typename T>
T smallest_heap<T>::top() {
	return heap[1];
}
template<typename T>
int smallest_heap<T>::size() {
	return len;
}

template<typename T>
bool smallest_heap<T>::empty() {
	return len;
}

很容易找到堆里的最小元素。

大根堆,改三个大于小于号就好了

template<typename T>
class smallest_heap {
private:
	//建堆
	T heap[10001];
	int len;
public:
	smallest_heap();
	void push(T const&);
	void pop();
	T top();
	int size();
	bool empty();
};
template<typename T>
smallest_heap<T>::smallest_heap() {
	len = 0;
	memset(heap, 0, sizeof(heap));
}
template<typename T>
void smallest_heap<T>::push(T const& n) {
	heap[++len] = n;
	int son = len, father = son / 2;
	//1.大根堆改这里<改成>
	while (heap[son] > heap[father] && father >= 1) {
		//交换
		std::swap(heap[son], heap[father]);
		//不断往上交换
		son = father, father = son / 2;
	}
}
//删除堆顶元素
template<typename T>
void smallest_heap<T>::pop() {
	//交换首元素和尾元素
	std::swap(heap[1], heap[len]);
	//将最后一个元素置0
	heap[len--] = 0;
	int father = 1, son = 2;
	//当子元素下标不超过最后一个
	while (son <= len) {
		//大根堆改这,>改成<
		if (son<len && heap[son]<heap[son + 1])son++;
        //这里同理
		if (heap[father] < heap[son]) {
			std::swap(heap[father], heap[son]);
			father = son, son = father * 2;
		}
		else break;
	}
}
template<typename T>
T smallest_heap<T>::top() {
	return heap[1];
}
template<typename T>
int smallest_heap<T>::size() {
	return len;
}

template<typename T>
bool smallest_heap<T>::empty() {
	return len;
}

相关推荐

  1. 2024-01-06 06:04:03       53 阅读
  2. c/c++ | 优先队列 |

    2024-01-06 06:04:03       55 阅读
  3. 【数据结构】的应用(

    2024-01-06 06:04:03       52 阅读
  4. 的实现和排序

    2024-01-06 06:04:03       25 阅读
  5. leetcode 2386. 找出数组的第 K 和【

    2024-01-06 06:04:03       43 阅读
  6. 存放自定义数据类型的/定义

    2024-01-06 06:04:03       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 06:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 06:04:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 06:04:03       82 阅读
  4. Python语言-面向对象

    2024-01-06 06:04:03       91 阅读

热门阅读

  1. 浏览器刷新页面,缓存的处理方式,强制刷新

    2024-01-06 06:04:03       61 阅读
  2. SpringBoot打造高效多级缓存体系

    2024-01-06 06:04:03       49 阅读
  3. jar to dmg app/windows .exe可执行文件打包方法

    2024-01-06 06:04:03       64 阅读
  4. Grafana相关问题及答案(2024)

    2024-01-06 06:04:03       61 阅读
  5. Vue 3.4 发布

    2024-01-06 06:04:03       54 阅读
  6. git 常用命令 查看文件内容

    2024-01-06 06:04:03       54 阅读
  7. Python技巧

    2024-01-06 06:04:03       53 阅读
  8. apisix 官方example,单机docker的etcd备份和恢复

    2024-01-06 06:04:03       55 阅读