-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
63 lines (57 loc) · 1.34 KB
/
tree.c
File metadata and controls
63 lines (57 loc) · 1.34 KB
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
#include "tree.h"
tnode *new_tree_node(int value)
{
tnode *nnode = (tnode *)malloc(sizeof(tnode));
nnode->left = NULL;
nnode->right = NULL;
nnode->value = value;
return nnode;
}
tree *new_tree()
{
tree *ntree = (tree *)malloc(sizeof(tree));
ntree->root = NULL;
return ntree;
}
void add_tree_node(tree *tree, tnode *current_root, int value)
{
if (tree->root == NULL)
{
tree->root = new_tree_node(value);
return;
}
if (value < current_root->value)
{
if (current_root->left != NULL)
return add_tree_node(tree, current_root->left, value);
current_root->left = new_tree_node(value);
return;
}
if (current_root->right != NULL)
return add_tree_node(tree, current_root->right, value);
current_root->right = new_tree_node(value);
}
void print_preorder(tnode *node)
{
if (node == NULL)
return;
printf("%d ", node->value);
print_preorder(node->left);
print_preorder(node->right);
}
void print_inorder(tnode *node)
{
if (node == NULL)
return;
print_inorder(node->left);
printf("%d ", node->value);
print_inorder(node->right);
}
void print_postorder(tnode *node)
{
if (node == NULL)
return;
print_postorder(node->left);
print_postorder(node->right);
printf("%d ", node->value);
}