forked from iamvs-2002/C_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.c
97 lines (88 loc) · 1.87 KB
/
Heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
int size = 0;
int main()
{
printf("Kindly enter the number of elements: ");
int n;
int i=0;
scanf("%d",&n);
int arr[n];
printf("Kindly enter the array elements: ");
for(i=0;i<n;i++){
int ss;
scanf("%d",&ss);
insert(arr,ss);
}
printf("Max-Heap array: ");
for (int i = 0; i < size; ++i)
printf("%d ", arr[i]);
printf("\n");
printf("\nKindly enter the element you want to delete: ");
int x;
scanf("%d",&x);
deleteRoot(arr, x);
printf("After deleting an element: ");
for (int i = 0; i < size; ++i)
printf("%d ", arr[i]);
printf("\n");
}
void insert(int array[], int newNum)
{
if (size == 0)
{
array[0] = newNum;
size += 1;
}
else
{
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
}
void deleteRoot(int array[], int num)
{
int i;
for (i = 0; i < size; i++)
{
if (num == array[i])
break;
}
swap(&array[i], &array[size - 1]);
size -= 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
void heapify(int array[], int size, int i)
{
if (size == 1)
{
printf("Single element in the heap");
}
else
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < size && array[l] > array[largest])
largest = l;
if (r < size && array[r] > array[largest])
largest = r;
if (largest != i)
{
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
}
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}