The author recently realized the smallest heap class and its derived priority queue, special code to serve, the shortcomings also please point out!
In the implementation of the priority queue, the author said that Meng Meng did not use the template to write derived classes, the result is written error: * * is not decleared in this scope. Later various fill up this-> just finished, in Csdn (author's post address ? http://bbs.csdn.net/topics/391806995) on the question after only know is the template parameter dependence, the author said the rise posture.
/** * the minimum heap class and heap sort in c++ * Thanks to Introduction to Algorithms (CLRS) Chapter 6 * thanks to tsinghua mooc of "Data structure and algorithms" * Author: Zheng Chen / Arclabs001 * Email : [email Protected] * copyright 2015 xi ' an university of posts & telecommunications. all rights reserved. */#include <iostream> #include <vector> #include <cstdlib> #include <ctime> #define inf 0xfffffffusing namespace std;template<class T>class Min_Heap{private: vector<t> a; int _size; //the size of Heap int parent (Int i) { return (i-1)/2; } //get the index of ith parent int left (int i) { return 2*i+1; } //get the index of ith left child int right (int i) { return 2*i+2; } //get the index of ith right childpublic: //here are three different constructor min_heap () {A.clear (); A.reserve ( ); _size=0;} min_heap (Vector<t> _array) { _size = 0; a.clear (); A.reserve (_array.size ()); for (int i=0; i<_ Array.size (); i++) { a.insert (A.end (), _array[i]); _size++; } for (int i= (_size-1)/2; i>=0; i--) { min_heapify (i); } } min_heap (T* _ Array, int array_size) { _size = 0; a.clear (); a.reserve (array_size*2); for (int i=0; i<_size; i++) { a.insert (A.end (), _array[i]); _size++; } for (int i= (_size-1)/2; i>=0; i--) { min_heapify (i); } } void min_heapify (int i) { int smallest; int l = left (i); int r = right (i); if (L<_size && a[l]<a[i]) smallest = l; else smallest = i; if (r<_ Size && a[r]<a[smallest]) smallest = r; if (smallest != i) { swap (A[i],a[smallest]); min_heapify (smallest); } } //The Heap Sort function //final status : the array a ' s element in desending order. void sort () { for (int i=_size-1; i>0; i--) { &Nbsp; swap (A[0],a[i]); --_size; min_ Heapify (0); } } t & pop () { --_size; T *tmp = new T; *tmp = a[0]; swap (A[0],A[_size]); min_heapify (0); Return *tmp; } void push (Const T &key) { a.insert (A.end (), INF); _size++; int i = _size-1; while (I>0 && key < a[parent (i)]) { a [I] = a[parent (i)]; i = parent (i); } a[i] = key; } void _delete (Int i) //delete the ith element { swap (a[i],a[_size-1]); --_size; a.erase (A.begin () +_size); Min_heapify (i); &nbSp; } bool decrease_key (Int i, const t &key) { if (Key > a[i]) { return false; } while (I>0 && key < a[parent (i)]) { a[i] = a[parent (i)]; i = parent (i); } A[i] = key; return true; } void showheap() { for (int i=0; i<_size; i++) { cout<<A[i]<< " "; } cout<<endl; } void showall () { for (int i=0; i<a.size (); i++) { cout<<A[i]<< " "; } cout<<endl; }};int main () { vector<int> a; A.clear (); a.reserve ( srand); (unsigned int) time (0)); for (int i=0; i<10; i++) a.insert (A.end (), rand ()%1000); min_heap<int> heap (A); heap.showheap (); heap.decrease_key (5,0); heap.showheap (); heap.sort (); heap.showall (); heap._delete (3); Heap.showall (); return 0;}
This is the priority queue:
/** * the priority queue class in c++ * thanks to introduction to algorithms (CLRS) chapter 6 * author: zheng chen / arclabs001 * email : [email protected] * copyright 2015 xi ' an university of posts & telecommunications. all rights reserved. */#include "myheap.h" #define inf 0xfffffffusing namespace std;template <class T>class Priority_Queue: public Min_Heap<T>{public: priority_queue (): min_heap<t> () {} priority_queue (vector <t> _array):min_heap<t> (_array) {} priority_queue (T *_array, int array_size): min_heap<t> (_array, array_size) {} t & minimum () {RETURN&NBSP;THIS->A[0];} t& pop () { --this->_size; T *tmp = new T; *tmp = this->A[0]; swap (This->a[0],this->a[this->_size]); this->min_heapify (0); return *tmp; } bool decrease_key (Int i, const t &key) { if (Key > this->a[i]) { return false; } this->a[i] = key; while (i>0 && this->a[i]<this->a[this->parent (i)]) { swap (this->A[i],this->A[ This->parent (i)]); i = This->parent (i); } return true; } void push (const T & Key) { this->a.insert (This->A.end ( ), INF); this->_size++; int i = this->_size-1; while (i >0 &&&nbSp;key < this->a[this->parent (i)]) { this->a[i] = this->a[this- >parent (i)]; i = this-> Parent (i); } This->a[i] = key; }};int main () { vector<int > a; a.clear (); a.reserve (; ) Srand ((unsigned int) time (0)); for (int i=0; i<10; i++) a.insert (A.end (), rand ()%1000); priority_queue <INT>&NBSP;PQ (A); pq.show (); pq.push (rand ()%1000); pq.show (); pq.pop (); pq.show (); pq.decrease_key ( 5,0); pq.show (); return 0;}
Beginner algorithm-Implementation of priority queue C + + based on minimum heap