Properties of a Binary Min-Heap
1. Structure Property
A binary min-heap has to be a complete binary tree.
2. Heap Order Property
The value of any node in a binary min-heap has to be no greater than its two children's values(if exists).
Operations Included in the Code
In the following code, you will see the operations below:
- BuildHeap
- PercolateDown
- PercolateUp
- Insert
- DeleteMin
- PrintHeap
Also, you will see the simple typedef of:
- Heap
Notes and Declarations
The code will sort the input sequence with a heap.
The language used is C.
The purpose of posting this code online is to make a quick note of my studying progress.
For students at school, DO NOT copy this code into your homework or projects. Otherwise, I will not be responsible for any punishments because of this.
Some bugs might be included in the code. The author(I) will NOT be responsible for any results brought out from this code.
Code
/* Operations on a Binary Min Heap */
#include <stdio.h>
#include <stdlib.h>
#define MAXN_SIZE 128
typedef struct Heap
{
int Size;
int *Elements;
}Heap;
void PercolateDown( Heap *H, int index );
void PercolateUp( Heap *H, int index );
void Insert( Heap *H, int value );
int DeleteMin( Heap *H );
Heap BuildHeap( int size, int *seq );
void PrintHeap( Heap H );
int main()
{
printf("SORT BY HEAP\n");
printf("To build a heap, how many numbers do you want?\n>>>");
int size = 0;
int i = 0;
scanf("%d", &size);
int *seq = (int *)malloc(sizeof(int *) * size);
printf("Please input your numbers:\n>>>");
for (i = 0; i < size; i++)
{
scanf("%d", &seq[i]);
}
Heap heap = BuildHeap(size, seq);
printf("Heap built!\n");
while (heap.Size > 0)
{
//PrintHeap(heap);
printf("%d ", DeleteMin(&heap));
}
return 0;
}
void PercolateDown( Heap *H, int index )
{
int i = 0, LastElement = H->Elements[index];
int Child = 0;
for (i = index; i * 2 <= H->Size; i = Child)
{
Child = i * 2;
if (Child != H->Size && H->Elements[Child + 1] < H->Elements[Child])
{
// Right child exists and is smaller than the left one
Child += 1;
}
if (LastElement > H->Elements[Child])
{
// Copy the smaller child's value upwards
H->Elements[i] = H->Elements[Child];
}
else
{
// The iteration is enough
break;
}
}
H->Elements[i] = LastElement;
}
void PercolateUp( Heap *H, int index )
{
int targ_key = H->Elements[index];
int i = 0;
for (i = index; i > 1 && H->Elements[i / 2] > targ_key; i /= 2)
{
H->Elements[i] = H->Elements[i / 2];
}
H->Elements[i] = targ_key;
}
void Insert( Heap *H, int value )
{
H->Size += 1;
if (H->Size >= MAXN_SIZE)
{
printf("OVERSIZING OCCURRED. INSERTION FAILED.\n");
return;
}
H->Elements[H->Size] = value;
PercolateUp(H, H->Size);
}
int DeleteMin( Heap *H )
{
int minn = H->Elements[1];
H->Elements[1] = H->Elements[H->Size];
H->Size -= 1;
PercolateDown(H, 1);
return minn;
}
Heap BuildHeap( int size, int *seq )
{
Heap res;
int i = 0;
res.Size = size;
res.Elements = (int *)malloc(MAXN_SIZE * sizeof(int *));
for (i = 1; i <= size; i++)
{
res.Elements[i] = seq[i - 1];
}
for (i = res.Size / 2; i >= 1; i--)
{
PercolateDown(&res, i);
}
return res;
}
void PrintHeap( Heap H )
{
int i = 0, s = H.Size;
for (i = 1; i <= s; i++)
{
printf("%d ", H.Elements[i]);
}
printf("\n");
return;
}