-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA.c
More file actions
457 lines (388 loc) · 9.86 KB
/
DSA.c
File metadata and controls
457 lines (388 loc) · 9.86 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
#define QUEUE_MAX_SIZE 20
#define DICTIONARY_MAX_SIZE 20
/// @brief Implementation of custom static sized stack
typedef struct
{
int data[MAX_SIZE];
int top;
} Stack;
void init(Stack* s){
s->top = -1;
}
void push(Stack* s, int value){
if(s->top + 1 >= MAX_SIZE){
printf("Stack overflow");
return;
}
s->top++;
s->data[s->top] = value;
}
int pop(Stack* s){
if(s->top == -1){
printf("Stack underflow");
return -1;
}
int value = s->data[s->top];
s->top--;
return value;
}
int peek(Stack* s){
if(s->top == -1){
printf("Stack is empty");
return -1;
}
return s->data[s->top];
}
/// Implementation of custom static sized Queue
typedef struct{
int data[QUEUE_MAX_SIZE];
int front;//index to dequeue at
int rare;//index to enqueue at
int size;//counter to see - if we have empty slots in circular array
} Queue;
void initQueue(Queue* q){
q->front = 0;
q->rare = 0;
q->size = 0;
}
int isQueueFull(Queue* q){
return q->size == QUEUE_MAX_SIZE;
}
int isQueueEmpty(Queue* q){
return q->size == 0;
}
void enqueue(Queue* q, int value){
if(isQueueFull(q)){
printf("Can't Enqueue %d : %d \n", q->rare, value);
return;
}
q->data[q->rare] = value;
printf("Enqueued %d : %d \n", q->rare, value);
q->rare = (q->rare + 1) % QUEUE_MAX_SIZE;
q->size++;
}
int dequeue(Queue* q){
if(isQueueEmpty(q)){
printf("Empty queue %d : \n", q->front);
return -1;
}
int value = q->data[q->front];
printf("Dequeued %d: %d\n", q->front, value);
q->front = (q->front + 1) % QUEUE_MAX_SIZE;
q->size--;
return value;
}
void printArray(int array[], int size){
for (int i = 0; i < size; i++){
printf("[%d]", array[i]);
}
printf("\n");
}
void testStack(){
Stack s;
init(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
printf("Peek : Top of stack is : %d\n", peek(&s));
printf("Top of stack is : %d\n", pop(&s));
printf("Top of stack is : %d\n", pop(&s));
}
void testQueue(){
Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
dequeue(&q);
enqueue(&q, 60);
enqueue(&q, 70);
dequeue(&q);
dequeue(&q);
dequeue(&q);
dequeue(&q);
dequeue(&q);
printArray(q.data, sizeof(q.data)/ sizeof(int));
}
///Implementation of custom static sized dictionary
typedef struct
{
char *key;
int value;
} KeyValuePair;
typedef struct{
KeyValuePair dictArrar[DICTIONARY_MAX_SIZE];
} Dictionary;
unsigned long int hashFunction(const char *str, int maxSize)
{
unsigned long int hash = 5381;
int c;
while((c = *str++)){
hash = ((hash << 5) + hash) + c;
}
//make sure it is within the size range of array
unsigned long int hashIndex = hash % maxSize;
return hashIndex;
}
void insert_dict(Dictionary* d,char* key, int value){
//generate hash from the key
unsigned long int hashIndex = hashFunction(key, DICTIONARY_MAX_SIZE);
//set key value pair
d->dictArrar[hashIndex].key = malloc(strlen(key) + 1);
strcpy(d->dictArrar[hashIndex].key,key);
d->dictArrar[hashIndex].value = value;
}
int get_value_dict(Dictionary* d, char* key){
//generate has
unsigned long int searchHash = hashFunction(key, DICTIONARY_MAX_SIZE);
char* foundKey = d->dictArrar[searchHash].key;
if(strcmp(key, foundKey)){
printf("KeyNotFound");
return -1;
}
int foundValue = d->dictArrar[searchHash].value;
return foundValue;
}
int freeDictionery(Dictionary* d){
for (int i = 0; i < DICTIONARY_MAX_SIZE; i++){
if (d->dictArrar[i].key != NULL){
free(d->dictArrar[i].key);
}
}
}
void testDictionary(){
Dictionary d;
insert_dict(&d, "chetan", 25);
insert_dict(&d, "pratik", 23);
insert_dict(&d, "Pragya", 25);
printf("Age of chetan is %d", get_value_dict(&d, "chetan"));
freeDictionery(&d);
}
//Implementaion of custom binary trees
typedef struct Node{
int data;
struct Node *left;
struct Node *right;
} Node;
Node* createNode(int data){
Node *n = (Node *)malloc(sizeof(Node));
if(n == NULL){
printf("malloc failed \n");
return NULL;
}
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
void freeTree(Node* root){
if(root == NULL){
return;
}
freeTree(root->left);
freeTree(root->right);
free(root);
}
//root -> left -> right
void preorderTraversal(Node* root){
if (root == NULL){
return;
}
printf("%d \n",root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
//left -> root -> right
void inorderTraversal(Node* root){
if(root == NULL){
return;
}
inorderTraversal(root->left);
printf("%d\n", root->data);
inorderTraversal(root->right);
}
//left -> right -> root
void postorderTraversal(Node* root){
if(root == NULL){
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d\n",root->data);
}
//height of tree
int treeHeight(Node* root){
if(root == NULL){
return -1;
}
int leftHeight = treeHeight(root->left);
int rightHeight = treeHeight(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}
void testBinaryTree(){
Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(40);
root->left->right = createNode(50);
printf("_____PREORDER____\n");
preorderTraversal(root);
printf("_____INORDER____\n");
inorderTraversal(root);
printf("_____POSTORDER____\n");
postorderTraversal(root);
printf("Tree height: %d\n", treeHeight(root));
freeTree(root);
}
Node* insert(Node* root,int data){
Node *n;
if(root == NULL){
//we empty place to insert node
return createNode(data);
}
if(data < root->data){
root->left = insert(root->left, data);
}else if(data >= root -> data){
//handle duplicates as well
root->right = insert(root->right, data);
}
return root;
}
int search(Node* root, int key){
int val;
if(root == NULL){
return 0;
}
if(root->data == key){
return 1;
}
if(key < root->data){
val = search(root->left, key);
}else if( key >= root->data){
val = search(root->right, key);
}
return val;
}
void testBST(){
int arr[] = {10, 5, 20, 5, 15, 20, 21, 1, 6, 3};
int size = sizeof(arr) / sizeof(arr[0]);
Node *root = NULL;
for (int i = 1; i < size; i++){
root = insert(root, arr[i]);
}
inorderTraversal(root);
printf("BST has 3 : %d\n", search(root, 3));
printf("BST has 99 : %d\n", search(root, 99));
}
//Implementation of custom min heap
typedef struct
{
int data;// lower means higher priority
} Item;
typedef struct{
Item items[QUEUE_MAX_SIZE + 1]; // index starts from zero
int size;
} Heap;
int parentIndex(int n){
if(n <= 1){
return -1;
}
else{
return (int)n / 2; // child node n -> when n/2 give index of parent
}
}
int youngChildIndex(int n){
return 2 * n;//child is always 2n and 2n+1 in heap\
}
void heapSwap(Heap* pq, int i, int j){
Item item = pq->items[i];
pq->items[i] = pq->items[j];
pq->items[j] = item;
}
void bubbleUp(Heap* pq, int index){
if (parentIndex(index) == -1) return; // nothing to bubble
if(pq->items[index].data < pq->items[parentIndex(index)].data){
heapSwap(pq, index, parentIndex(index));
bubbleUp(pq, parentIndex(index));
}
}
void insertItem(Heap* pq,Item item){
if(pq->size >= QUEUE_MAX_SIZE){
printf("Queue is full");
return;
}
//insert element at the end
pq->size = pq->size + 1;
pq->items[pq->size] = item;
bubbleUp(pq, pq->size);
}
void makeHeap(Heap* hp,int* arr,int size){
for (int i = 0; i < size; i++){
Item item;
item.data = arr[i];
insertItem(hp, item);
}
}
void printHeap(Heap* hp){
for (int i = 1; i <= hp->size; i++){
printf("%d : %d\n", i, hp->items[i].data);
}
}
void bubbleDown(Heap* pq, int index){
int c;
int i;
int min_index;
min_index = index;
c = youngChildIndex(index);// we will get child at 2n
//iterate through both child
for (i = 0; i <= 1; i++){
//two child are at 2n and 2n + 1 -> min_index = parent index
//if we find child with value smaller than current(parent)
if(c + i <= pq->size && pq->items[min_index].data > pq->items[c + i].data){ //in first iteration we compare parent with first child
//in next we compare first child with second child
min_index = c + i;
}
}
//we found child to be smaller than parent
if(index != min_index){
heapSwap(pq, index, min_index);
bubbleDown(pq, min_index);
}
}
int extract_min(Heap* pq){
int min = -1; //minimum value
if(pq->size <= 0){
printf("Empty queue");
}else{
min = pq->items[1].data;//item at top is always minimum
//replace first item with last item
pq->items[1] = pq->items[pq->size];
// reduce total size
pq->size = pq->size - 1;
bubbleDown(pq, 1);
}
return min;
}
void testHeap(){
int arr[] = {12, 11, 5, 4, 3, 9, 15, 19};
Heap* hp = (Heap*)malloc(sizeof(Heap));
hp->size = 0;
makeHeap(hp, arr, sizeof(arr)/sizeof(arr[0]));
printHeap(hp);
printf("----min is----: %d\n", extract_min(hp));
printHeap(hp);
printf("----min is----: %d\n", extract_min(hp));
printHeap(hp);
printf("----min is----: %d\n", extract_min(hp));
printHeap(hp);
}
int main(){
testHeap();
return 0;
}