123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #pragma once
- #include "Array.h"
- NS_BF_BEGIN;
- template <typename T>
- class BinaryMaxHeap : public Array<T>
- {
- private:
- void HeapifyUp(int32 childIdx)
- {
- if (childIdx > 0)
- {
- int32 parentIdx = (childIdx - 1) / 2;
- if (this->mVals[childIdx] > this->mVals[parentIdx])
- {
- // swap parent and child
- T t = this->mVals[parentIdx];
- this->mVals[parentIdx] = this->mVals[childIdx];
- this->mVals[childIdx] = t;
- HeapifyUp(parentIdx);
- }
- }
- }
- void HeapifyDown(int32 parentIdx)
- {
- int32 leftChildIdx = 2 * parentIdx + 1;
- int32 rightChildIdx = leftChildIdx + 1;
- int32 largestChildIdx = parentIdx;
- if (leftChildIdx < this->mSize && this->mVals[leftChildIdx] > this->Vals[largestChildIdx])
- {
- largestChildIdx = leftChildIdx;
- }
- if (rightChildIdx < this->mSize && this->mVals[rightChildIdx] > this->mVals[largestChildIdx])
- {
- largestChildIdx = rightChildIdx;
- }
- if (largestChildIdx != parentIdx)
- {
- T t = this->mVals[parentIdx];
- this->mVals[parentIdx] = this->mVals[largestChildIdx];
- this->mVals[largestChildIdx] = t;
- HeapifyDown(largestChildIdx);
- }
- }
- public:
- BinaryMaxHeap() : Array<T>()
- {
- }
- /// Add an item to the heap
- void Add(T item)
- {
- Array<T>::Add(item);
- HeapifyUp(this->mSize - 1);
- }
- /// Get the item of the root
- T Peek()
- {
- return this->mVals[0];
- }
- /// Extract the item of the root
- T Pop()
- {
- T item = this->mVals[0];
- this->mSize--;
- this->mVals[0] = this->mVals[this->mSize];
- HeapifyDown(0);
- return item;
- }
- };
- template <typename T>
- class BinaryMinHeap : public Array<T>
- {
- private:
- void HeapifyUp(int32 childIdx)
- {
- if (childIdx > 0)
- {
- int32 parentIdx = (childIdx - 1) / 2;
- if (this->mVals[childIdx] < this->mVals[parentIdx])
- {
- // swap parent and child
- T t = this->mVals[parentIdx];
- this->mVals[parentIdx] = this->mVals[childIdx];
- this->mVals[childIdx] = t;
- HeapifyUp(parentIdx);
- }
- }
- }
- void HeapifyDown(int32 parentIdx)
- {
- int32 leftChildIdx = 2 * parentIdx + 1;
- int32 rightChildIdx = leftChildIdx + 1;
- int32 largestChildIdx = parentIdx;
- if (leftChildIdx < this->mSize && this->mVals[leftChildIdx] < this->mVals[largestChildIdx])
- {
- largestChildIdx = leftChildIdx;
- }
- if (rightChildIdx < this->mSize && this->mVals[rightChildIdx] < this->mVals[largestChildIdx])
- {
- largestChildIdx = rightChildIdx;
- }
- if (largestChildIdx != parentIdx)
- {
- T t = this->mVals[parentIdx];
- this->mVals[parentIdx] = this->mVals[largestChildIdx];
- this->mVals[largestChildIdx] = t;
- HeapifyDown(largestChildIdx);
- }
- }
- public:
- BinaryMinHeap() : Array<T>()
- {
- }
- /// Add an item to the heap
- void Add(T item)
- {
- Array<T>::Add(item);
- HeapifyUp(this->mSize - 1);
- }
- /// Get the item of the root
- T Peek()
- {
- return this->mVals[0];
- }
- /// Extract the item of the root
- T Pop()
- {
- T item = this->mVals[0];
- this->mSize--;
- this->mVals[0] = this->mVals[this->mSize];
- HeapifyDown(0);
- return item;
- }
- };
- NS_BF_END
|