//heap.h
#pragma once
#include<iostream>
using namespace std;
class Heap {//静态实现堆
private:
int count; //堆中元素个数
int capacity; //堆的大小
int *array; //数组实现堆
int heap_type; //最小堆或最大堆,0为最小,1为最大
public:
Heap(int capacitay, int heap_type);//堆得容量,堆得类型
~Heap();
int Parents(int i);//得到父亲下标
int LeftChild(int i);//左孩子
int RightChild(int i);//右孩子
int GetMaximum();//得到堆头
void PercolateDown(int i);//向下堆化
int Delete();
void Resize();
int Insert(int i);//插入
void Build(Heap &H, int a[], int n);//利用现有数组构建堆
void Heapsort();//堆排序
};
//heap.cpp
#include"heap.h" Heap::Heap(int capacitay, int heap_type) { this->heap_type = heap_type; this->capacity = capacitay; this->array = new int[capacitay]; this->count = 0; cout << "Creation is OK~" << endl; } Heap::~Heap() { this->count = 0; this->array = NULL; } int Heap::Parents(int i) { if (i <= 0 || i >= this->count) return -1; return i - 1 / 2; } int Heap::LeftChild(int i) { int left = 2 * i + 1; if (left >= this->count) return -1; return left; } int Heap::RightChild(int i) { int right = 2 * i + 2; if (right >= this->count) return -1; return right; } int Heap::GetMaximum() { if (this->count == 0) return -1; return this->array[0]; } void Heap::PercolateDown(int i)//向下渗透 { int max, temp; int l = LeftChild(i); int r = RightChild(i); if(this->heap_type==0) { if (l != -1 && this->array[l] > this->array[i]) max = l; else max = i; if (r != -1 && this->array[r] > this->array[max]) max = r; if (max != i) { temp = this->array[i]; this->array[i] = this->array[max]; this->array[max] = temp; PercolateDown(max); } } else { if (l != -1 && this->array[l] < this->array[i]) max = l; else max = i; if (r != -1 && this->array[r] < this->array[max]) max = r; if (max != i) { temp = this->array[i]; this->array[i] = this->array[max]; this->array[max] = temp; PercolateDown(max); } } } int Heap::Delete() { if (this->count == 0) return -1; int top = this->array[0]; this->array[0] = this->array[this->count - 1]; this->count--; PercolateDown(0); return top; } void Heap::Resize() { int *array_old = new int[this->capacity]; for (int i = 0; i < this->count; i++) array_old[i] = array[i]; this->array = new int[this->capacity * 2]; for (int i = 0; i < this->count; i++) array[i] = array_old[i]; this->capacity *= 2; array_old = NULL; } int Heap::Insert(int i) { int temp; if (this->capacity == this->count) Resize(); this->count++; temp = this->count - 1; while(i >= 0 && i>this->array[(temp-1)/2]) { this->array[temp] = this->array[(temp - 1) / 2]; temp = temp - 1 / 2; } this->array[temp] = i; return 1; } void Heap::Build(Heap &H, int a[], int n) { // if (H.array == NULL) // return; while (n > H.capacity) H.Resize(); for (int i = 0; i < n; i++) H.array[i] = a[i]; H.count = n; for (int i = (n - 2) / 2; i >= 0; i--) H.PercolateDown(i); } void Heap::Heapsort()//堆排序 { while (this->count > 0) { cout << this->GetMaximum(); this->Delete(); } }
//main.cpp
#include"heap.h" void main()//最小堆 { int a[8] = {}; int type,i; for (i = 0; i < 8;i++) { cin >> a[i];} cin >> type; Heap H(6,type); H.Build(H, a, 8); H.Heapsort(); }