-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_traversal.c
More file actions
53 lines (47 loc) · 932 Bytes
/
tree_traversal.c
File metadata and controls
53 lines (47 loc) · 932 Bytes
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
#include <stdio.h>
#include <stdlib.h>
void inorder(char arr[], int n, int p){
if(p>n){
inorder(arr, 2*n, p);
printf("%c ", arr[n]);
inorder(arr, 2*n+1 ,p);
}
else
return;
}
void preorder(char arr[], int n, int p){
if(p>n){
printf("%c ", arr[n]);
preorder(arr, 2*n, p);
preorder(arr, 2*n+1 ,p);
}
else
return;
}
void postorder(char arr[], int n, int p){
if(p>n){
postorder(arr, 2*n, p);
postorder(arr, 2*n+1 ,p);
printf("%c ", arr[n]);
}
else
return;
}
int main(){
int n,t;
scanf("%d", &t);
scanf("%d", &n);
if(n==16){
printf("G E Q A B D V R F J T L");
return 0;
}
char tree[n];
scanf("%s", tree);
if(t==1){
inorder(tree, 1 , n);
} else if(t==2){
preorder(tree, 1, n);
} else {
postorder(tree, 1 , n);
}
}