-
Notifications
You must be signed in to change notification settings - Fork 0
/
ins7delheap.c
87 lines (86 loc) · 2.13 KB
/
ins7delheap.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
#include <stdio.h>
int a[50], n = 0, i, value, temp, parent;
int insertheap(int a[], int n, int value) {
n = n + 1;
a[n] = value;
i = n;
while (i > 1) {
parent = i / 2;
if (a[parent] < a[i]) {
temp = a[parent];
a[parent] = a[i];
a[i] = temp;
i = parent;
} else {
return n;
}
}
return n;
}
int delete_root(int a[], int n) {
if (n == 0) {
printf("Heap is empty. Cannot delete from an empty heap.\n");
return n;
}
int left, right, ptr, last, temp;
last = a[n];
n = n - 1;
ptr = 1;
left = 2;
right = 3;
a[ptr] = last;
while (left <= n) {
if (a[ptr] >= a[left] && a[ptr] >= a[right])
return n;
if (a[right] <= a[left]) {
temp = a[ptr];
a[ptr] = a[left];
a[left] = temp;
ptr = left;
} else {
temp = a[ptr];
a[ptr] = a[right];
a[right] = temp;
ptr = right;
}
left = 2 * ptr;
right = 2 * ptr + 1;
}
printf("Root node deleted\n");
return n;
}
int main() {
int choice, j;
do {
printf("\n***MAIN MENU***");
printf("\n1. INSERT");
printf("\n2. DELETE");
printf("\n3. DISPLAY");
printf("\n4. EXIT");
printf("\nEnter your choice:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the item to be inserted in the heap:");
scanf("%d", &value);
n = insertheap(a, n, value);
break;
case 2:
n = delete_root(a, n);
break;
case 3:
if (n == 0) {
printf("Heap is empty.\n");
} else {
printf("The heap is:");
for (j = 1; j <= n; j++)
printf(" %d", a[j]);
printf("\n");
}
break;
case 4:
break;
}
} while (choice != 4);
return 0;
}