int main(int argc, char* argv[]) { int dummy; Heap<int> hp; hp.Insert(10).Insert(6).Insert(20).Insert(30).Insert(25).Insert(3).Insert(8); cout << "Heap elements: " << hp << endl; hp.PrintTreeVertically(cout, 64); cout << endl << "Heap elements on delete: "; while (!hp.IsEmpty()) { hp.DeleteMin(dummy); cout << dummy << ", "; } cout << endl; // Heap sort // In this mechanism/implementation, note that: // Descending sort by using Min-Heap, while ascending sort should use Max-Heap int a[] = { 1, 3, 4, 2, 5, 9, 3, 6, 7, 9, 4, 8 }; #define ELEM_COUNT(a) (sizeof(a) / sizeof(a[0])) Heap<int>::Sort(a, ELEM_COUNT(a)); cout << "Sort array descending: "; for (int i = 0; i < ELEM_COUNT(a); i++) { cout << a[i] << ", "; } cout << endl; return 0; }
void CreateHuffmanTree(const T* array, size_t size, const T& invalid) { struct Compare { bool operator()(HuffmanNode_P<T>*& lhs, HuffmanNode_P<T>*& rhs) { return lhs->_weight < rhs->_weight; } }; // 1.将所有值构建为节点放入到一个最小堆中,Compare仿函数做比较器 Heap<HuffmanNode_P<T>*, Compare> minHeap; for (int i = 0; i < size; ++i) { if (array[i] != invalid) { HuffmanNode_P<T>* node = new HuffmanNode_P<T>(array[i]); minHeap.Insert(node); } } if (minHeap.Empty()) return; // 2.获取出最小和次小的节点做孩子节点,并构建这两个孩子节点的父节点进行链接。 HuffmanNode_P<T>* parent = minHeap.GetHeapTop(); while (minHeap.Size() > 1) { HuffmanNode_P<T>* first = minHeap.GetHeapTop(); minHeap.Remove(); HuffmanNode_P<T>* second = minHeap.GetHeapTop(); minHeap.Remove(); parent = new HuffmanNode_P<T>(first->_weight + second->_weight); parent->_left = first; parent->_right = second; first->_parent = parent; second->_parent = parent; minHeap.Insert(parent); } _root = parent; }
void CreateHuffmanTree(const T* array, size_t size) { struct Compare { bool operator()(const HuffmanNode_A<T>* lhs, const HuffmanNode_A<T>* rhs) { return lhs->_weight < rhs->_weight; } }; // 将节点的指针放入最小堆中,重写比较器 _vNodes.reserve(size*2 - 1); Heap<HuffmanNode_A<T>*, Compare> minHeap; size_t index = 0; for (; index < size; ++index) { _vNodes.push_back(HuffmanNode_A<T>(array[index])); HuffmanNode_A<T>& node = _vNodes.back(); node._index = index; minHeap.Insert(&node); } while (minHeap.Size() > 1) { HuffmanNode_A<T>* first = minHeap.GetHeapTop(); minHeap.Remove(); HuffmanNode_A<T>* second = minHeap.GetHeapTop(); minHeap.Remove(); _vNodes.push_back(HuffmanNode_A<T>(first->_weight + second->_weight)); HuffmanNode_A<T>& parent = _vNodes.back(); parent._index = index++; minHeap.Insert(&parent); first->_parent = parent._index; second->_parent = parent._index; parent._left = first->_index; parent._right = second->_index; } _rootIndex = minHeap.GetHeapTop()->_index; }
void insert(T data){ if(MaxHeap->size()==0){ MaxHeap->Insert(data); return; } T maxHeap = MaxHeap->pop(); T minHeap = MinHeap->pop(); if(data < maxHeap){ MaxHeap->Delete(); MaxHeap->Insert(data); swap( maxHeap, data); } if(MinHeap->size()==0){ MinHeap->Insert(data); return; } if(data > minHeap){ MinHeap->Delete(); MinHeap->Insert(data); swap(data,minHeap); } if(MaxHeap->size() > MinHeap->size()) MinHeap->Insert(data); else MaxHeap->Insert(data); }
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) { if (startFuel >= target) return 0; Heap bfs_q; bfs_q.Insert(Stop{-1, startFuel}); auto n = 0; Heap q; unordered_map<int, bool> is_added; while (!bfs_q.Empty()) { auto s = bfs_q.Pop(); for(auto i = s.index+1; i < stations.size(); ++i) { if (s.fuel >= stations[i][0]) { if (is_added.find(i) != is_added.end()) { //continue break; } is_added[i] = true; auto p = Stop{i, stations[i][1] + s.fuel}; if (p.fuel >= target) return n+1; q.Insert(p); } else { break; } } if (bfs_q.Empty() && !q.Empty()) { bfs_q = q; n++; q.Clear(); is_added.clear(); } } return -1; }
int main() { Heap h; vector<Stop*> s; for (auto i = 0; i < 10; ++i) { auto t = new Stop{i+1, i+1}; s.push_back(t); h.Insert(t); } h.Print(); vector<Stop*> r; h.HeapSort(r); for (auto i : r) { cout << i->index << endl; } return 0; }
// Function implementing algorithm to find median so far. int getMedian(int e, int &m, Heap &l, Heap &r) { // Are heaps balanced? If yes, sig will be 0 int sig = Signum(l.GetCount(), r.GetCount()); switch(sig) { case 1: // There are more elements in left (max) heap if( e < m ) // current element fits in left (max) heap { // Remore top element from left heap and // insert into right heap r.Insert(l.ExtractTop()); // current element fits in left (max) heap l.Insert(e); } else { // current element fits in right (min) heap r.Insert(e); } // Both heaps are balanced m = Average(l.GetTop(), r.GetTop()); break; case 0: // The left and right heaps contain same number of elements if( e < m ) // current element fits in left (max) heap { l.Insert(e); m = l.GetTop(); } else { // current element fits in right (min) heap r.Insert(e); m = r.GetTop(); } break; case -1: // There are more elements in right (min) heap if( e < m ) // current element fits in left (max) heap { l.Insert(e); } else { // Remove top element from right heap and // insert into left heap l.Insert(r.ExtractTop()); // current element fits in right (min) heap r.Insert(e); } // Both heaps are balanced m = Average(l.GetTop(), r.GetTop()); break; } // No need to return, m already updated return m; }