最大堆相关操作

来源:互联网 发布:人工智能入门教程 编辑:程序博客网 时间:2024/04/29 16:59

原理可参见:点击打开链接

运行代码:

#include<iostream>using namespace std;class heapSort{private:int* array;int maxSize;int currentSize;public:heapSort();~heapSort();void buildHeap(int size);bool isLeftChild(int pos);bool isRightChild(int pos);int getLeftChild(int pos);int getRightChild(int pos);int getParent(int pos);int deleteValue(int pos);void insertValue(int pos, int data);int deleteMax();void makeHeap(int *array, int i, int currentSize);void getResult();};heapSort::heapSort(){array = NULL;maxSize = 0;currentSize = 0;}heapSort::~heapSort(){if (array != NULL) {delete[]array;}array = NULL;maxSize = 0;currentSize = 0;}bool heapSort::isLeftChild(int pos){   //判断左孩子是否存在if (pos < 0 || pos >= currentSize) {cout << "输入数据长度有误" << endl;return false;}if (pos * 2 + 1< currentSize) {return true;}else {return false;}}bool heapSort::isRightChild(int pos){   //判断右孩子是否存在if (pos < 0 || pos >= currentSize) {cout << "输入数据长度有误" << endl;return false;}if ((pos * 2 + 2)< currentSize) {return true;}else {return false;}}int heapSort::getLeftChild(int pos){   //得到左孩子的角标if (pos < 0 || pos >= currentSize) {cout << "输入数据长度有误" << endl;return -1;}if (isLeftChild(pos)) {return pos * 2 + 1;}}int heapSort::getRightChild(int pos){if (pos < 0 || pos >= currentSize) {   //得到右孩子的角标cout << "输入数据长度有误" << endl;return -1;}if (isRightChild(pos)) {return pos * 2 + 2;}}int heapSort::getParent(int pos){if (pos < 0 || pos >= currentSize) {      //得到父结点的角标cout << "输入数据长度有误" << endl;return -1;}return (pos - 1) / 2;}int heapSort::deleteValue(int pos){    //删除固定角标的结点if (pos < 0 || pos >= currentSize) {cout << "输入数据长度有误" << endl;return -1;}int temp = array[pos];array[pos] = array[currentSize - 1];currentSize--;for (int i = currentSize / 2; i > 0; i--) {makeHeap(array, pos, currentSize);}return temp;}void heapSort::insertValue(int pos, int data){   //在堆的随意位置插入if (pos < 0 || pos >= currentSize) {cout << "输入数据长度有误" << endl;return ;}if (currentSize + 1 > maxSize) {cout << "当前可插入空间已满无法插入" << endl;return;}currentSize++;array[currentSize - 1] = array[pos];array[pos] = data;for (int i = currentSize / 2; i > 0; i--) {makeHeap(array, pos, currentSize);}}int heapSort::deleteMax(){int temp = array[0];   //记录下最大值array[0] = array[currentSize - 1];   //为删除的最大值填充成最小的重新排序currentSize--;for (int i = currentSize / 2; i > 0; i--) {makeHeap(array, i, currentSize);}return temp;}void heapSort::makeHeap(int *array, int i, int currentSize) {   //比较排序int large = i;int left = getLeftChild(i);int right = getRightChild(i);if (left && array[left] > array[large]) {large = left;}if (right && array[right] > array[large]) {large = right;}if (large != i) {int temp = array[i];array[i] = array[large];array[large] = temp;makeHeap(array, large, currentSize);}}void heapSort::buildHeap(int maxSize) {   //初始化array = new int[maxSize];this->maxSize = maxSize;cout << "请输入要构建的数据大小:" << endl;int num = 0;cin >> num;currentSize = num;for (int i = 0; i < currentSize; i++) {cin >> array[i];}for (int i = currentSize / 2; i > 0; i--) {makeHeap(array, i, currentSize);   //构造最大堆}}void heapSort::getResult() {   //输出堆int count = 0;int rem = -1;for (int i = 0; i < currentSize; i++) {cout << array[i] << " ";if ((i - rem) == pow(2, count)) {cout << endl;rem = i;count++;}}cout << endl;}int main() {heapSort h;int maxsize = 0;cout << "请输入您想创建的最大结点值:" << endl;cin >> maxsize;h.buildHeap(maxsize);cout << "输入数据构成的堆是:" << endl;h.getResult();h.deleteMax();cout << endl << "删除最大元素后的堆是:" << endl;h.getResult();cout << endl << "请输入您想要删除的坐标数字:" << endl;int n = 0;cin >> n;h.deleteValue(n);cout << endl << "删除后的结果为:" << endl;h.getResult();cout << endl << "请输入想要插入的值的角标和值的大小:" << endl;int data;cin >> n >> data;h.insertValue(n, data);cout << endl << "插入后的结果是:" << endl;h.getResult();return 0;}


0 0