-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd_heap.c
More file actions
269 lines (250 loc) · 4.35 KB
/
d_heap.c
File metadata and controls
269 lines (250 loc) · 4.35 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define LENGTH 200
char delimiters[LENGTH] = " ,'\n''\t'";
int A[LENGTH];
int d=2;
int heapSize;
//int A[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
//30 10 20 40 60 50
void buildHeap(int *A);
void heapify(int *A, int i);
void swap(int *x, int *y);
void print(int *A);
void heapSort(int *A);
void heapInsert(int *A, int key);
void buildHeapPrepare(char *pstr);
void halt();
void stoi(char string[], int *A);
void readline(char *s);
int main(int argc, char *argv[])
{
int menu, key, new_d;
heapSize = 0;
while (1)
{
printf("D-ARY HEAP MENU\n\n");
printf("1.\tBuild a heap\n");
printf("2.\tInsert a key to the heap\n");
printf("3.\tExtract and print max key from heap\n");
printf("4.\tPrint heap\n");
printf("5.\tChange d-ary heap\n");
printf("6.\tExit\n");
printf("Please choose one: ");
scanf("%d", &menu);
switch (menu)
{
case 1:
{
while (1)
{
printf("Please enter integer 0(zero) to stop: ");
scanf("%d", &key);
if (key !=0)
A[heapSize++] = key;
else
break;
}
buildHeap(A);
print(A);
break;
}
case 2:
{
printf("Please enter a key to insert: ");
scanf("%d", &key);
heapInsert(A,key);
break;
}
case 3:
{
if(A[0])
{
printf("Max heap %d extracted\n",heapExtractMax(A));
}
print(A);
break;
}
case 4:
{
print(A);
break;
}
case 5:
{
printf("Please enter d number: ");
scanf("%d", &new_d);
if(new_d < 2)
{
printf("D can't be <2\n");
continue;
}
d = new_d;
buildHeap(A);
print(A);
break;
}
case 6:
{
halt();
break;
}
default: printf("Sorry does not correspond to that function please try again.\n");
}
}
return 0;
}
int length(int *A)
{
int i=0;
while(A[i])
i++;
return i;
}
void heapSort(int *A)
{
printf("start heap sort\n");
int i;
buildHeap(A);
for (i=length(A)-1;i > 0;i--)
{
swap(&A[0], &A[i]);
heapSize--;
heapify(A,0);
}
}
void print(int *A)
{
int i;
for(i=0;i<length(A);i++)
printf("%d ",A[i]);
printf("\n");
}
int heapExtractMax(int *A)
{
int max;
if(heapSize < 0)
{
printf("Heap underflow: %d\n", heapSize);
return -1;
}
max = A[0];
A[0] = A[heapSize-1];
A[heapSize-1] = (int)NULL;
heapSize--;
heapify(A,0);
return max;
}
void heapInsert(int *A, int key)
{
heapSize++;
int i = heapSize-1;
while (i > 0 && A[parent(i)] < key)
{
A[i] = A[parent(i)];
i = parent(i);
}
A[i] = key;
}
void buildHeapPrepare(char *pstr)
{
int i=1;
while ((pstr = strtok((char *)NULL, delimiters )) != NULL)
{
if (i > LENGTH)
break;
A[i++] = atoi(pstr);
}
}
void buildHeap(int *A)
{
heapSize = length(A);
int i;
for(i=length(A)/2; i > 0; i--)
heapify(A, i-1);
}
void heapify(int *A, int i)
{
int largest=i, l, r;
do
{
print(A);
l = left(i);
printf("l<%d>heapSize<%d>\n",l,heapSize);
r = right(i);
printf("r<%d>i<%d>\n",r,i);
if (l < heapSize)
{
printf("in L: %d\n",l);
if(A[l]>A[i])
{
printf("A[l]<%d>A[i]%d\n",A[l],A[i]);
largest = l;
}
else
{
printf("A[l]<%d>A[i]%d\n",A[l],A[i]);
largest = i;
}
}
if (r < heapSize)
{
printf("in R: %d\n",r);
if(A[r] > A[largest])
{
printf("A[r]<%d>A[largest]%d\n",A[r],A[largest]);
largest = r;
}
}
if(largest != i)
{
swap(&A[i], &A[largest]);
i=largest;
}
else
break;
}
while (i < heapSize);
}
int max(int x, int y)
{
return (x > y) ? x : y;
}
int parent(int i)
{
return (i / 2);
}
int left(int i)
{
double a=heapSize, b=d, c;
c=log(a) / log(b);
long e = c;
printf("log base-%lf of %lf c=%lf is: %d\n",a,b,c,d);
return e;
}
/*
int left(int i)
{
return 2*i+1;
}
*/
int right(int i)
{
return left(i)+1;
}
void swap(int *x, int *y)
{
int tmp;
printf("swap %d with %d\n",*x,*y);
tmp = *x;
*x = *y;
*y = tmp;
}
/* quit program with greetings */
void halt()
{
printf("bye bye\n");
exit(0);
}