Deleting the max-heap

Three variables, leftchild, rightchild, and root, are initialized as follows:

leftchild=0
rightchild=0
root=1

The following steps are performed to delete a max-heap:

  1. The element at the root node is temporarily assigned to the n variable.
  2. The last element of the heap is placed at the root node.
  3. If the value of the last index location is 1 or 2, that is, if the heap has only  1 or 2 elements left, then return to the caller with the n variable.
  4. Since the last element is placed at the root node, reduce the size of the heap by 1.
  5. To maintain the max-heap property, repeat steps 6 through 9 while rightchild <=last. Recall, the property of the max-heap is that the value of the parent node should be always larger than its children node.
  6. Calculate the leftchild and rightchild locations.
  7. If heap[root] > heap[leftchild] && heap[root] > heap[rightchild], return n and exit.
  8. If the value of the left child is greater than the value of the right child, then interchange the value of the root and that of the left child. The root will come down at the left child to check whether the max-heap property is maintained or not.
  9. If the value of the right child is greater than the value of the left child, then interchange the value of the root and that of the right child. The root will come down at the right child to check whether the max-heap property is maintained or not.
  10. When all the elements of the max-heap are over, that means the arr array will have all the sorted elements. So, the final step is to print the arr array, which contains the sorted elements.

The program for sorting elements of an integer array using the heap sort technique is as follows:

//heapsort.c

# include <stdio.h>
#define max 20
int heap[max], len;

void insheap(int h);
int delsheap(int j);

int main() {
int arr[max], numb, i, j;
printf("How many elements to sort? ");
scanf("%d", & len);
printf("Enter %d values \n", len);
for (i = 0; i < len; i++) {
scanf("%d", & numb);
insheap(numb);
}
j = len - 1;
for (i = 0; i < len; i++) {
arr[i] = delsheap(j);
j--;
}
printf("\nThe Descending order is: \n");
for (i = 0; i < len; i++)
printf("%d\n", arr[i]);
return 0;
}

void insheap(int value) {
static int x;
int par, cur, temp;
if (x == 0) {
heap[x] = value;\
x++;
} else {
heap[x] = value;
par = (x - 1) / 2;
cur = x;
do {
if (heap[cur] > heap[par]) {
temp = heap[cur];
heap[cur] = heap[par];
heap[par] = temp;
cur = par;
par = (cur - 1) / 2;
} else break;
} while (cur != 0);
x++;
}
}

int delsheap(int j) {
int loc, n = 0, pos, lc = 0, rc = 0, temp = 0;
loc = j;
pos = 0;
n = heap[pos];
heap[pos] = heap[loc];
if (loc == 0 || loc == 1) return (n);
loc--;
lc = 2 * pos + 1;
rc = 2 * pos + 2;
while (rc <= loc) {
if ((heap[pos] > heap[lc] && heap[pos] > heap[rc]))
return (n);
else {
if (heap[lc] > heap[rc]) {
temp = heap[lc];
heap[lc] = heap[pos];
heap[pos] = temp;
pos = lc;
} else {
temp = heap[rc];
heap[rc] = heap[pos];
heap[pos] = temp;
pos = rc;
}
lc = 2 * pos + 1;
rc = 2 * pos + 2;
}
}
if (lc == loc) {
if (heap[pos] < heap[lc]) {
temp = heap[pos];
heap[pos] = heap[lc];
heap[lc] = temp;
pos = lc;
}
}
return (n);
}

Now, let's go behind the scenes to understand the code better.