aboutsummaryrefslogtreecommitdiff
path: root/src/treeutil.h
blob: a68ff35cc280a65bf4ed4d7f66f2122e263a2282 (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
// aux display and verification routines, helpful but not essential
struct trunk {
	struct trunk *prev;
	const char   *str;
};

static void
show_trunks(struct trunk *p)
{
	if ( !p )
		return;
	show_trunks(p->prev);
	printf("%s", p->str);
}

// this is very haphazzard
static void
show_tree(struct tree_node *root, struct trunk *prev, int is_left)
{
	if ( root == NULL )
		return;

	struct trunk this_disp = { prev, "    " };
	const char  *prev_str  = this_disp.str;
	show_tree(root->right, &this_disp, 1);

	if ( !prev )
		this_disp.str = "---";
	else if ( is_left ) {
		this_disp.str = ".--";
		prev_str      = "   |";
	}
	else {
		this_disp.str = "`--";
		prev->str     = prev_str;
	}

	show_trunks(&this_disp);
	if ( root->key >= 'A' && root->key <= 'Z' )
		printf("%c\n", root->key);
	else
		printf("%d\n", root->key);

	if ( prev )
		prev->str = prev_str;
	this_disp.str = "   |";

	show_tree(root->left, &this_disp, 0);
	if ( !prev )
		puts("");
}