-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathtree.c
59 lines (53 loc) · 849 Bytes
/
tree.c
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
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
tree *getTreeNode(int data)
{
tree *nn = (tree *)malloc(sizeof(tree));
nn->left = nn->right = NULL;
nn->data = data;
return nn;
}
tree *insert(tree *root, int data)
{
if (!root)
{
return getTreeNode(data);
}
else if (root->data < data)
{
root->right = insert(root->right, data);
}
else
{
root->left = insert(root->left, data);
}
return root;
}
void inorder(tree *root)
{
if (root)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(tree *root)
{
if (root)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(tree *root)
{
if (root)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}