Binary Heap
来源:互联网 发布:国外便宜域名注册 编辑:程序博客网 时间:2024/06/07 10:13
BinaryHeap.h:
#include <iostream>#include <string>#define MAX_HEAP_SIZE 101class BinaryMinHeap {public:BinaryMinHeap(int *heapArray);BinaryMinHeap(int size);~BinaryMinHeap() {delete []heapArray;}void insert(int value);void removeMin();int getMin();void displayHeapArray();private:int *heapArray;int heapSize;int heapArraySize;void siftUp(int nodeIndex);void siftDown(int nodeIndex);int getLeftChildIndex(int nodeIndex) {return 2*nodeIndex+1;}int getRightChildIndex(int nodeIndex) {return 2*nodeIndex+2;}int getParentIndex(int nodeIndex) {return (nodeIndex-1)/2;}};
BinaryHeap.cpp:
#include "BinaryHeap.h"#include <stdio.h>BinaryMinHeap::BinaryMinHeap(int size) {heapArray = new int[size];heapSize = 0;heapArraySize = size;}void BinaryMinHeap::insert(int value) {if(heapSize == heapArraySize) {throw std::string("Insertion Error: The heap is full!");} else {heapSize++;heapArray[heapSize-1] = value;siftUp(heapSize-1);}}/* * NON-Recursive Version */void BinaryMinHeap::siftUp(int nodeIndex) {int i = nodeIndex;int parent = getParentIndex(nodeIndex);while(parent >= 0) {if(heapArray[parent] > heapArray[i]) {int tmp = heapArray[parent];heapArray[parent] = heapArray[i];heapArray[i] = tmp;i = parent;parent = getParentIndex(parent);} else {break;}}//fprintf(stderr, "parent = %d\n", parent);}void BinaryMinHeap::removeMin() {if(heapSize == 0) {throw std::string("Remove Error: The heap is empty!");} else {heapArray[0] = heapArray[heapSize - 1];heapSize--;siftDown(0);}}void BinaryMinHeap::siftDown(int nodeIndex) {int leftIndex = getLeftChildIndex(nodeIndex);int rightIndex = getRightChildIndex(nodeIndex);int minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;while(minIndex <= heapSize) {if(heapArray[minIndex] < heapArray[nodeIndex]) {int tmp = heapArray[minIndex];heapArray[minIndex] = heapArray[nodeIndex];heapArray[nodeIndex] = tmp;nodeIndex = minIndex;leftIndex = getLeftChildIndex(nodeIndex);rightIndex = getRightChildIndex(nodeIndex);minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;} else {break;}}}void BinaryMinHeap::displayHeapArray() {if(heapSize == 0) {throw std::string("Empty Heap!");} else {std::cout << "\n--------------------\n";std::cout << "heap size is:\t" << heapSize << std::endl; for(int i = 0; i < heapSize; i++) {std::cout << heapArray[i] << "\t";}std::cout << "\n--------------------\n";}}int BinaryMinHeap::getMin() {return heapArray[0];}
Main.cpp:
#include "BinaryHeap.h"using namespace std;int main() {BinaryMinHeap minHeap(10);minHeap.insert(10);minHeap.displayHeapArray();minHeap.insert(23);minHeap.displayHeapArray();minHeap.insert(11);minHeap.displayHeapArray();minHeap.insert(4);minHeap.displayHeapArray();minHeap.insert(3);minHeap.displayHeapArray();minHeap.removeMin();minHeap.displayHeapArray();minHeap.removeMin();minHeap.displayHeapArray();minHeap.removeMin();minHeap.displayHeapArray();minHeap.removeMin();minHeap.displayHeapArray();minHeap.removeMin();try {minHeap.displayHeapArray();} catch(std::string e) {cout << e << endl;}return 0;}
- Binary Heap
- Binary Heap
- Binary heap
- binary heap(array based)
- Binary Heap(二叉堆)
- 二叉堆-Binary heap
- 2.Binary Heap[数据结构]
- 二叉堆(Binary Heap)
- 二叉堆(Binary heap)
- C++实现最小堆(binary heap)
- POJ 1785 Binary Search Heap Construction
- POJ 1785 Binary Search Heap Construction(RMQ)
- 堆:Binary Heap, Fibonacci Heaps资料+总结
- POJ 1785 Binary Search Heap Construction [treap]
- POJ 1875 Binary Search Heap Construction
- STL 简单 binary heap 的实现
- 数据结构之Binary Heap(二叉堆)
- POJ 1785 : Binary Search Heap Construction
- linux WIFI P2P
- Android推送框架+xmpp
- Android入门第七篇之ListView (二)
- 面试常考题-存储过程实现自增长
- Bluetooth Sniff 详解
- Binary Heap
- python学习笔记三
- C,C++中使用可变参数
- RedHat9.0中安装qt4.5
- Emacs+auto-complete+clang进行代码补全
- DB2自定义函数(C语言)实战
- IOS开发之所有类型的UIKeyboardType图片展示
- C/C++ 在控制台下显示进度
- 使用BlueZ Interface修改ACL连接Supervision timeout