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
原创粉丝点击