介绍:
最大堆是一种常见的数据结构,它具有以下特性:父节点的值大于或等于其子节点的值。在本文中,我们将介绍如何使用C++语言实现一个最大堆,并提供详细的代码解析。
实现最大堆的C++代码示例及详细解析:
#include <iostream>
#include <vector>
using namespace std;
class MaxHeap {
private:
vector<int> maxHeap; // 存储最大堆
int heapSize;
int realSize;
public:
// 构造函数
MaxHeap(int size) : heapSize(size), realSize(0) {
maxHeap.resize(heapSize + 1);
maxHeap[0] = 0;
}
void add(int element) {
realSize++;
// 如果实际元素数量超过了数组大小,则提示错误并返回
if (realSize > heapSize) {
cout << "Add too many elements!" << endl;
realSize--;
return;
}
else {
maxHeap[realSize] = element;
int index = realSize;
int parent = index / 2;
while (index > 1 && maxHeap[index] > maxHeap[parent]) {
swap(maxHeap[index], maxHeap[parent]);
index = parent;
parent = index / 2;
}
}
}
int peek() {
if (realSize == 0) {
cout << "Heap is empty!" << endl;
return INT_MIN;
}
return maxHeap[1];
}
int pop() {
if (realSize < 1) { // 如果堆中没有元素,则返回错误提示
cout << "Don't have any element!" << endl;
return INT_MIN;
}
else {
int removeElement = maxHeap[1]; // 获取堆顶元素
maxHeap[1] = maxHeap[realSize]; // 将堆中最后一个元素移到堆顶
realSize--;
int index = 1;
// 当删除的元素不是叶子节点时,交换元素直到满足最大堆的性质
while (index <= realSize / 2) {
int left = index * 2; // 左孩子节点索引
int right = (index * 2) + 1; // 右孩子节点索引
int largest = index;
if (left <= realSize && maxHeap[left] > maxHeap[largest]) {
largest = left;
}
if (right <= realSize && maxHeap[right] > maxHeap[largest]) {
largest = right;
}
if (largest != index) {
swap(maxHeap[index], maxHeap[largest]);
index = largest;
}
else {
break;
}
}
return removeElement;
}
}
int size() {
return realSize;
}
void printHeap() {
if (realSize == 0) {
cout << "No element!" << endl;
}
else {
cout << "[";
for (int i = 1; i < realSize; i++) {
cout << maxHeap[i] << ",";
}
cout << maxHeap[realSize] << "]" << endl;
}
}
int search(int val) {
for (int i = 1; i <= realSize; i++) {
if (maxHeap[i] == val) {
return i;
}
}
return 0;
}
int delete_(int val) {
int index = search(val);
if (index == 0) {
cout << "Element not found!" << endl;
return INT_MIN;
}
int removeElement = maxHeap[index];
maxHeap[index] = maxHeap[realSize];
realSize--;
// 调整堆
while (index <= realSize / 2) {
int left = index * 2; // 左子节点
int right = (index * 2) + 1; // 右子节点
int largest = index;
if (left <= realSize && maxHeap[left] > maxHeap[largest]) {
largest = left;
}
if (right <= realSize && maxHeap[right] > maxHeap[largest]) {
largest = right;
}
if (largest != index) {
swap(maxHeap[index], maxHeap[largest]);
index = largest;
}
else {
break;
}
}
return removeElement;
}
};
int main() {
MaxHeap maxheap(5);
maxheap.add(1);
maxheap.add(2);
maxheap.add(3);
maxheap.printHeap(); // [3,1,2]
cout << maxheap.peek() << endl; // 3
cout << maxheap.pop() << endl; // 3
maxheap.delete_(1); // 删除值为1的元素
maxheap.printHeap(); // [2],删除了1后剩下的元素
maxheap.add(4);
maxheap.add(5);
maxheap.printHeap(); // [5,4,2]
while (maxheap.size() > 0) {
cout << maxheap.pop() << endl;
}
return 0;
}
C++标准库中提供了用于实现最大堆的相关功能的库函数,主要包括在头文件 <queue>
中定义的 priority_queue
类模板。priority_queue
类模板是一个模拟队列(queue)的容器适配器,它使用了比较函数来确定元素的优先级,因此可以用来实现最大堆。
#include <iostream>
#include <queue> // 包含 priority_queue 头文件
int main() {
// 创建一个最大堆
std::priority_queue<int> maxHeap;
// 向最大堆中添加元素
maxHeap.push(3);
maxHeap.push(1);
maxHeap.push(4);
maxHeap.push(1);
maxHeap.push(5);
// 获取堆顶元素(最大值)
std::cout << "Top element of the max heap: " << maxHeap.top() << std::endl;
// 弹出堆顶元素
maxHeap.pop();
// 打印最大堆中所有元素
std::cout << "Max heap elements: ";
while (!maxHeap.empty()) {
std::cout << maxHeap.top() << " ";
maxHeap.pop();
}
std::cout << std::endl;
return 0;
}
代码解析:
-
MaxHeap 类:实现了最大堆的各种功能,包括添加元素、获取堆顶元素、弹出堆顶元素、获取堆中元素个数和打印堆等。
-
add() 函数:向最大堆中添加元素,并通过调整堆的结构以满足最大堆的性质。
-
peek() 函数:获取堆顶元素(最大值)。
-
pop() 函数:删除堆顶元素,并通过调整堆的结构以满足最大堆的性质。
-
printHeap() 函数:打印最大堆中的所有元素。
总结:
最大堆是一种常见的数据结构,在实际编程中具有广泛的应用场景。掌握了最大堆的实现方法,可以为我们解决许多实际问题提供便利。
希望本文能够对您理解最大堆的原理和使用有所帮助,欢迎大家提出宝贵的意见和建议!
参考文献:
-
C++ Reference - priority_queue: https://en.cppreference.com/w/cpp/container/priority_queue
-
Introduction to Algorithms (Third Edition) by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein.