-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree_traversal.c
163 lines (155 loc) · 3.47 KB
/
tree_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<stdio.h>
#include<stdlib.h>
//8+8/2*3+8/2=23
//queue functions
/* L I
1 12 N
2 3 6 11
4 5 6 7 3 5
8 9 1 1 1 1 1 1 1 3
1
1 1
1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
struct node
{
struct node* next;
struct t_node* data;
};
struct node* new_qNode(struct t_node* x)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->next=NULL;
temp->data=x;
return temp;
}
//queue functions
//trees function
struct t_node
{
struct t_node* left;
struct t_node* right;
int data;
};
struct t_node* new_tNode(int x)
{
struct t_node* temp=(struct t_node*)malloc(sizeof(struct t_node));
temp->left=temp->right=NULL;
temp->data=x;
return temp;
}
void Inorder(struct t_node* root)
{//LNR
if(!root)
return;
Inorder(root->left);
printf("%d ",root->data);
Inorder((root->right));
}
void Preorder(struct t_node* root)
{
//NLR
if(!root)
return;
printf("%d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
void Postorder(struct t_node* root)
{
if(!root)
return;
Postorder(root->left);
Postorder((root->right));
printf("%d ",root->data);
}
/*
1. 1
2. 2 3
3.4 5
Inorder:4 2 5 1 3
*/
//trees function
int main()
{
printf("NOTE:For tree input take -1 as NULL\n\n");
struct t_node* root=NULL;
int x,lc,rc;
printf("Enter root node data: ");
scanf("%d",&x);
root=new_tNode(x);
//queue
struct node* head=new_qNode(root);
struct node* tail=head;//rear pointer
while(head)
{
struct t_node* temp=head->data;
printf("Enter left child of %d: ",temp->data);
scanf("%d",&lc);
if(lc!=-1)
{
//tree Linking Nodes
temp->left=new_tNode(lc);
//tree
//queue
tail->next=new_qNode(temp->left);
tail=tail->next;
//queue
}
printf("Enter right child of %d: ",temp->data);
scanf("%d",&rc);
if(rc!=-1)
{
//tree linking
temp->right=new_tNode(rc);
//tree
//queue
tail->next=new_qNode(temp->right);
tail=tail->next;
//queue
}
struct node* t=head;
head=head->next;
free(t);
}
int c=1,choice;
while(c)
{
printf("1.In-order Traversal\n2.Pre-order Traversal\n3.Post-order traversal\n4.Exit\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("In-order Traversal: ");
Inorder(root);
break;
}
case 2:
{
printf("Pre-order Traversal: ");
Preorder(root);
break;
}
case 3:
{
printf("Post-order Traversal: ");
Postorder(root);
break;
}
case 4:
{
c=0;
break;
}
default:
{
printf("Invalid choice!!!!Try again!!!!\n");
}
}
printf("\n");
}
return 0;
}