Advertisement
cd62131

BFS and DFS

May 24th, 2019
1,672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. typedef struct tree tree;
  6. struct tree {
  7.   int data;
  8.   tree *left, *right;
  9. };
  10. static tree *tree_new(int data) {
  11.   tree *new = calloc(1, sizeof(*new));
  12.   new->data = data, new->left = new->right = NULL;
  13.   return new;
  14. }
  15. static void tree_free(tree *t) {
  16.   if (!t) { return; }
  17.   tree_free(t->left), tree_free(t->right), free(t);
  18. }
  19. static tree *tree_insert(tree *t, int x) {
  20.   if (!t) { return tree_new(x); }
  21.   if (x < t->data) {
  22.     t->left = tree_insert(t->left, x);
  23.   } else if (t->data < x) {
  24.     t->right = tree_insert(t->right, x);
  25.   }
  26.   return t;
  27. }
  28. static void tree_inorder(tree *t) {
  29.   if (!t) { return; }
  30.   tree_inorder(t->left), printf("%3d", t->data), tree_inorder(t->right);
  31. }
  32. static void tree_show(tree *t, int level) {
  33.   if (!t) { return; }
  34.   tree_show(t->left, level + 1);
  35.   for (int i = 0; i < level; ++i) {
  36.     fputs((i < level - 1 ? "    " : " ---"), stdout);
  37.   }
  38.   printf("%d\n", t->data);
  39.   tree_show(t->right, level + 1);
  40. }
  41. static int randint(int max) {
  42.   static bool first = true;
  43.   if (first) { srand(time(NULL)), first = false; }
  44.   return rand() % max;
  45. }
  46. typedef struct entry entry;
  47. struct entry {
  48.   tree *tree;
  49.   entry *next;
  50. };
  51. typedef struct {
  52.   entry *head, *tail;
  53. } stack_queue;
  54. static entry *entry_new(tree *t) {
  55.   entry *new = calloc(1, sizeof(*new));
  56.   new->tree = t, new->next = NULL;
  57.   return new;
  58. }
  59. static stack_queue *queue_new(void) {
  60.   stack_queue *new = calloc(1, sizeof(*new));
  61.   new->head = new->tail = NULL;
  62.   return new;
  63. }
  64. static bool queue_is_empty(stack_queue *q) { return !q->head; }
  65. static void queue_enqueue(stack_queue *q, tree *t) {
  66.   if (!t) { return; }
  67.   entry *new = entry_new(t);
  68.   if (queue_is_empty(q)) {
  69.     q->head = q->tail = new;
  70.     return;
  71.   }
  72.   q->tail->next = new, q->tail = new;
  73. }
  74. static void entry_free(entry *e) {
  75.   if (!e) { return; }
  76.   entry_free(e->next), free(e);
  77. }
  78. static tree *queue_dequeue(stack_queue *q) {
  79.   if (queue_is_empty(q)) { return NULL; }
  80.   entry *e = q->head;
  81.   q->head = e->next, e->next = NULL;
  82.   tree *t = e->tree;
  83.   entry_free(e);
  84.   return t;
  85. }
  86. static void queue_free(stack_queue *q) {
  87.   if (!q) { return; }
  88.   entry_free(q->head);
  89.   free(q);
  90. }
  91. static stack_queue *stack_new(void) { return queue_new(); }
  92. static bool stack_is_empty(stack_queue *s) { return queue_is_empty(s); }
  93. static void stack_push(stack_queue *s, tree *t) {
  94.   if (!t) { return; }
  95.   entry *new = entry_new(t);
  96.   if (stack_is_empty(s)) {
  97.     s->head = s->tail = new;
  98.     return;
  99.   }
  100.   new->next = s->head, s->head = new;
  101. }
  102. static tree *stack_pop(stack_queue *s) { return queue_dequeue(s); }
  103. static void stack_free(stack_queue *s) { queue_free(s); }
  104. static tree *bfs(tree *t, int x) {
  105.   stack_queue *q = queue_new();
  106.   tree *found;
  107.   for (queue_enqueue(q, t); !queue_is_empty(q);
  108.        queue_enqueue(q, found->left), queue_enqueue(q, found->right)) {
  109.     found = queue_dequeue(q), printf("%d... ", found->data);
  110.     if (found->data == x) {
  111.       puts("!"), queue_free(q);
  112.       return found;
  113.     }
  114.   }
  115.   puts(""), queue_free(q);
  116.   return NULL;
  117. }
  118. static tree *dfs(tree *t, int x) {
  119.   stack_queue *s = stack_new();
  120.   tree *found;
  121.   for (stack_push(s, t); !stack_is_empty(s);
  122.        stack_push(s, found->left), stack_push(s, found->right)) {
  123.     found = stack_pop(s), printf("%d... ", found->data);
  124.     if (found->data == x) {
  125.       puts("!"), stack_free(s);
  126.       return found;
  127.     }
  128.   }
  129.   puts(""), stack_free(s);
  130.   return NULL;
  131. }
  132. int main(void) {
  133.   int n = 15, max = 100, x = 0;
  134.   tree *root = NULL;
  135.   for (int i = 0, r = randint(max); i < n; ++i, r = randint(max)) {
  136.     if (i == n / 2) { x = r; }
  137.     root = tree_insert(root, r);
  138.   }
  139.   tree_inorder(root), puts("");
  140.   tree_show(root, 0);
  141.   printf("BFS(%d): ", x), bfs(root, x);
  142.   printf("DFS(%d): ", x), dfs(root, x);
  143.   printf("BFS(%d): ", max), bfs(root, max);
  144.   printf("DFS(%d): ", max), dfs(root, max);
  145.   tree_free(root);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement