C++二叉堆的实现
来源:互联网 发布:yii2源码下载 编辑:程序博客网 时间:2024/06/15 02:45
头文件 BinaryHeap.h
#ifndef __BinaryHeap__#define __BinaryHeap__#include <iostream>class BinaryHeap{public:typedef struct node{int startIndex;int endIndex;int weight;} Node;private:Node * m_heap;int m_size;int m_currentSize;void m_percolateUp (int index);void m_percolateDown (int index);public:BinaryHeap (int size = 0);~BinaryHeap (void);bool isEmpty (void);bool isFull (void);bool insert (int startIndex, int endIndex, int weight);bool deleteMin (Node * const pNode);};#endif /* defined(__BinaryHeap__) */
实现文件 BinaryHeap.cpp
#include "BinaryHeap.h"//Private methods://---------------------void BinaryHeap ::m_percolateUp (int index){Node temp = m_heap[index];size_t i = index;size_t parent = (i - 1) / 2;while (i != 0){if (temp.weight < m_heap[parent].weight){m_heap[i] = m_heap[parent];i = parent;parent = (i - 1) / 2;}else{m_heap[i] = temp;break;}}if (0 == i){m_heap[0] = temp;}}//---------------------//---------------------void BinaryHeap ::m_percolateDown (int index){Node temp = m_heap[index];int i = index;int child = i * 2 + 1;while (child < m_currentSize){if (child != m_currentSize - 1 && m_heap[child + 1].weight < m_heap[child].weight)++child;if (temp.weight > m_heap[child].weight){m_heap[i] = m_heap[child];i = child;child = i * 2 + 1;}else{m_heap[i] = temp;break;}}if (child >= m_currentSize){m_heap[i] = temp;}}//---------------------//Public methods://---------------------BinaryHeap ::BinaryHeap (int size){if (size <= 0){std ::cerr << "Wrong size." << std ::endl;return;}m_heap = new Node[size];if (NULL == m_heap){std ::cerr << "Out of space." << std ::endl;return;}m_size = size;m_currentSize = 0;}//---------------------//---------------------BinaryHeap ::~BinaryHeap (void){delete []m_heap;}//---------------------//---------------------bool BinaryHeap ::isEmpty (void){if (0 == m_currentSize)return true;elsereturn false;}//---------------------//---------------------bool BinaryHeap ::isFull (void){if (m_currentSize == m_size)return true;elsereturn false;}//---------------------//---------------------bool BinaryHeap ::insert (int startIndex, int endIndex, int weight){if (isFull()){std ::cerr << "Heap is full already." << std ::endl;return false;}if (startIndex >= m_size || startIndex < 0 ||endIndex >= m_size || endIndex < 0){std ::cerr << "Wrong start index or end index." << std ::endl;return false;}m_heap[m_currentSize].startIndex = startIndex;m_heap[m_currentSize].endIndex = endIndex;m_heap[m_currentSize].weight = weight;m_percolateUp(m_currentSize);++m_currentSize; return true;}//---------------------//---------------------bool BinaryHeap ::deleteMin (Node * const pNode){if (isEmpty()){std ::cerr << "Heap is empty already." << std ::endl;return false;}*pNode = m_heap[0];m_heap[0] = m_heap[--m_currentSize];m_percolateDown(0);return true;}//---------------------
转自:http://blog.csdn.net/golden_shadow/article/details/6730500
参考:http://blog.csdn.net/listeningsea/article/details/7718484
http://blog.chinaunix.net/uid-20937170-id-3331263.html
http://en.wikipedia.org/wiki/Binary_heap
http://baike.baidu.com/link?url=325xU5U7MrGN46ESO3LqK7tJ-6mtB3EqAyyxHnyKo8vl8qX8xzTKWp73oqwTHHT5
http://blog.csdn.net/morewindows/article/details/6976468
0 0
- 二叉堆的C语言实现
- 二叉堆的实现C语言
- 二叉堆--C语言实现
- 二叉堆C语言实现
- 二叉堆--C语言实现
- 二叉堆的实现
- 二叉堆的实现
- 二叉堆的实现
- 二叉堆的实现
- 配合Dijkstra算法的二叉堆实现文件C语言
- 配合Kruskal算法的二叉堆实现文件C语言
- 二叉堆--二叉堆实现文件C语言
- C++二叉堆的实现
- 二叉树--堆的实现
- 优先队列二叉堆 C语言实现
- 优先队列的实现--二叉堆
- JAVA完全二叉堆的实现
- 二叉堆的c++模板类实现
- java List排序
- UITextfiled控件属性方法使用总结
- 支持向量机通俗导论(理解SVM的三层境界)
- 德国:这就是中国对克里米亚危机高兴的原因
- JBOSS EAP 6 系列二 Oracle数据源的配置—认识模块的使用
- C++二叉堆的实现
- 【爱上cocos2d-x之九】cocos2d-x坐标系与anchorPoint锚点
- Uva 10892 - LCM Cardinality 解题报告(因式分解)
- mfc LISTbox
- Java byte数组与十六进制字符串互转
- poj1861 Network
- F5公司负载均衡技术
- Core Animation之简单使用CALayer
- kettle使用手册