Summary Files Commits Branches @commit @diff @stats
path: root / src / blog / sane-explanation-of-compilers / part-1 / proper / tree.c
blob: 6119d5c2b7a78380cb5acf7087ea1ae589ea925c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "tree.h"

node* newNode(char val, node *left, node *right) {
	node* out = malloc(sizeof(node));
	out->val = val;
	out->left = left;
	out->right = right;
	return out;
}

void printTree(node *n, int level) {
	if (n == NULL) return;
	// %*s will add level amounts of spaces (but with the "" string)
	printf("%*s|-%c\n", level, "", n->val);
	printTree(n->left, level + 1);
	printTree(n->right, level + 1);
}

void freeNode(node *n) {
	if (n == NULL) return;
	freeNode(n->left);
	freeNode(n->right);
	free(n);
}