c++: Max Heap Template
https://en.wikipedia.org/wiki/Min-max_heap My simple implementation of max heap. Change the compare from > to < in below codes, you will get a min heap. template<typename T> class MaxHeap { public: MaxHeap() { size_ = 0; capacity_ = 2; heap_ = new T[capacity_]; } MaxHeap(int capacity) { if (capacity >= 2) { heap_ = new T[capacity]; capacity_ = capacity; size_ = 0; } else { size_ = 0; capacity_ = 2; heap_ = new T[capacity_]; } } ~MaxHeap() { if (heap_) delete[] heap_; } public: int size() { return size_; } void insert(const T& t) { if ((size_+1) >= capacity_) { capacity_ *= 2; T* tmp = new T[capacity_]; for (int i = 0; i <= size_; i++) tmp[i] = heap_[i]; delete[] heap_; heap_ = tmp; } size_++; heap_[size_] = t; int i = size_; int tmp = heap_[i]; while (i > 1 && tmp > heap_[i / 2]) { heap_[i] = heap_[i / 2]; heap_[i / 2] = tmp; tmp = heap_[i / 2]; i /= 2;