forked from Priyadarshan2000/Hacktoberfest_2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree_order.c
107 lines (100 loc) · 2.15 KB
/
Tree_order.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
103
104
105
106
107
#include<stdio.h>
#include<stdlib.h>
struct tree
{
char key;
struct tree *left;
struct tree *right;
};
typedef struct tree tree;
void create(tree *root);
void preorder(tree *root);
void inorder(tree *root);
void postorder(tree *root);
main()
{
int c;
tree* root;
root=(tree*)malloc(sizeof(tree));
do
{
printf("\n1. Create ");
printf("\n2. Pre-order ");
printf("\n3. In-Order ");
printf("\n4. Post-order ");
printf("\n5. Exit ");
printf("\nEnter Your Choice ::");
scanf("%d",&c);
switch(c)
{
case 1:
create(root);
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
}
}while(c!=5);
}
void create(tree *root)
{
char u;
fflush(stdin);
printf("\nKey ::");
scanf("%c",&root->key);
fflush(stdin);
printf("\nDo you want to add left child of %c ::",root->key);
scanf("%c",&u);
if(u=='y' || u=='Y')
{
root->left=(tree*)malloc(sizeof(tree));
create(root->left);
}
else
root->left=NULL;
fflush(stdin);
printf("\nDo you want to add right child of %c ::",root->key);
scanf("%c",&u);
if(u=='y' || u=='Y')
{
root->right=(tree*)malloc(sizeof(tree));
create(root->right);
}
else
{
root->right=NULL;
}
}
void preorder(tree *root)
{
if(root!=NULL)
{
printf("%c",root->key);
preorder(root->left);
preorder(root->right);
}
}
void inorder(tree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%c",root->key);
inorder(root->right);
}
}
void postorder(tree *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%c",root->key);
}
}