-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmaxheap.c
More file actions
149 lines (133 loc) · 4.02 KB
/
Copy pathmaxheap.c
File metadata and controls
149 lines (133 loc) · 4.02 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct max_heap {
int *x;
int size;
int heap_count;
} Heap;
Heap *create_heap(Heap *heap);
void down_heapify(Heap *heap, int index);
void up_heapify(Heap *heap, int index);
void heap_push(Heap *heap, int x);
void heap_pop(Heap *heap);
int heap_top(Heap *heap);
int heap_empty(Heap *heap);
int heap_size(Heap *heap);
int main() {
Heap *head = create_heap(head);
heap_push(head, 19125052);
printf("Pushing Element : 19125052\n");
heap_push(head, 19125003);
printf("Pushing Element : 19125003\n");
heap_push(head, 19125002);
printf("Pushing Element : 19125002\n");
heap_push(head, 19125008);
printf("Pushing Element : 19125008\n");
printf("Top Element = %d \n", heap_top(head));
heap_push(head, 19125001);
printf("Pushing Element : 19125001\n");
heap_push(head, 19125007);
printf("Pushing Element : 19125007\n");
printf("Top Element = %d \n", heap_top(head));
heap_pop(head);
printf("Popping An Element...\n");
printf("Top Element = %d \n", heap_top(head));
heap_pop(head);
printf("Popping An Element...\n");
printf("Top Element = %d \n", heap_top(head));
printf("\n");
return 0;
}
Heap *create_heap(Heap *heap) {
heap = (Heap *)malloc(sizeof(Heap));
heap -> size = 1;
heap -> x = (int *)malloc(heap -> size * sizeof(int));
heap -> heap_count = 0;
return heap;
}
void down_heapify(Heap *heap, int index) {
if (index >= heap -> heap_count) {
return;
}
int heapleft = index * 2 + 1;
int heapright = index * 2 + 2;
int left_flag = 0, right_flag = 0;
int heap_maxima = *((heap -> x) + index);
if (heapleft < heap -> heap_count && heap_maxima < *((heap -> x) + heapleft)) {
heap_maxima = *((heap -> x) + heapleft);
left_flag = 1;
}
if (heapright < heap -> heap_count && heap_maxima < *((heap -> x) + heapright)) {
heap_maxima = *((heap -> x) + heapright);
left_flag = 0;
right_flag = 1;
}
if (left_flag) {
*((heap -> x) + heapleft) = *((heap -> x) + index);
*((heap -> x) + index) = heap_maxima;
down_heapify(heap, heapleft);
}
if (right_flag) {
*((heap -> x) + heapright) = *((heap -> x) + index);
*((heap -> x) + index) = heap_maxima;
down_heapify(heap, heapright);
}
}
void up_heapify(Heap *heap, int index) {
int parent = (index - 1) / 2;
if (parent < 0) {
return;
}
if (*((heap -> x) + index) > *((heap -> x) + parent)) {
int temp = *((heap -> x) + index);
*((heap -> x) + index) = *((heap -> x) + parent);
*((heap -> x) + parent) = temp;
up_heapify(heap, parent);
}
}
void heap_push(Heap *heap, int x) {
if (heap -> heap_count >= heap -> size) {
return;
}
*((heap -> x) + heap -> heap_count) = x;
heap -> heap_count++;
if (4 * heap -> heap_count >= 3 * heap -> size) {
heap -> size *= 2;
(heap -> x) = (int *)realloc((heap -> x), (heap -> size) * sizeof(int));
}
up_heapify(heap, heap->heap_count - 1);
}
void heap_pop(Heap *heap) {
if (heap->heap_count == 0) {
return;
}
heap -> heap_count--;
int temp = *((heap -> x) + heap -> heap_count);
*((heap -> x) + heap -> heap_count) = *(heap -> x);
*(heap -> x) = temp;
down_heapify(heap, 0);
if (4 * heap -> heap_count <= heap -> size) {
heap -> size /= 2;
(heap -> x) = (int *)realloc((heap -> x), (heap -> size) * sizeof(int));
}
}
int heap_top(Heap *heap) {
if (heap -> heap_count != 0) {
return *(heap -> x);
}
else {
return INT_MIN;
}
}
int heap_empty(Heap *heap) {
if (heap -> heap_count != 0) {
return 0;
}
else {
return 1;
}
}
int heap_size(Heap *heap) {
return heap -> heap_count;
}