1// Max-Heap data structure in C
2
3#include <stdio.h>
4int size = 0;
5void swap(int *a, int *b)
6{
7 int temp = *b;
8 *b = *a;
9 *a = temp;
10}
11void heapify(int array[], int size, int i)
12{
13 if (size == 1)
14 {
15 printf("Single element in the heap");
16 }
17 else
18 {
19 int largest = i;
20 int l = 2 * i + 1;
21 int r = 2 * i + 2;
22 if (l < size && array[l] > array[largest])
23 largest = l;
24 if (r < size && array[r] > array[largest])
25 largest = r;
26 if (largest != i)
27 {
28 swap(&array[i], &array[largest]);
29 heapify(array, size, largest);
30 }
31 }
32}
33void insert(int array[], int newNum)
34{
35 if (size == 0)
36 {
37 array[0] = newNum;
38 size += 1;
39 }
40 else
41 {
42 array[size] = newNum;
43 size += 1;
44 for (int i = size / 2 - 1; i >= 0; i--)
45 {
46 heapify(array, size, i);
47 }
48 }
49}
50void deleteRoot(int array[], int num)
51{
52 int i;
53 for (i = 0; i < size; i++)
54 {
55 if (num == array[i])
56 break;
57 }
58
59 swap(&array[i], &array[size - 1]);
60 size -= 1;
61 for (int i = size / 2 - 1; i >= 0; i--)
62 {
63 heapify(array, size, i);
64 }
65}
66void printArray(int array[], int size)
67{
68 for (int i = 0; i < size; ++i)
69 printf("%d ", array[i]);
70 printf("\n");
71}
72int main()
73{
74 int array[10];
75
76 insert(array, 3);
77 insert(array, 4);
78 insert(array, 9);
79 insert(array, 5);
80 insert(array, 2);
81
82 printf("Max-Heap array: ");
83 printArray(array, size);
84
85 deleteRoot(array, 4);
86
87 printf("After deleting an element: ");
88
89 printArray(array, size);
90}
1// Max-Heap data structure in C++
2
3#include <iostream>
4#include <vector>
5using namespace std;
6
7void swap(int *a, int *b)
8{
9 int temp = *b;
10 *b = *a;
11 *a = temp;
12}
13void heapify(vector<int> &hT, int i)
14{
15 int size = hT.size();
16 int largest = i;
17 int l = 2 * i + 1;
18 int r = 2 * i + 2;
19 if (l < size && hT[l] > hT[largest])
20 largest = l;
21 if (r < size && hT[r] > hT[largest])
22 largest = r;
23
24 if (largest != i)
25 {
26 swap(&hT[i], &hT[largest]);
27 heapify(hT, largest);
28 }
29}
30void insert(vector<int> &hT, int newNum)
31{
32 int size = hT.size();
33 if (size == 0)
34 {
35 hT.push_back(newNum);
36 }
37 else
38 {
39 hT.push_back(newNum);
40 for (int i = size / 2 - 1; i >= 0; i--)
41 {
42 heapify(hT, i);
43 }
44 }
45}
46void deleteNode(vector<int> &hT, int num)
47{
48 int size = hT.size();
49 int i;
50 for (i = 0; i < size; i++)
51 {
52 if (num == hT[i])
53 break;
54 }
55 swap(&hT[i], &hT[size - 1]);
56
57 hT.pop_back();
58 for (int i = size / 2 - 1; i >= 0; i--)
59 {
60 heapify(hT, i);
61 }
62}
63void printArray(vector<int> &hT)
64{
65 for (int i = 0; i < hT.size(); ++i)
66 cout << hT[i] << " ";
67 cout << "\n";
68}
69
70int main()
71{
72 vector<int> heapTree;
73
74 insert(heapTree, 3);
75 insert(heapTree, 4);
76 insert(heapTree, 9);
77 insert(heapTree, 5);
78 insert(heapTree, 2);
79
80 cout << "Max-Heap array: ";
81 printArray(heapTree);
82
83 deleteNode(heapTree, 4);
84
85 cout << "After deleting an element: ";
86
87 printArray(heapTree);
88}