-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap Data Structure.c
135 lines (110 loc) · 3.2 KB
/
Heap Data Structure.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Heap Data Structure implementation
Date created: Monday; June 19, 2023
In this code, you can perform operations on a heap data structure, including inserting elements, deleting elements, and printing the heap. The program will keep prompting the user for a decision until they choose to exit.*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEAP_SIZE 100
struct Heap {
int arr[MAX_HEAP_SIZE];
int size;
};
struct Heap* createHeap() {
struct Heap* heap = (struct Heap*)malloc(sizeof(struct Heap));
heap->size = 0;
return heap;
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void insert(struct Heap* heap, int value) {
if (heap->size == MAX_HEAP_SIZE) {
printf("Heap is full. Cannot insert any more elements.\n");
return;
}
heap->size++;
int i = heap->size - 1;
heap->arr[i] = value;
while (i != 0 && heap->arr[i] < heap->arr[(i - 1) / 2]) {
swap(&heap->arr[i], &heap->arr[(i - 1) / 2]);
i = (i - 1) / 2;
}
printf("Element inserted successfully!\n");
}
void heapify(struct Heap* heap, int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < heap->size && heap->arr[left] < heap->arr[smallest])
smallest = left;
if (right < heap->size && heap->arr[right] < heap->arr[smallest])
smallest = right;
if (smallest != index) {
swap(&heap->arr[index], &heap->arr[smallest]);
heapify(heap, smallest);
}
}
void deleteElement(struct Heap* heap, int value) {
int i;
for (i = 0; i < heap->size; i++) {
if (heap->arr[i] == value)
break;
}
if (i == heap->size) {
printf("Element %d not found in the heap.\n", value);
return;
}
heap->arr[i] = heap->arr[heap->size - 1];
heap->size--;
heapify(heap, i);
printf("Element deleted successfully!\n");
}
void printHeap(struct Heap* heap) {
if (heap->size == 0) {
printf("Heap is empty.\n");
return;
}
int i;
printf("Heap elements: ");
for (i = 0; i < heap->size; i++) {
printf("%d ", heap->arr[i]);
}
printf("\n");
}
int main() {
struct Heap* heap = createHeap();
int value;
char decision;
do {
printf("\nHeap Operations:\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Print the heap\n");
printf("4. Exit\n");
printf("Enter your decision: ");
scanf(" %c", &decision);
switch (decision) {
case '1':
printf("Enter the element to be inserted: ");
scanf("%d", &value);
insert(heap, value);
break;
case '2':
printf("Enter the element to be deleted: ");
scanf("%d", &value);
deleteElement(heap, value);
break;
case '3':
printHeap(heap);
break;
case '4':
printf("Exiting...\n");
break;
default:
printf("Invalid decision. Please try again.\n");
}
} while (decision != '4');
return 0;
}