summaryrefslogtreecommitdiff
path: root/src/02/01/binary_tree.c
blob: 16eec3d64b356fa3d0c78d93f7fefc348d5638af (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
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
#include "binary_tree.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * Initialize a new node for a Binary Tree.
 *
 * @param data the data to assign to the root of the tree.
 * @return The root of the binary tree.
 */
Node *initialize(int data) {
  Node *item = malloc(sizeof(Node));
  item->data = data;
  item->left = NULL;
  item->right = NULL;
  return item;
}

/*
 * Traverses a binary tree using the traversal algorithm specified.
 * Time: O(n)
 * Space: O(1)
 *
 * @param node The root of the binary tree
 * @param vistior A callback function to invoke on each node during the tree
 * traversal
 * @param traversal Specifies what type of traversal to perform
 */
void traverse(Node *node, Visitor visitor, enum Traversal traversal) {
  if (!node)
    return;

  switch (traversal) {
  case PREORDER:
    visitor(node);
    traverse(node->left, visitor, traversal);
    traverse(node->right, visitor, traversal);
    break;
  case INORDER:
    traverse(node->left, visitor, traversal);
    visitor(node);
    traverse(node->right, visitor, traversal);
    break;
  case POSTORDER:
    traverse(node->left, visitor, traversal);
    traverse(node->right, visitor, traversal);
    visitor(node);
    break;
  default:
    visitor(node);
    break;
  }
}

/**
 * Frees the memory allocated for a node in a tree
 *
 * @param node The node in the binary tree to free
 */
static void destructor(Node *node) { free(node); }

/**
 * Frees the memory associated with each node in a binary tree.
 *
 * @param root The root of the tree
 */
void destroy(Node *root) { traverse(root, destructor, POSTORDER); }

/**
 * A helper method to print out a visual representation of the tree
 *
 * @param node A node in a binary tree.
 * @param level The level in the tree that the node is at
 */
void inspect(Node *node, int level) {
  if (!node)
    return;

  for (int i = 0; i < level; i++)
    printf("  ");
  printf("(%d)\n", node->data);
  inspect(node->left, level + 1);
  inspect(node->right, level + 1);
}