2023-02-04 15:58:39 +08:00
|
|
|
/**
|
|
|
|
* File: my_heap.cpp
|
2023-02-04 16:33:46 +08:00
|
|
|
* Created Time: 2023-02-04
|
|
|
|
* Author: LoneRanger (836253168@qq.com), what-is-me (whatisme@outlook.jp)
|
2023-02-04 15:58:39 +08:00
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.hpp"
|
2023-02-04 15:53:58 +08:00
|
|
|
|
2023-02-07 19:05:19 +08:00
|
|
|
/* 大顶堆 */
|
2023-02-04 15:53:58 +08:00
|
|
|
class MaxHeap {
|
2023-04-14 03:44:02 +08:00
|
|
|
private:
|
2023-08-20 14:51:39 +08:00
|
|
|
// 使用动态数组,这样无须考虑扩容问题
|
2023-02-04 15:53:58 +08:00
|
|
|
vector<int> maxHeap;
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取左子节点索引 */
|
2023-02-04 15:53:58 +08:00
|
|
|
int left(int i) {
|
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取右子节点索引 */
|
2023-02-04 15:53:58 +08:00
|
|
|
int right(int i) {
|
|
|
|
return 2 * i + 2;
|
2023-04-14 03:44:02 +08:00
|
|
|
}
|
2023-02-04 15:53:58 +08:00
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取父节点索引 */
|
2023-02-04 15:53:58 +08:00
|
|
|
int parent(int i) {
|
2023-02-04 16:33:46 +08:00
|
|
|
return (i - 1) / 2; // 向下取整
|
2023-02-04 15:53:58 +08:00
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-02-08 04:17:26 +08:00
|
|
|
void siftUp(int i) {
|
2023-02-04 15:53:58 +08:00
|
|
|
while (true) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 获取节点 i 的父节点
|
2023-04-14 03:44:02 +08:00
|
|
|
int p = parent(i);
|
2023-08-20 14:51:39 +08:00
|
|
|
// 当“越过根节点”或“节点无须修复”时,结束堆化
|
2023-02-04 16:33:46 +08:00
|
|
|
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
2023-02-04 15:53:58 +08:00
|
|
|
break;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 交换两节点
|
2023-02-04 16:33:46 +08:00
|
|
|
swap(maxHeap[i], maxHeap[p]);
|
2023-02-04 15:53:58 +08:00
|
|
|
// 循环向上堆化
|
|
|
|
i = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-02-08 04:17:26 +08:00
|
|
|
void siftDown(int i) {
|
2023-02-04 15:53:58 +08:00
|
|
|
while (true) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-02-04 15:53:58 +08:00
|
|
|
int l = left(i), r = right(i), ma = i;
|
2023-08-20 14:51:39 +08:00
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
2023-04-14 03:44:02 +08:00
|
|
|
if (l < size() && maxHeap[l] > maxHeap[ma])
|
2023-02-04 15:53:58 +08:00
|
|
|
ma = l;
|
|
|
|
if (r < size() && maxHeap[r] > maxHeap[ma])
|
|
|
|
ma = r;
|
2023-08-20 14:51:39 +08:00
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
2023-04-14 03:44:02 +08:00
|
|
|
if (ma == i)
|
2023-02-04 16:33:46 +08:00
|
|
|
break;
|
|
|
|
swap(maxHeap[i], maxHeap[ma]);
|
2023-02-04 15:53:58 +08:00
|
|
|
// 循环向下堆化
|
|
|
|
i = ma;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
public:
|
2023-02-10 01:04:26 +08:00
|
|
|
/* 构造方法,根据输入列表建堆 */
|
2023-02-04 15:53:58 +08:00
|
|
|
MaxHeap(vector<int> nums) {
|
|
|
|
// 将列表元素原封不动添加进堆
|
|
|
|
maxHeap = nums;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 堆化除叶节点以外的其他所有节点
|
2023-02-04 15:53:58 +08:00
|
|
|
for (int i = parent(size() - 1); i >= 0; i--) {
|
2023-02-08 04:17:26 +08:00
|
|
|
siftDown(i);
|
2023-02-04 15:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
int size() {
|
|
|
|
return maxHeap.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
2023-09-14 03:36:31 +08:00
|
|
|
bool isEmpty() {
|
2023-02-04 15:53:58 +08:00
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
int peek() {
|
2023-02-04 16:33:46 +08:00
|
|
|
return maxHeap[0];
|
2023-02-04 15:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
void push(int val) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 添加节点
|
2023-02-04 15:53:58 +08:00
|
|
|
maxHeap.push_back(val);
|
|
|
|
// 从底至顶堆化
|
2023-02-08 04:17:26 +08:00
|
|
|
siftUp(size() - 1);
|
2023-02-04 15:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:31:05 +08:00
|
|
|
void pop() {
|
2023-02-04 15:53:58 +08:00
|
|
|
// 判空处理
|
2023-09-14 03:36:31 +08:00
|
|
|
if (isEmpty()) {
|
2023-02-11 18:04:29 +08:00
|
|
|
throw out_of_range("堆为空");
|
2023-02-04 16:33:46 +08:00
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
2023-02-04 16:33:46 +08:00
|
|
|
swap(maxHeap[0], maxHeap[size() - 1]);
|
2023-04-09 04:32:17 +08:00
|
|
|
// 删除节点
|
2023-02-04 15:53:58 +08:00
|
|
|
maxHeap.pop_back();
|
|
|
|
// 从顶至底堆化
|
2023-02-08 04:17:26 +08:00
|
|
|
siftDown(0);
|
2023-02-04 15:53:58 +08:00
|
|
|
}
|
|
|
|
|
2023-02-04 16:33:46 +08:00
|
|
|
/* 打印堆(二叉树)*/
|
2023-02-04 15:53:58 +08:00
|
|
|
void print() {
|
2023-02-04 16:33:46 +08:00
|
|
|
cout << "堆的数组表示:";
|
2023-04-14 03:44:02 +08:00
|
|
|
printVector(maxHeap);
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "堆的树状表示:" << endl;
|
2023-07-19 16:09:27 +08:00
|
|
|
TreeNode *root = vectorToTree(maxHeap);
|
2023-04-14 03:44:02 +08:00
|
|
|
printTree(root);
|
2023-02-07 01:17:10 +08:00
|
|
|
freeMemoryTree(root);
|
2023-02-04 15:53:58 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-04 16:33:46 +08:00
|
|
|
/* Driver Code */
|
2023-02-04 15:53:58 +08:00
|
|
|
int main() {
|
|
|
|
/* 初始化大顶堆 */
|
2023-02-04 16:33:46 +08:00
|
|
|
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
|
2023-03-13 22:31:05 +08:00
|
|
|
MaxHeap maxHeap(vec);
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "\n输入列表并建堆后" << endl;
|
2023-03-13 22:31:05 +08:00
|
|
|
maxHeap.print();
|
2023-02-04 15:53:58 +08:00
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
2023-03-13 22:31:05 +08:00
|
|
|
int peek = maxHeap.peek();
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "\n堆顶元素为 " << peek << endl;
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
int val = 7;
|
2023-03-13 22:31:05 +08:00
|
|
|
maxHeap.push(val);
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "\n元素 " << val << " 入堆后" << endl;
|
2023-03-13 22:31:05 +08:00
|
|
|
maxHeap.print();
|
2023-02-04 15:53:58 +08:00
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
2023-03-13 22:31:05 +08:00
|
|
|
peek = maxHeap.peek();
|
|
|
|
maxHeap.pop();
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "\n堆顶元素 " << peek << " 出堆后" << endl;
|
2023-03-13 22:31:05 +08:00
|
|
|
maxHeap.print();
|
2023-02-04 15:53:58 +08:00
|
|
|
|
|
|
|
/* 获取堆大小 */
|
2023-03-13 22:31:05 +08:00
|
|
|
int size = maxHeap.size();
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "\n堆元素数量为 " << size << endl;
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
2023-09-14 03:36:31 +08:00
|
|
|
bool isEmpty = maxHeap.isEmpty();
|
2023-02-04 15:53:58 +08:00
|
|
|
cout << "\n堆是否为空 " << isEmpty << endl;
|
2023-06-12 23:04:01 +08:00
|
|
|
|
|
|
|
return 0;
|
2023-02-04 16:33:46 +08:00
|
|
|
}
|