-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.c
163 lines (139 loc) · 2.73 KB
/
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "Heap.h"
#define TRUE 0
#define FALSE 1
//Creating heap
Heap* createHeap(int max)
{
Heap* pHeap = (Heap*)malloc(sizeof(Heap));
if (pHeap == NULL)
{
return NULL;
}
pHeap->heapAry = (Element*)malloc(sizeof(Element));
pHeap->maxSize = max;
pHeap->size = 0;
return pHeap;
}
//Inserting data
void InsertHeap(Heap* pHeap, Element data)
{
if (heapFull(pHeap))
{
printf("Heap is full!");
return;
}
pHeap->heapAry[(pHeap->size)++] = data;
reHeapUp_recursive(pHeap, (pHeap->size) - 1);
}
//Resorting recursively after inserting data
void reHeapUp_recursive(Heap* pHeap, int index)
{
int parentIndex = 0;
if (index <= 0 || index >= pHeap->size)
{
return;
}
parentIndex = (index - 1) / 2;
if (pHeap->heapAry[index] > pHeap->heapAry[parentIndex])
{
Element temp = pHeap->heapAry[index];
pHeap->heapAry[index] = pHeap->heapAry[parentIndex];
pHeap->heapAry[parentIndex] = temp;
reHeapUp_recursive(pHeap, parentIndex);
}
}
//Deleting data from heap
int deleteHeap(Heap *pHeap, Element dataBye)
{
if (heapEmpty(pHeap))
{
printf("Heap is empty!\n");
return FALSE;
}
dataBye = pHeap->heapAry[0];
pHeap->heapAry[0] = pHeap->heapAry[(pHeap->size)--];
reHeapDown_recursive(pHeap, 0);
return TRUE;
}
//Resorting datas from Heap after deleting
void reHeapDown_recursive(Heap* pHeap, int index)
{
if (index < 0 || index >= pHeap->size)
{
return;
}
if (index * 2 + 1 < pHeap->size)
{
int maxChild = index * 2 + 1;
if (maxChild + 1 < pHeap->size && pHeap->heapAry[maxChild] < pHeap->heapAry[maxChild + 1])
{
maxChild++;
}
if (pHeap->heapAry[index] < pHeap->heapAry[maxChild])
{
Element temp = pHeap->heapAry[index];
pHeap->heapAry[index] = pHeap->heapAry[maxChild];
pHeap->heapAry[maxChild] = temp;
reHeapDown_recursive(pHeap, maxChild);
}
}
}
//Creating heap
void buildHeap(Heap* pHeap)
{
int i;
for (i = 0; i < pHeap->size; i++)
{
reHeapUp_recursive(pHeap, i);
}
}
//Counting the number of elements in heap
int heapCount(Heap* pHeap)
{
if (heapEmpty(pHeap))
{
return 0;
}
else if (heapFull(pHeap))
{
return pHeap->maxSize;
}
else
return pHeap->size;
}
//Is Heap Empty?
bool heapEmpty(Heap* pHeap)
{
if (pHeap->size <= 0)
{
return true;
}
else
return false;
}
bool heapFull(Heap* pHeap)
{
if (pHeap->size == pHeap->maxSize)
{
return true;
}
return false;
}
bool heapDestroy(Heap* pHeap)
{
int i = 0;
if (heapEmpty(pHeap))
{
free(pHeap);
return true;
}
while (i < pHeap->size)
{
free(pHeap->heapAry+i);
}
free(pHeap);
return true;
}