-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathtreeTraversal.c
60 lines (47 loc) · 965 Bytes
/
treeTraversal.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
60
// to run type "gcc treeTraversal.c tree.c stack.c"
// then run the executable file and enter your input
#include <stdio.h>
#include "tree.h"
#include "stack.h"
void printstack(node *stack)
{
printf("stack: ");
while (!isempty(stack))
{
printf("%p ", stack->top->data);
}
printf("\n");
}
int main()
{
tree *root = NULL;
int n, val;
printf("Enter number of nodes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter node %d: ", i+1);
scanf("%d", &val);
root = insert(root, val);
}
//using binary search tree.
//for an input of 2 1 4 3 5
// 2
// / \
// 1 3
// / \
// 4 5
// inorder: 1 2 3 4 5
// preorder: 2 1 4 3 5
// postorder: 1 3 5 4 2
printf("inorder: ");
inorder(root);
printf("\n");
printf("preorder: ");
preorder(root);
printf("\n");
printf("postorder: ");
postorder(root);
printf("\n");
return 0;
}