-
Notifications
You must be signed in to change notification settings - Fork 0
/
traversals.c
76 lines (62 loc) · 1.27 KB
/
traversals.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include<stdlib.h>
int count=0;
struct node {
int d;
struct node *l;
struct node *r;
};
struct node *root;
struct node *newNode(int a) {
struct node *new = (struct node *)malloc(sizeof(struct node));
new->d = a;
new->l = NULL;
new->r = NULL;
return new;
}
void printPreorder(struct node *root) {
//struct node *temp = root;
if(root != NULL) {
printf("%d ", root->d);
count++;
printPreorder(root->l);
printPreorder(root->r);
}
}
void printInorder(struct node *root) {
//struct node *temp = root;
if(root != NULL) {
printInorder(root->l);
printf("%d ", root->d);
printInorder(root->r);
}
}
void printPostorder(struct node *root) {
//struct node *temp = root;
if(root != NULL) {
printPostorder(root->l);
printPostorder(root->r);
printf("%d ", root->d);
}
}
int main(void) {
root = NULL;
root = newNode(1);
root->l = newNode(2);
root->r = newNode(3);
root->l->l = newNode(4);
root->l->r = newNode(5);
root->r->r = newNode(6);
//printPreorder(root);
printf("Preorder traversal : ");
printPreorder(root);
printf("\n\n");
printf("%d\n",count);
printf("In order traversal : ");
printInorder(root);
printf("\n\n");
printf("Post order traversal : ");
printPostorder(root);
printf("\n\n");
return 0;
}