Skip to content

Commit 096b376

Browse files
Add Min Binary Heap implementation
1 parent e5dad3f commit 096b376

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <limits.h>
4+
5+
// ------------------------------
6+
// Min Binary Heap Structure
7+
// ------------------------------
8+
typedef struct MinHeap {
9+
int *arr;
10+
int size;
11+
int capacity;
12+
} MinHeap;
13+
14+
// ------------------------------
15+
MinHeap* initHeap(int capacity) {
16+
MinHeap *heap = (MinHeap*)malloc(sizeof(MinHeap));
17+
heap->arr = (int*)malloc(capacity * sizeof(int));
18+
heap->size = 0;
19+
heap->capacity = capacity;
20+
return heap;
21+
}
22+
23+
// ------------------------------
24+
void swap(int *a, int *b) {
25+
int temp = *a;
26+
*a = *b;
27+
*b = temp;
28+
}
29+
30+
// ------------------------------
31+
void heapifyUp(MinHeap *heap, int index) {
32+
if (index == 0) return;
33+
34+
int parent = (index - 1) / 2;
35+
36+
if (heap->arr[parent] > heap->arr[index]) {
37+
swap(&heap->arr[parent], &heap->arr[index]);
38+
heapifyUp(heap, parent);
39+
}
40+
}
41+
42+
// ------------------------------
43+
void heapifyDown(MinHeap *heap, int index) {
44+
int left = 2 * index + 1;
45+
int right = 2 * index + 2;
46+
int smallest = index;
47+
48+
if (left < heap->size && heap->arr[left] < heap->arr[smallest])
49+
smallest = left;
50+
51+
if (right < heap->size && heap->arr[right] < heap->arr[smallest])
52+
smallest = right;
53+
54+
if (smallest != index) {
55+
swap(&heap->arr[smallest], &heap->arr[index]);
56+
heapifyDown(heap, smallest);
57+
}
58+
}
59+
60+
// ------------------------------
61+
void insert(MinHeap *heap, int value) {
62+
if (heap->size == heap->capacity) {
63+
printf("Heap Overflow\n");
64+
return;
65+
}
66+
67+
heap->arr[heap->size] = value;
68+
heapifyUp(heap, heap->size);
69+
heap->size++;
70+
}
71+
72+
// ------------------------------
73+
int extractMin(MinHeap *heap) {
74+
if (heap->size <= 0)
75+
return INT_MIN;
76+
77+
int root = heap->arr[0];
78+
79+
heap->arr[0] = heap->arr[heap->size - 1];
80+
heap->size--;
81+
82+
heapifyDown(heap, 0);
83+
84+
return root;
85+
}
86+
87+
// ------------------------------
88+
int search(MinHeap *heap, int value) {
89+
for (int i = 0; i < heap->size; i++)
90+
if (heap->arr[i] == value)
91+
return i;
92+
return -1;
93+
}
94+
95+
// ------------------------------
96+
void display(MinHeap *heap) {
97+
printf("Heap elements: ");
98+
for (int i = 0; i < heap->size; i++)
99+
printf("%d ", heap->arr[i]);
100+
printf("\n");
101+
}
102+
103+
// ------------------------------
104+
void freeHeap(MinHeap *heap) {
105+
free(heap->arr);
106+
free(heap);
107+
}
108+
109+
// ------------------------------
110+
// USER INPUT (Exactly what you asked for)
111+
// ------------------------------
112+
int main() {
113+
int n;
114+
115+
printf("Enter number of elements to insert: ");
116+
scanf("%d", &n);
117+
118+
MinHeap *heap = initHeap(n);
119+
120+
printf("Enter %d elements:\n", n);
121+
for (int i = 0; i < n; i++) {
122+
int val;
123+
scanf("%d", &val);
124+
insert(heap, val);
125+
}
126+
127+
printf("\nFinal Min Heap:\n");
128+
display(heap);
129+
130+
printf("Min element (root) = %d\n", heap->arr[0]);
131+
132+
freeHeap(heap);
133+
return 0;
134+
}

0 commit comments

Comments
 (0)