//最大堆实现的最大优先级队列
#include <iostream>
#include <cstdlib>
using namespace std;
int array_size;
int heap_size;
//几个内联函数
inline int left(int i)
{
return i<<1;
}
inline int right(int i)
{
return i<<1|1;
}
inline int parent(int i)
{
return i>>1;
}
//最大化堆
void max_heapify(int *heap,int pos)
{
int l = left(pos);
int r = right(pos);
int largest;
if(l <= heap_size && heap[l] > heap[pos])
largest = l;
else largest = pos;
if(r <= heap_size && heap[r] > heap[largest])
largest = r;
if(largest != pos)
{
int t = heap[largest];
heap[largest] = heap[pos];
heap[pos] = t;
max_heapify(heap, largest);
}
}
//建堆
void build_max_heap(int *heap)
{
heap_size = array_size;
for(int i = array_size/2; i >= 1; i--)
max_heapify(heap, i);
}
//堆排序
void heap_sort(int *heap)
{
build_max_heap(heap);
for(int i = array_size; i >= 2; i--)
{
int t = heap[1];
heap[1] = heap[heap_size];
heap[heap_size] = t;
heap_size--;
max_heapify(heap, 1);
}
}
//取出最大元素
int heap_extract_max(int *heap)
{
if(heap_size < 1)
{
cout<<"heap is empty!"<<endl;
exit(1);//异常退出
}
int max = heap[1];
heap[1] = heap[heap_size];
heap_size--;
max_heapify(heap,1);
return max;
}
//增大指定元素的大小
void heap_increase_key(int *heap, int pos, int key)
{
if(key < heap[pos])
{
cout<<"new key is smaller than current key!"<<endl;
exit(1);
}
heap[pos] = key;
while(pos > 1 && heap[parent(pos)] < heap[pos])//当双亲大于孩子时结束
{
int t = heap[parent(pos)];
heap[parent(pos)] = heap[pos];
heap[pos] = t;
pos = parent(pos);//一直往上寻找
}
}
//插入操作
//这里涉及到一种思想,可以使插入操作变得很方便
//思想是这样的:首先插入在堆的末尾插入一个最小值,然后再利用上面的增加元素的值得形式将节点上流
//这样大大节省了时间
void max_heap_insert(int *heap, int key)
{
heap[++heap_size] = -INT_MAX;
heap_increase_key(heap, heap_size, key);
}
int main()
{
int n;//堆元素个数
cout<<"please enter:"<<endl;
while(cin >> n)
{
array_size = n;
int i,j;
int heap[20] = { 0 };
cout<<"please enter heap's element:"<<endl;
for(i = 1; i <= n; i++)
cin>>heap[i];
//heap_sort(heap);
//建堆
build_max_heap(heap);
//取出最大元素
cout<<"max element:"<<heap_extract_max(heap)<<endl;
//插入一个元素
int insert_element;
cout<<"please enter the element to insert:"<<endl;
cin>>insert_element;
max_heap_insert(heap, insert_element);
//增大指定元素的大小
int increase_element;
cout<<"please enter the element to increase:"<<endl;
cin>>increase_element;
heap_increase_key(heap, 2, increase_element);
for(i = 1; i <= heap_size; i++)
cout<<heap[i]<<" ";
cout<<endl;
cout<<endl<<"please enter:"<<endl;
}
return 0;
}
//利用最小堆实现最小优先级队列
#include <iostream>
#include <cstdlib>
using namespace std;
int heap_size;
int array_size;
//几个内联函数
inline int left(int i)
{
return i<<1;
}
inline int right(int i)
{
return i<<1|1;
}
inline int parent(int i)
{
return i>>1;
}
//最小化堆
void min_heapify(int *heap, int pos)
{
int l = left(pos);
int r = right(pos);
int smallest;
if(l <= heap_size && heap[l] < heap[pos])
smallest = l;
else smallest = pos;
if(r <= heap_size && heap[r] < heap[smallest])
smallest = r;
if(smallest != pos)
{
int t = heap[pos];
heap[pos] = heap[smallest];
heap[smallest] = t;
min_heapify(heap, smallest);
}
}
//建堆
void build_min_heap(int *heap)
{
heap_size = array_size;
for(int i = array_size/2; i >= 1; i--)
min_heapify(heap, i);
}
//取出最小元素
int heap_extract_min(int *heap)
{
int min = heap[1];
int t = heap[1];
heap[1] = heap[heap_size];
heap[heap_size] = t;
heap_size--;
min_heapify(heap, 1);
return min;
}
//减小某元素的值
void heap_decrease_key(int *heap, int pos, int key)
{
if(key > heap[pos])
{
cout<<"the element is bigger than current key!"<<endl;
exit(1);
}
heap[pos] = key;
while(pos >= 1 && heap[pos] < heap[parent(pos)])
{
int t = heap[pos];
heap[pos] = heap[parent(pos)];
heap[parent(pos)] = t;
pos = parent(pos);
}
}
//插入元素
//插入的思想与最大优先级队列相似,首先在堆尾插入一个最大的元素
void min_heap_insert(int *heap, int key)
{
heap_size++;
heap[heap_size] = INT_MAX;
heap_decrease_key(heap, heap_size, key);
}
int main()
{
int n;
cout<<"please enter:"<<endl;
while(cin >> n)
{
array_size = n;
int heap[30];
int i, j;
cout<<"please heap's elements:"<<endl;
for(i = 1; i <= n; i++)
cin>>heap[i];
//建堆
build_min_heap(heap);
//取出最小元素
cout<<"minimum: "<<heap_extract_min(heap)<<endl;
//插入一个元素
int insert_element;
cout<<"please enter the element to insert:"<<endl;
cin>>insert_element;
min_heap_insert(heap, insert_element);
for(i = 1; i <= heap_size; i++)
cout<<heap[i]<<" ";
cout<<endl;
cout<<"please enter:"<<endl;
}
return 0;
}