The data structure that we expect is capable of supporting the insert operation and being able to easily remove records with the minimum or maximum key, which is the priority queue. In the various implementations of priority queues, the heap is the most efficient kind of data structure.
minimum heap: The key code for any node is less than or equal to the key for its left and right children, and the key to the node at the top of the heap is the smallest of the entire set of elements, so call it the smallest heap. Maximum heap similar definition.
To create a heap : to create a heap by progressively adjusting from the bottom up to form a heap. Call down algorithm siftdown for the following branch nodes to adjust the minimum heap with their root subtree. From the local to the whole, the smallest heap is scaled up until the entire tree is adjusted to the minimum heap.
Inserts an element: the minimum heap insertion algorithm calls another heap adjustment method Siftup to achieve bottom-up adjustment. Since each new node is always inserted behind the smallest heap that has been built, it is necessary to follow the comparison path opposite to the sift, from the bottom up, to compare with the key code of the parent node and swap.
deletes an element: the heap top element of the smallest heap is removed from the minimum heap when an operation with a minimum key record is deleted. That is, the No. 0 element in the order of its complete binary tree is deleted, and after taking this element, it usually fills the top element of the pile with the last node. and reduce the actual number of elements in the heap by 1. But replacing the heap top element with the last element will destroy the heap, requiring the siftdown algorithm to be invoked to adjust the heap.
The code in this article takes the implementation of the minimum heap as an example.
Copy Code code as follows:
#include <iostream>
#include <assert.h>
UsingNamespace std;
Constint maxheapsize=100;
Staticint currentsize=0;
Adjust heap from top to bottom
void Siftdown (int* heap,int currentpos,int m)
{
int i=currentpos;
int j=currentpos*2+1;//i ' s leftchild
int temp=heap[i];
while (J<=M)
{
if (J<m&&heap[j]>heap[j+1]) j++;//J points to Minchild
if (Temp<=heap[j]) break;
Else
{
HEAP[I]=HEAP[J];
I=j;
j=2*i+1;
}
}
Heap[i]=temp;
}
Adjust heap from bottom up
void Siftup (int* heap, int start)
{
int i=start,j= (I-1)/2;
int temp=heap[i];
while (i>0)
{
if (heap[j]>temp)
{
HEAP[I]=HEAP[J];
I=j;
j= (i-1)/2;
}
Elsebreak;
}
Heap[i]=temp;
}
//Build heap
int* Heap (int*arr, int size)
{
int i;
currentsize=size;
int* Heap =newint[maxheapsize];
assert (Heap!=null);
for (i=0;i<currentsize;i++) heap[i]=arr[i];
int currentpos= (currentsize-2)/2;
while (currentpos>=0)
{
Siftdown (heap,currentpos,currentsize-1);
currentpos--;
}
return heap;
}
Add an Element
void Insert (int* heap,int value)
{
if (currentsize>=maxheapsize)
{
cout<< "Heap is full!" <<endl;
return;
}
Heap[currentsize]=value;
Siftup (heap,currentsize);
currentsize++;
}
Deletes an element and returns the heap top element before it is deleted
int Removemin (int* heap)
{
ASSERT (currentsize>=0);
int removevalue=heap[0];
HEAP[0]=HEAP[CURRENTSIZE-1];
currentsize--;
Siftdown (heap,0,currentsize-1);
return removevalue;
}
int main ()
{
Constint size=10;
int arr[size]={2,1,3,0,8,1,6,9,7,10};
int* heap=heap (arr,size);
Heap Sort
for (int i=0;i<size;i++)
{
Arr[i]=removemin (heap);
cout<<arr[i]<<endl;
}
delete []heap;
Return0;
}