Heap is an array object that we can view as a nearly complete binary tree. The tree is completely filled on all levels except possibly the lowest, which is filled from left to a point. An array as a heap is an object with two attributes: A.length and A.heap-size. The first is number of elements in array, and the latter is elements in the heap are stored in array. A.heap-size<=A.length.
Index: The root of the tree is A[1], given index i of a node, we can
easily compute indices of its parent, left and right child. parent(i)
= i/2; left(i) = 2i; right(i) = 2i +1;
There are two kinds of binary heap: max heap and min heap, with a specific heap property. In a max heap, the max heap property is that for each node other than the root,
A[parent(i)] >= A[i].
To get the array of a heap, we can just write the heap level by level.
1. Maintain the heap property
This call is max-heapify, to let the value A[i] flow down to in the max heap to maintain the subtree rooted at index i the max property.
pseudo code:
Max-Heapify(A, i) {
l = left(i);
r = right(i);
if (l <= A.heap-size && A[l]>A[i])
largest = l;
else largest = i;
if (r <= A.heap-size && A[r]>A[largest])
largest = r;
if (largest != i)
swap(A[i], A[largest]);
Max-Heapify(A, largest)
}
running time complexity: O(h) = O(logn), which is actually a recursive procedure with max depth log n.
2. Build a heap
We can use the Max-Heapify procedure in a bottom-up manner to convert an array into a max heap.
pseudo code:
Build-Max_Heap(A) {
A.heap-size = A.length;
for i = A.length/2 downto 1
Max-Heapify(A, i);
}
loop invariant:
at the start of each iteration of the for loop, each node i+1, i+2, …, n is a root of max heap.
We can analyze the invariant from initialization, maintenance, and termination.
the running time complexity is O(n).
3. Heap sort algorithm
Just show the pseudo code like:
Heapsort(A) {
Build-Max-Heap(A);
for i = A.length downto 2,
exchange A[1] with A[i];
A.heap-size = A.heap-size - 1;
Max-Heapify(A, 1);
}
The running time of this sort algorithm is O(nlogn), because we call the Max-Heapify O(logn) for n-1 times.
C++ implementation:
headfile:
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Myheap {
public:
int heapsize;
int heaplength;
vector<int> data;
Myheap() {
data = {};
heapsize = 0;
heaplength = 0;
}
void heapify( int i);
void build_heap(vector<int>& s);
void myheapsort(vector<int>& s);
};
cpp with headfile:
#include "stdafx.h"
#include <vector>
#include "Myheap.h"
void Myheap::heapify( int i) {
vector<int>& s = data;
int l = (i + 1) * 2;
int r = (i + 1) * 2 + 1;
int largest;
if (l <= heapsize && s[l - 1] > s[i])
largest = l - 1;
else largest = i;
if (r <= heapsize && s[r - 1] > s[largest])
largest = r - 1;
if (largest != i) {
int temp = s[i];
s[i] = s[largest];
s[largest] = temp;
heapify(largest);
}
}
void Myheap::build_heap(vector<int>& s) {
heaplength = s.size();
heapsize = s.size();
data = s;
for (int i = heapsize / 2 - 1; i >= 0; i--) {
heapify(i);
}
}
void Myheap::myheapsort(vector<int>& s) {
build_heap(s);
for (int i = heapsize - 1; i > 0; i--) {
int temp = data[0];
data[0] = data[i];
data[i] = temp;
heapsize--;
heapify(0);
}
}
Then, you just need to include “myheap.h” in your main function.