summaryrefslogtreecommitdiff
path: root/src/02/01/main.c
blob: ae403e7143dc0befed73354ffe5e6eb5ef6b4a88 (plain)
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
#include "binary_tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static Node *nodes[32];
static int visited_count;

static void visitor(Node *node) {
  nodes[visited_count] = node;
  visited_count++;
}

void print_traversal(Node *tree, enum Traversal direction) {
  visited_count = 0;
  memset(nodes, 0, sizeof(nodes));

  traverse(tree, visitor, direction);

  for (int i = 0; i < visited_count; i++)
    printf("%d ", nodes[i]->data);
  printf("\n");
}

int main(int argc, char *argv[]) {
  printf("=== COMP-272 - Assignment 02 - Question 01 ===\n");

  Node *a = initialize(100);
  Node *b = initialize(200);
  Node *c = initialize(300);
  Node *d = initialize(400);
  Node *e = initialize(500);

  a->left = b;
  a->right = c;
  b->left = d;
  b->right = e;
  inspect(a, 0);

  printf("\n=== Pre order traversal ===\n");
  print_traversal(a, PREORDER);

  printf("\n=== In order traversal ===\n");
  print_traversal(a, INORDER);

  printf("\n=== Post order traversal ===\n");
  print_traversal(a, POSTORDER);

  return 0;
}