-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree_create_traverse.cpp
More file actions
272 lines (236 loc) · 5.58 KB
/
tree_create_traverse.cpp
File metadata and controls
272 lines (236 loc) · 5.58 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <iostream>
using namespace std;
class treenode // create a class for single node of tree
{
char data;
treenode *left;
treenode *right;
friend class tree;
};
class stack // creating a stack class instead of using the default class as
// instructed by the professor
{
int top;
treenode *data[30];
friend class tree;
public:
stack() { top = -1; }
void push(treenode *temp) {
top++;
data[top] = temp;
}
treenode *pop() {
if (!empty()) {
treenode *ele = data[top];
top--;
return ele;
} else
return nullptr;
}
bool empty() {
if (top < 0)
return true;
else
return false;
}
};
class tree // creating a class for tree
{
treenode *root;
public:
tree() // initailizing root to null
{
root = NULL;
}
// creating a tree recursively
void create_r() // initializing root and driver function for recursive
// function call
{
root = new treenode();
cout << "Enter Data To Be Put In Root" << endl;
cin >> root->data;
root->left = root->right = NULL;
create_r(root);
}
void create_r(treenode *root) {
char ch;
treenode *ptr;
cout << "Do You Want To Attach Node on Left or Right (L/R) " << endl;
cin >> ch;
if (ch == 'L') {
ptr = new treenode();
cin >> ptr->data;
ptr->left = ptr->right = NULL;
root->left = ptr;
create_r(ptr);
}
if (ch == 'R') {
ptr = new treenode();
cin >> ptr->data;
ptr->left = ptr->right = NULL;
root->right = ptr;
create_r(ptr);
} else
cout << "Invalid Input" << endl;
}
// creating a tree non-recursively
void create_nr() {
treenode *temp;
char ch;
if (root == NULL) {
root = new treenode();
cout << "Enter Data To Be Put In Root" << endl;
cin >> root->data;
root->left = root->right = NULL;
}
do {
temp = root;
int flag = 0;
while (flag == 0) {
cout << "Do You Want To Attach Node on Left or Right (L/R) " << endl;
cin >> ch;
if (ch == 'L') {
if (temp->left == NULL) {
temp->left = new treenode();
cout << "Enter Data For Left Child" << endl;
cin >> temp->left->data;
temp->left->left = temp->left->right = NULL;
flag = 1;
}
temp = temp->left;
} else if (ch == 'R') {
if (temp->right == NULL) {
temp->right = new treenode();
cout << "Enter Data For Right Child" << endl;
cin >> temp->right->data;
temp->right->left = temp->right->right = NULL;
flag = 1;
}
temp = temp->right;
}
}
cout << "Do You Want To Continue (Y/N) " << endl;
cin >> ch;
} while (ch == 'Y');
}
// postorder traversal recursively
void postorder() { postorder(root); }
void postorder(treenode *temp) {
if (temp != NULL) {
postorder(temp->left);
postorder(temp->right);
cout << temp->data << endl;
}
}
// inorder traversal recursively
void inorder() { inorder(root); }
void inorder(treenode *temp) {
if (temp != NULL) {
inorder(temp->left);
cout << temp->data << endl;
inorder(temp->right);
}
}
// preorder traversal recursively
void preorder() { preorder(root); }
void preorder(treenode *temp) {
if (temp != NULL) {
cout << temp->data << endl;
preorder(temp->left);
preorder(temp->right);
}
}
// inorder traversal non-recursively
void inorder_nr() {
treenode *temp = root;
stack s;
while (1) {
while (temp != NULL) {
s.push(temp);
temp = temp->left;
}
if (s.empty())
break;
temp = s.pop();
cout << temp->data << endl;
temp = temp->right;
}
}
// preorder traversal non-recursively
void preorder_nr() {
treenode *temp = root;
stack s;
while (1) {
while (temp != NULL) {
cout << temp->data << endl;
s.push(temp);
temp = temp->left;
}
if (s.empty())
break;
temp = s.pop();
temp = temp->right;
}
}
// postorder traversal non-recursively
void postorder_nr() {
treenode *temp = root;
stack s;
while (1) {
while (temp != NULL) {
s.push(temp);
temp = temp->left;
}
if (s.data[s.top]->right == NULL) {
temp = s.pop();
cout << temp->data << endl;
}
while (!s.empty() && s.data[s.top]->right == temp) {
temp = s.pop();
cout << temp->data << endl;
}
if (s.empty())
break;
temp = s.data[s.top]->right;
}
}
};
int main() {
tree bt;
int create_choice;
cout << "Enter 1 to create tree recursively,2 to create non-recursively"
<< endl;
cin >> create_choice;
switch (create_choice) {
case 1:
bt.create_r();
break;
case 2:
bt.create_nr();
break;
default:
cout << "Invalid Input" << endl;
break;
}
int trav_choice;
cout << "Enter 1 to traverse tree recursively,2 to traverse non-recursively"
<< endl;
cin >> trav_choice;
switch (trav_choice) {
case 1:
cout << "Preorder Traversal" << endl;
bt.preorder();
cout << "Inorder Traversal" << endl;
bt.inorder();
cout << "Postorder Traversal" << endl;
break;
case 2:
cout << "Inorder Traversal Non recursive" << endl;
bt.inorder_nr();
cout << "Preorder Traversal Non recursive" << endl;
bt.preorder_nr();
cout << "Postorder Traversal Non recursive" << endl;
bt.postorder_nr();
}
return 0;
}