-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pre-Post-Inorder Traversal.c
102 lines (90 loc) · 1.58 KB
/
Pre-Post-Inorder Traversal.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newnode(int data)
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void preorderprinting(struct node* root)
{
if(root!=NULL)
{
printf("%d\n",root->data);
preorderprinting(root->left);
preorderprinting(root->right);
}
}
void postorderprinting(struct node* root)
{
if(root!=NULL)
{
postorderprinting(root->left);
postorderprinting(root->right);
printf("%d\n",root->data);
}
}
void inorderprinting(struct node* root)
{
if (root != NULL)
{
inorderprinting(root->left);
printf("%d \n", root->data);
inorderprinting(root->right);
}
}
int main()
{
int i,j,n,m,f,q;
struct node* temp;
struct node* s;
struct node* g;
struct node* node;
scanf("%d",&n);
scanf("%d",&m);
s=newnode(m);
for(i=1;i<n;i++)
{
scanf("%d",&m);
temp=newnode(m);
node = s;
while(node!=NULL)
{
if(node->data<temp->data)
{
g = node;
node = node->right;
f=0;
}
else if(node->data>temp->data)
{
g=node;
node = node->left;
f=1;
}
}
if(f==1)
g->left = temp;
else
g->right=temp;
}
printf("1:for inorder traversal\n");
printf("2:for preorder traversal\n");
printf("3:for postorder traversal\n");
scanf("%d",&q);
if(q==1)
inorderprinting(s);
else if(q==2)
preorderprinting(s);
else if(q==3)
postorderprinting(s);
}