-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.h
More file actions
152 lines (128 loc) · 3.59 KB
/
heap.h
File metadata and controls
152 lines (128 loc) · 3.59 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
150
151
152
#ifndef HEAP_H_INCLUDED
#define HEAP_H_INCLUDED
#include "helper.h"
#include <math.h>
#define SIZE 25
enum {
MAX_HEAP,
MIN_HEAP
};
typedef struct {
int size;
int currIdx;
int mode;
int *data;
} Heap;
void createHeap(Heap **heap, int mode, int size) {
if (size == NULL)
size = SIZE;
if (mode == NULL)
mode = MAX_HEAP;
else if (mode != 0 && mode != 1)
mode = MAX_HEAP;
(*heap) = malloc(sizeof(Heap));
(*heap)->size = size;
(*heap)->currIdx = 0;
(*heap)->mode = mode;
(*heap)->data = malloc(sizeof(int) * size);
}
void heapify(Heap **heap, int i) {
if ((*heap)->currIdx <= 1)
return;
int left = 2*i + 1;
int right = 2*i + 2;
if ((*heap)->mode == MAX_HEAP) {
int largest = i;
if (left < (*heap)->currIdx && (*heap)->data[left] > (*heap)->data[largest])
largest = left;
if (right < (*heap)->currIdx && (*heap)->data[right] > (*heap)->data[largest])
largest = right;
if (largest != i) {
swap(&(*heap)->data[i], &(*heap)->data[largest]);
heapify(heap, largest);
}
} else {
int minimum = i;
if (left < (*heap)->currIdx && (*heap)->data[left] < (*heap)->data[minimum])
minimum = left;
if (right < (*heap)->currIdx && (*heap)->data[right] < (*heap)->data[minimum])
minimum = right;
if (minimum != i) {
swap(&(*heap)->data[i], &(*heap)->data[minimum]);
heapify(heap, minimum);
}
}
}
void insert(Heap **heap, int data) {
if ((*heap)->currIdx == (*heap)->size)
return;
(*heap)->data[(*heap)->currIdx++] = data;
for (int i=(*heap)->currIdx/2-1; i>=0; i--)
heapify(heap, i);
}
int getData(Heap *heap, int data) {
if (heap->mode == MAX_HEAP) {
for (int i=0; i<heap->currIdx; i++) {
if (heap->data[i] == data)
return i;
if (data > heap->data[i])
break;
}
return -1;
} else {
for (int i=0; i<heap->currIdx; i++) {
if (heap->data[i] == data)
return i;
if (data < heap->data[i])
break;
}
return -1;
}
}
void removeData(Heap **heap, int data) {
int idxToRemove = getData((*heap), data);
if (idxToRemove == -1)
return;
else {
swap(&(*heap)->data[(*heap)->currIdx--], &(*heap)->data[idxToRemove]);
for (int i=(*heap)->currIdx/2-1; i>=0; i--)
heapify(heap, i);
}
}
int getDepth(Heap *heap) {
return (log(heap->currIdx) / log(2));
}
int getMin(Heap *heap) {
if (heap->mode == MIN_HEAP)
return heap->data[0];
else {
int depth = getDepth(heap);
int first = pow(2, depth) - 1;
int last = heap->currIdx - 1;
int n = last - first + 1, j = 0;
int *arr = malloc(sizeof(int) * n);
for (int i=first; i<(first + n); i++)
arr[j++] = heap->data[i];
return minimum(arr, n);
}
}
int getMax(Heap *heap) {
if (heap->mode == MAX_HEAP)
return heap->data[0];
else {
int depth = getDepth(heap);
int first = pow(2, depth) - 1;
int last = heap->currIdx - 1;
int n = last - first + 1, j = 0;
int *arr = malloc(sizeof(int) * n);
for (int i=first; i<(first + n); i++)
arr[j++] = heap->data[i];
return maximum(arr, n);
}
}
void printHeap(Heap *heap) {
for (int i=0; i<heap->currIdx; i++)
printf("%d ", heap->data[i]);
printf("\n");
}
#endif // HEAP_H_INCLUDED