堆(英語:heap)是計算機科學中一類特殊的數據結構的統稱。堆通常是一個可以被看做一棵樹的數組對象。
基本介紹
- 中文名:堆
- 外文名:heap
- 定義:一類特殊的數據結構的統稱
- 物理特性:一棵完全二叉樹
釋義
- 堆中某個節點的值總是不大於或不小於其父節點的值;
- 堆總是一棵完全二叉樹。
支持操作
- build:建立一個空堆;
- insert:向堆中插入一個新元素;
- update:將新元素提升使其符合堆的性質;
- get:獲取當前堆頂元素的值;
- delete:刪除堆頂元素;
- heapify:使刪除堆頂元素的堆再次成為堆。
算法思想
篩選法
建堆效率
操作實現
代碼實現
#pragma oncetemplate<class T>class JBMinHeap{private: //申請堆空間 T *_minHeap = NULL; int _index,_maxSize;public: JBMinHeap(int maxSize) { _maxSize = maxSize; _minHeap = new T[_maxSize]; _index = -1; } JBMinHeap(JBMinHeap &h) { _index = h._index; _maxSize = h._maxSize; _minHeap = new T[_maxSize]; for (int i = 0;i<_maxSize) { *_minHeap[i] = *h._minHeap[i]; } } ~JBMinHeap() { delete[]_minHeap; } //獲取整個最小堆的頭部指針 T * getMinHeap() { return _minHeap; } //判斷堆是不是空的 bool isEmpty() { return _index == -1; } bool add(T x) { if (isFull()) { return false; } _index++; _minHeap[_index] = x; return true; } bool isFull() { return _index == _maxSize; } //堆進行向下調整 void adjustDown(int index); //隊進行向上調整 void adjustUp(int index); //建堆運算 void createMinHeap() { if (isEmpty()) { return; } for (int i = (_index-1)/2;i >-1;i--) {//直接從倒數第二層 逐層向下調整 adjustDown(i); } }};template<class T>void JBMinHeap<T>::adjustDown(int index) { if (isEmpty()) { return; } while (index<_index) { T temp = _minHeap[index];//將當前索引的位置的值保存下來 int oneC = 2 * index + 1;//獲取到兩個孩子的位置 int twoC = 2 * index + 2; if (oneC == _index) {//若第一個孩子是整個堆最後一個位置 則直接執行交換操作並結束執行 _minHeap[index] = _minHeap[oneC]; _minHeap[oneC] = temp; return; } if (twoC >_index) {//如果第二個孩子的索引位置越界 結束執行 return; } if (_minHeap[oneC] <= _minHeap[twoC]) {//正常情況的數據互動執行 if (temp > _minHeap[oneC]) { _minHeap[index] = _minHeap[oneC]; _minHeap[oneC] = temp; index = oneC; } else {//如果該處索引值已經是比兩個孩子小 則結束循環 index = _index; } } else { if (temp > _minHeap[twoC]) { _minHeap[index] = _minHeap[twoC]; _minHeap[twoC] = temp; index = twoC; } else { index = _index; } } }}template<class T>void JBMinHeap<T>::adjustUp(int index) { if (index > _index) {//大於堆的最大值直接return return; } while (index>-1) { T temp = _minHeap[index]; int father = (index - 1) / 2; if (father >= 0) {//如果索引沒有出界就執行想要的操作 if (temp < _minHeap[father]) { _minHeap[index] = _minHeap[father]; _minHeap[father] = temp; index=father; } else {//若果已經是比父親大 則直接結束循環 index = -1; } } else//出界就結束循環 { index = -1; } }}
Public Class Priority_QueueFriend Element() As IntegerFriend point As Integer ReadOnly Property Top As IntegerGetTryReturn Element(1)Catch ex As ExceptionReturn 0End TryEnd GetEnd PropertyReadOnly Property Size As IntegerGetReturn pointEnd GetEnd PropertyReadOnly Property Empty As BooleanGetIf point = 0 Then Return TrueReturn FalseEnd GetEnd Property Sub push(ByVal _ele_val As Integer)point += 1Element(point) = _ele_valDim Now As Integer = pointDim Nxt As Integer = Now / 2While (Now <> 1 AndAlso Element(Now) > Element(Nxt))Swap(Element(Now), Element(Nxt))Now = NxtNxt /= 2End WhileEnd SubSub pop()Dim now As Integer = 1 Element(1) = Element(point)point -= 1 While (1) Dim left As Integer = 0Dim right As Integer = 0 If (now * 2 <= point) Thenleft = Element(now * 2)End IfIf (now * 2 + 1 <= point) Thenright = Element(now * 2 + 1)End If If (right = left And right = 0) ThenExit SubEnd If If (Element(now) < left OrElse Element(now) < right) ThenIf left > right ThenSwap(Element(now), Element(now * 2))now *= 2ElseSwap(Element(now), Element(now * 2 + 1))now *= 2now += 1End IfElseExit WhileEnd IfEnd While End Sub Sub New(ByVal _size As Integer)ReDim Element(_size)point = 0End SubProtected Overrides Sub Finalize()MyBase.Finalize()End Sub End Class