本段代码实现了建堆,维护最大堆的性质,堆排序函数,优先队列的相关函数(插入,找最大值,提取出最大值,增加关键值,增加元素),以及相关的测试
#include <iostream>
#include <memory>
#include <iomanip>
#define LEFT(i) (2 * i)
#define RIGHT(i) (2*i + 1)
#define PARENT(i) (i >> 1)
using namespace std;
template< class T >
class myHeap
{
public:
int heapSize;
int maxSize;
T *data;//[heapSize];// = new T[heapSize];
myHeap( T a[], int inputSize, int inputMaxSize )
{
heapSize = inputSize;
maxSize = inputMaxSize;
data = new T[maxSize + 1];
if( inputMaxSize < inputSize )
{
cout << "error! input size is bigger than maxSize!" << endl;
exit( 0 );
}
memcpy( &(data[1]), a, inputSize * sizeof( T ) );
//debug
for ( int i = 1; i <= inputSize; i ++ )
{
cout << setw(3) << data[i] << " ";
}
for ( int i = inputSize / 2; i >= 1; i -- )
{
max_heapify( i );
}
cout << endl;
for ( int i = 1; i <= inputSize; i ++ )
{
cout << setw(3) << data[i] << " ";
}
}
void max_heapify( int i )
{
int l = LEFT(i);
int r = RIGHT(i);
int largest = i;
if( l <= heapSize && data[l] > data[i] )
largest = l;
if( r <= heapSize && data[r] > data[largest] )
largest = r;
if ( largest != i )
{
T tem = data[i];
data[i] = data[largest];
data[largest] = tem;
max_heapify( largest );
}
}
//void buildMaxHeap( T a[], int inputLength, int maxSize );
void heap_insert( T x );
T heap_maximum();
T heap_extract_max();
void heap_increase_key( int i, T key );
void heap_output();// for debug
};
template<class T>
T myHeap<T>::heap_maximum()
{
return data[1];
}
template<class T>
T myHeap<T>::heap_extract_max()
{
if( heapSize < 1 )
{
cout << "heap underflow!" << endl;
exit(1);
}
T max = data[1];
data[1] = data[heapSize];
heapSize --;
max_heapify( *this, 1 );
}
template<class T>
void myHeap<T>::heap_increase_key( int i, T key )
{
if ( key < data[i] )
{
cerr << "new key is smaller than current key!\n";
//exit(1);
return;
}
data[i] = key;
while ( i >1 && key > data[ PARENT(i) ] )
{
data[i] = data[ PARENT(i) ];
i = PARENT(i);
}
data[i] = key;
}
template<class T>
void myHeap<T>::heap_insert( T x )
{
heapSize ++;
data[heapSize] = x - 3;
heap_increase_key( heapSize, x );
}
template<class T>
void myHeap<T>::heap_output()
{
for ( int i = 1; i <= heapSize; i ++ )
{
cout << setw(3) << data[i] << " ";
if( i % 20 == 0 )
cout << endl;
}
cout << endl;
}
template<typename T>
myHeap<T> buildMaxHeap( T a[], int inputSize, int inputMaxSize )
{
myHeap<T> heapRet( a[], inputSize, inputMaxSize );
return heapRet;
}
template< typename T >
void heapSort( T a[], int inputSize )
{
myHeap<T> heapTem( a, inputSize, inputSize + 3 );
for ( int i = heapTem.heapSize; i >= 2; i -- )
{
T tem;
tem = heapTem.data[1];
heapTem.data[1] = heapTem.data[heapTem.heapSize];
heapTem.data[heapTem.heapSize] = tem;
heapTem.heapSize --;
heapTem.max_heapify( 1 );
}
cout << endl;
for ( int i = 1; i <= inputSize; i ++ )
{
cout << setw(3)<< heapTem.data[i] << " ";
if( i%10 == 0 )
cout << endl;
}
}
int main()
{
int a[] = { 1, 2, 4, 5, 66, 4, 78, 12, 14, 199, 278, 986,56 };
myHeap<int> heap1( a, 13, 20 );
cout << "\nheap1 Max is: " << heap1.heap_maximum() << endl;
heap1.heap_increase_key( 10, 25 );
heap1.heap_output();
heap1.heap_insert( 999 );
heap1.heap_output();
heap1.heap_extract_max();
heap1.heap_output();
heapSort( a, 13 );
return 0;
}