-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
320 lines (250 loc) · 10.4 KB
/
code_1.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// code_1.cpp
// Data Structure
//
// Created by Mohd Shoaib Rayeen on 19/03/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include<iostream>
using namespace std;
struct bnode {
/*
------------------------------------------------------------------------------------------------
objective : creating structure for binary tree
------------------------------------------------------------------------------------------------
input parameter : none
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : structure defines a node structure which contains data ( int type ) and
left and right ( bnode types for storing address of left and right elements )
------------------------------------------------------------------------------------------------
*/
int data;
bnode *left;
bnode *right;
};
class btree {
/*
------------------------------------------------------------------------------------------------
objective : binary tree class
------------------------------------------------------------------------------------------------
input parameter : none
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : class defines constructor and functions which can be accessed publicly such as :-
1. btree() for initializing the pointer
2. insert() for insertion in binary tree
3. display() for displaying binary tree
6. newNode() for creating new node
7. preorder() for displaying preorder of tree
8. postorder() for displaying postorder of tree
9. inorder() for displaying inorder of tree
10. creating root node for binary tree
11. arr[] for storing path and pathlen for storing length of path for every leaf node
------------------------------------------------------------------------------------------------
*/
public:
bnode *root;
btree();
void insert();
void display();
bnode* newNode(int);
void preorder(bnode*);
void postorder(bnode*);
void inorder(bnode*);
void reverseLevelOrder(bnode*);
int height(bnode*);
void printLevel(bnode* , int);
};
btree::btree() {
/*
------------------------------------------------------------------------------------------------
objective : initializing root as NULL and pathlen as zero
------------------------------------------------------------------------------------------------
input parameter : none
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : assigning NULL to root and 0 to pathlen
------------------------------------------------------------------------------------------------
*/
root = NULL;
}
bnode* btree:: newNode(int value) {
/*
------------------------------------------------------------------------------------------------
objective : creating newnode and assigning it to given value
------------------------------------------------------------------------------------------------
input parameter : value which has to inserted into binary tree
------------------------------------------------------------------------------------------------
output parameter : new node which is created
------------------------------------------------------------------------------------------------
approach : creating a bnode type node and assiging its left and right as NULL and data as value
------------------------------------------------------------------------------------------------
*/
bnode* temp=new bnode;
temp->data=value;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void btree:: insert() {
/*
------------------------------------------------------------------------------------------------
objective : insertion in binary tree
------------------------------------------------------------------------------------------------
input parameter : none
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : inserting nodes from root , then its right & left and so on
------------------------------------------------------------------------------------------------
*/
/*
root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->right->left = newNode(8);
root->left->right->left = newNode(9);
*/
root = newNode(20);
root->left = newNode(8);
root->left->left = newNode(4);
root->left->right = newNode(12);
root->left->right->left = newNode(10);
root->left->right->right = newNode(14);
root->right = newNode(22);
root->right->right = newNode(25);
}
void btree::display() {
/*
------------------------------------------------------------------------------------------------
objective : displaying binary tree and path
------------------------------------------------------------------------------------------------
input parameter : none
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : calling insert() , inorder() , preorder() , postorder and path() functions
------------------------------------------------------------------------------------------------
*/
insert();
cout << "\nInorder\t\t\t:\t";
inorder(root);
cout << "\nPostorder\t\t:\t";
postorder(root);
cout << "\nPreorder\t\t:\t";
preorder(root);
cout << "\nReverse Level Order\t:\t";
reverseLevelOrder(root);
}
void btree :: postorder(bnode* root) {
/*
------------------------------------------------------------------------------------------------
objective : displaying tree in postorder recursively
------------------------------------------------------------------------------------------------
input parameter : root of tree
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : calling this function recursively in following sequence :-
- left , right and then value of pointer
------------------------------------------------------------------------------------------------
*/
if (root == NULL) {
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << "\t";
}
void btree :: preorder(bnode* root) {
/*
------------------------------------------------------------------------------------------------
objective : displaying tree in preorder recursively
------------------------------------------------------------------------------------------------
input parameter : root of tree
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : calling this function recursively in following sequence :-
- value of pointer , left and right
------------------------------------------------------------------------------------------------
*/
if (root == NULL) {
return;
}
cout << root->data << "\t";
preorder(root->left);
preorder(root->right);
}
void btree :: inorder(bnode* root) {
/*
------------------------------------------------------------------------------------------------
objective : displaying tree in inorder recursively
------------------------------------------------------------------------------------------------
input parameter : root of tree
------------------------------------------------------------------------------------------------
output parameter : none
------------------------------------------------------------------------------------------------
approach : calling this function recursively in following sequence :-
- left , value of pointer and right
------------------------------------------------------------------------------------------------
*/
if (root == NULL) {
return;
}
preorder(root->left);
cout << root->data << "\t";
preorder(root->right);
}
void btree::reverseLevelOrder(bnode* root)
{
int high = height(root);
for (int i = high; i >= 1; i--) {
printLevel(root, i);
}
}
void btree::printLevel(bnode* root, int level)
{
if (root == NULL) {
return;
}
if (level == 1) {
cout << root->data << "\t";
}
else if (level > 1)
{
printLevel(root->left, level - 1);
printLevel(root->right, level - 1);
}
}
int btree::height(bnode* node)
{
if (node == NULL) {
return 0;
}
else
{
int leftHeight = height(node->left);
int rightHeight = height(node->right);
if (leftHeight > rightHeight) {
return(leftHeight + 1);
}
else {
return(rightHeight + 1);
}
}
}
int main() {
btree obj;
obj.display();
cout << "\n";
return 0;
}