@@ -327,3 +327,164 @@ def main():
327
327
328
328
main()
329
329
```
330
+
331
+ ### C
332
+
333
+ ``` C
334
+ #include < stdio.h>
335
+ #include < stdlib.h>
336
+ #include < string.h>
337
+
338
+ typedef struct node{
339
+ struct node *children; // I chose a pointer to create a dynamic array
340
+ int children_num;
341
+ int ID;
342
+ } node;
343
+
344
+ // There is no stack or queue in c so I created my own
345
+ typedef struct node_list{
346
+ node n;
347
+ struct node_list *last_list, *next_list;
348
+ } node_list;
349
+
350
+ typedef struct node_points{
351
+ node_list *start_point, *end_point;
352
+ } node_points;
353
+
354
+ void push (node_points * np, node * n){
355
+ // Adding node into a queue or a stack
356
+ node_list * temp = (node_list* )malloc(sizeof(node_list));
357
+ temp->n = * n;
358
+ temp->last_list = temp->next_list = NULL;
359
+
360
+ if(!np->end_point){
361
+ np->start_point = temp;
362
+ }else{
363
+ np->end_point->next_list = temp;
364
+ temp->last_list = np->end_point;
365
+ }
366
+ np->end_point = temp;
367
+ }
368
+
369
+ void stack_pop(node_points * np){
370
+ // Removing the last node_list of the stack
371
+ node_list * temp;
372
+ temp = np->end_point;
373
+ if(temp){
374
+ np->end_point = temp->last_list;
375
+ if(!np->end_point){
376
+ np->start_point = NULL;
377
+ }
378
+ free(temp);
379
+ }
380
+ }
381
+
382
+ void queue_pop(node_points * np){
383
+ // Removing the first node_list of the queue
384
+ node_list * temp;
385
+ temp = np->start_point;
386
+ if(temp){
387
+ np->start_point = temp->next_list;
388
+ if(!np->start_point){
389
+ np->end_point = NULL;
390
+ }
391
+ free(temp);
392
+ }
393
+ }
394
+
395
+ void create_tree(node * n, int num_row, int num_child) {
396
+ n->ID = num_row;
397
+ if(num_row == 0){
398
+ return;
399
+ }
400
+
401
+ // Creating children
402
+ // Using malloc to make an array of nodes with size num_child
403
+ n->children = (node *)malloc(num_child*sizeof(*n->children));
404
+ // When an array is made into a pointer you can't get it's size later
405
+ n->children_num = num_child;
406
+ for(int i = 0; i < num_child; ++i){
407
+ node child;
408
+ create_tree(&child, num_row - 1, num_child);
409
+ *(n->children + i) = child;
410
+ }
411
+ }
412
+
413
+ void DFS_recursive(node * n){
414
+ printf("%d\n", n->ID);
415
+
416
+ // Checking if the node's children exist
417
+ if(!n->children){
418
+ return;
419
+ }
420
+
421
+ for(int i = 0; i < n->children_num; ++i){
422
+ DFS_recursive((n->children + i));
423
+ }
424
+ }
425
+
426
+ void DFS_stack(node * n){
427
+ // Creating a stack and then setting its value to 0
428
+ node_points stack;
429
+ memset(&stack, 0, sizeof(node_points));
430
+ push(&stack, n);
431
+ node temp;
432
+
433
+ while(stack.start_point != NULL){
434
+ temp = stack.end_point->n;
435
+ printf("%d\n", temp.ID);
436
+ stack_pop(&stack);
437
+ for(int i=0; i < temp.children_num; ++i){
438
+ // Checking if the node has any children
439
+ if(!temp.children){
440
+ break;
441
+ }
442
+ push(&stack, temp.children + i);
443
+ }
444
+ }
445
+ }
446
+
447
+ void BFS_queue(node * n){
448
+ // Creating a queue and then setting its value to 0
449
+ node_points queue;
450
+ memset(&queue, 0, sizeof(node_points));
451
+ push(&queue, n);
452
+ node temp;
453
+
454
+ while(queue.start_point != NULL){
455
+ temp = queue.start_point->n;
456
+ printf("%d\n", temp.ID);
457
+ queue_pop(&queue);
458
+ for(int i = 0; i < temp.children_num; ++i){
459
+ // Checking if the node has any children
460
+ if(!temp.children){
461
+ break;
462
+ }
463
+ push(&queue, temp.children + i);
464
+ }
465
+ }
466
+ }
467
+
468
+ void destroy_tree(node * n){
469
+ // This function is for cleaning up all the nodes
470
+ if(n->ID == 0){
471
+ return;
472
+ }
473
+
474
+ for(int i = 0; i < n->children_num; ++i){
475
+ destroy_tree(n->children + i);
476
+ }
477
+ free(n->children);
478
+ }
479
+
480
+ int main() {
481
+ node root;
482
+ create_tree(&root, 3, 3);
483
+ //DFS_recursive(&root);
484
+ //DFS_stack(&root);
485
+ BFS_queue(&root);
486
+ destroy_tree(&root);
487
+
488
+ return 0;
489
+ }
490
+ ```
0 commit comments