-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathmax_heap.c
149 lines (143 loc) · 3.97 KB
/
max_heap.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <limits.h> /// for INT_MIN
#include <stdio.h> /// for IO operations
#include <stdlib.h> /// for dynamic memory allocation
typedef struct max_heap
{
int *p;
int size;
int count;
} Heap;
Heap *create_heap(Heap *heap); /*Creates a max_heap structure and returns a
pointer to the struct*/
void down_heapify(Heap *heap, int index); /*Pushes an element downwards in the
heap to find its correct position*/
void up_heapify(Heap *heap, int index); /*Pushes an element upwards in the heap
to find its correct position*/
void push(Heap *heap, int x); /*Inserts an element in the heap*/
void pop(Heap *heap); /*Removes the top element from the heap*/
int top(Heap *heap); /*Returns the top element of the heap or returns INT_MIN if
heap is empty*/
int empty(Heap *heap); /*Checks if heap is empty*/
int size(Heap *heap); /*Returns the size of heap*/
int main()
{
Heap *head = create_heap(head);
push(head, 10);
printf("Pushing element : 10\n");
push(head, 3);
printf("Pushing element : 3\n");
push(head, 2);
printf("Pushing element : 2\n");
push(head, 8);
printf("Pushing element : 8\n");
printf("Top element = %d \n", top(head));
push(head, 1);
printf("Pushing element : 1\n");
push(head, 7);
printf("Pushing element : 7\n");
printf("Top element = %d \n", top(head));
pop(head);
printf("Popping an element.\n");
printf("Top element = %d \n", top(head));
pop(head);
printf("Popping an element.\n");
printf("Top element = %d \n", top(head));
printf("\n");
return 0;
}
Heap *create_heap(Heap *heap)
{
heap = (Heap *)malloc(sizeof(Heap));
heap->size = 1;
heap->p = (int *)malloc(heap->size * sizeof(int));
heap->count = 0;
return heap;
}
void down_heapify(Heap *heap, int index)
{
if (index >= heap->count)
return;
int left = index * 2 + 1;
int right = index * 2 + 2;
int leftflag = 0, rightflag = 0;
int maximum = *((heap->p) + index);
if (left < heap->count && maximum < *((heap->p) + left))
{
maximum = *((heap->p) + left);
leftflag = 1;
}
if (right < heap->count && maximum < *((heap->p) + right))
{
maximum = *((heap->p) + right);
leftflag = 0;
rightflag = 1;
}
if (leftflag)
{
*((heap->p) + left) = *((heap->p) + index);
*((heap->p) + index) = maximum;
down_heapify(heap, left);
}
if (rightflag)
{
*((heap->p) + right) = *((heap->p) + index);
*((heap->p) + index) = maximum;
down_heapify(heap, right);
}
}
void up_heapify(Heap *heap, int index)
{
int parent = (index - 1) / 2;
if (parent < 0)
return;
if (*((heap->p) + index) > *((heap->p) + parent))
{
int temp = *((heap->p) + index);
*((heap->p) + index) = *((heap->p) + parent);
*((heap->p) + parent) = temp;
up_heapify(heap, parent);
}
}
void push(Heap *heap, int x)
{
if (heap->count >= heap->size)
return;
*((heap->p) + heap->count) = x;
heap->count++;
if (4 * heap->count >= 3 * heap->size)
{
heap->size *= 2;
(heap->p) = (int *)realloc((heap->p), (heap->size) * sizeof(int));
}
up_heapify(heap, heap->count - 1);
}
void pop(Heap *heap)
{
if (heap->count == 0)
return;
heap->count--;
int temp = *((heap->p) + heap->count);
*((heap->p) + heap->count) = *(heap->p);
*(heap->p) = temp;
down_heapify(heap, 0);
if (4 * heap->count <= heap->size)
{
heap->size /= 2;
(heap->p) = (int *)realloc((heap->p), (heap->size) * sizeof(int));
}
}
int top(Heap *heap)
{
if (heap->count != 0)
return *(heap->p);
else
return INT_MIN;
}
int empty(Heap *heap)
{
if (heap->count != 0)
return 0;
else
return 1;
}
int size(Heap *heap) { return heap->count; }