52
52
*/
53
53
54
54
#include < cassert> // / for assert
55
+ #include < cstdint> // / for std::uint64_t
55
56
#include < iostream> // / for I/O operations
56
57
#include < vector> // / for vector
57
58
@@ -76,33 +77,34 @@ namespace recursive_tree_traversals {
76
77
* @param right follow up right subtree.
77
78
*/
78
79
struct Node {
79
- uint64_t data = 0 ; // /< The value/key of the node.
80
- struct Node *left{}; // /< struct pointer to left subtree.
81
- struct Node *right{}; // /< struct pointer to right subtree.
80
+ std:: uint64_t data = 0 ; // /< The value/key of the node.
81
+ struct Node *left{}; // /< struct pointer to left subtree.
82
+ struct Node *right{}; // /< struct pointer to right subtree.
82
83
};
83
84
/* *
84
85
* @brief BT used to make the entire structure of the binary tree and the
85
86
* functions associated with the binary tree
86
87
*/
87
88
class BT {
88
89
public:
89
- std::vector<uint64_t >
90
+ std::vector<std:: uint64_t >
90
91
inorder_result; // vector to store the inorder traversal of the tree.
91
- std::vector<uint64_t >
92
+ std::vector<std:: uint64_t >
92
93
preorder_result; // vector to store the preorder traversal of the tree.
93
- std::vector<uint64_t > postorder_result; // vector to store the preorder
94
- // traversal of the tree.
94
+ std::vector<std::uint64_t >
95
+ postorder_result; // vector to store the preorder
96
+ // traversal of the tree.
95
97
96
98
Node *createNewNode (
97
- uint64_t ); // function that will create new node for insertion.
99
+ std:: uint64_t ); // function that will create new node for insertion.
98
100
99
- std::vector<uint64_t > inorder (
101
+ std::vector<std:: uint64_t > inorder (
100
102
Node *); // function that takes root of the tree as an argument and
101
103
// returns its inorder traversal.
102
- std::vector<uint64_t > preorder (
104
+ std::vector<std:: uint64_t > preorder (
103
105
Node *); // function that takes root of the tree as an argument and
104
106
// returns its preorder traversal.
105
- std::vector<uint64_t > postorder (
107
+ std::vector<std:: uint64_t > postorder (
106
108
Node *); // function that takes root of the tree as an argument and
107
109
// returns its postorder traversal.
108
110
};
@@ -113,7 +115,7 @@ class BT {
113
115
* @param data value that a particular node will contain.
114
116
* @return pointer to the newly created node with assigned data.
115
117
*/
116
- Node *BT::createNewNode (uint64_t data) {
118
+ Node *BT::createNewNode (std:: uint64_t data) {
117
119
Node *node = new Node ();
118
120
node->data = data;
119
121
node->left = node->right = nullptr ;
@@ -127,7 +129,7 @@ Node *BT::createNewNode(uint64_t data) {
127
129
* @param root head/root node of a tree
128
130
* @return result that is containing the inorder traversal of a tree
129
131
**/
130
- std::vector<uint64_t > BT::inorder (Node *root) {
132
+ std::vector<std:: uint64_t > BT::inorder (Node *root) {
131
133
if (root == nullptr ) { // return if the current node is empty
132
134
return {};
133
135
}
@@ -147,7 +149,7 @@ std::vector<uint64_t> BT::inorder(Node *root) {
147
149
* @param root head/root node of a tree
148
150
* @return result that is containing the preorder traversal of a tree
149
151
*/
150
- std::vector<uint64_t > BT::preorder (Node *root) {
152
+ std::vector<std:: uint64_t > BT::preorder (Node *root) {
151
153
if (root == nullptr ) { // if the current node is empty
152
154
return {};
153
155
}
@@ -167,7 +169,7 @@ std::vector<uint64_t> BT::preorder(Node *root) {
167
169
* @param root head/root node of a tree
168
170
* @return result that is containing the postorder traversal of a tree
169
171
*/
170
- std::vector<uint64_t > BT::postorder (Node *root) {
172
+ std::vector<std:: uint64_t > BT::postorder (Node *root) {
171
173
if (root == nullptr ) { // if the current node is empty
172
174
return {};
173
175
}
@@ -180,6 +182,14 @@ std::vector<uint64_t> BT::postorder(Node *root) {
180
182
return postorder_result;
181
183
}
182
184
185
+ void deleteAll (const Node *const root) {
186
+ if (root) {
187
+ deleteAll (root->left );
188
+ deleteAll (root->right );
189
+ delete root;
190
+ }
191
+ }
192
+
183
193
} // namespace recursive_tree_traversals
184
194
185
195
} // namespace others
@@ -200,17 +210,23 @@ void test1() {
200
210
root->left ->right ->right = obj1.createNewNode (11 );
201
211
root->right ->right ->left = obj1.createNewNode (4 );
202
212
203
- std::vector<uint64_t > actual_result_inorder{2 , 7 , 5 , 6 , 11 , 2 , 5 , 4 , 9 };
204
- std::vector<uint64_t > actual_result_preorder{2 , 7 , 2 , 6 , 5 , 11 , 5 , 9 , 4 };
205
- std::vector<uint64_t > actual_result_postorder{2 , 5 , 11 , 6 , 7 , 4 , 9 , 5 , 2 };
206
- std::vector<uint64_t > result_inorder; // /< result stores the inorder
207
- // /< traversal of the binary tree
208
- std::vector<uint64_t > result_preorder; // /< result stores the preorder
209
- // /< traversal of the binary tree
210
- std::vector<uint64_t > result_postorder; // /< result stores the postorder
211
- // /< traversal of the binary tree
212
-
213
- uint64_t size = actual_result_inorder.size ();
213
+ std::vector<std::uint64_t > actual_result_inorder{2 , 7 , 5 , 6 , 11 ,
214
+ 2 , 5 , 4 , 9 };
215
+ std::vector<std::uint64_t > actual_result_preorder{2 , 7 , 2 , 6 , 5 ,
216
+ 11 , 5 , 9 , 4 };
217
+ std::vector<std::uint64_t > actual_result_postorder{2 , 5 , 11 , 6 , 7 ,
218
+ 4 , 9 , 5 , 2 };
219
+ std::vector<std::uint64_t >
220
+ result_inorder; // /< result stores the inorder
221
+ // /< traversal of the binary tree
222
+ std::vector<std::uint64_t >
223
+ result_preorder; // /< result stores the preorder
224
+ // /< traversal of the binary tree
225
+ std::vector<std::uint64_t >
226
+ result_postorder; // /< result stores the postorder
227
+ // /< traversal of the binary tree
228
+
229
+ std::uint64_t size = actual_result_inorder.size ();
214
230
215
231
// Calling inorder() function by passing a root node,
216
232
// and storing the inorder traversal in result_inorder.
@@ -240,6 +256,7 @@ void test1() {
240
256
std::cout << " Passed!" << std::endl;
241
257
242
258
std::cout << std::endl;
259
+ deleteAll (root);
243
260
}
244
261
245
262
/* *
@@ -257,17 +274,20 @@ void test2() {
257
274
root->right ->left ->left = obj2.createNewNode (7 );
258
275
root->right ->left ->right = obj2.createNewNode (8 );
259
276
260
- std::vector<uint64_t > actual_result_inorder{4 , 2 , 1 , 7 , 5 , 8 , 3 , 6 };
261
- std::vector<uint64_t > actual_result_preorder{1 , 2 , 4 , 3 , 5 , 7 , 8 , 6 };
262
- std::vector<uint64_t > actual_result_postorder{4 , 2 , 7 , 8 , 5 , 6 , 3 , 1 };
263
- std::vector<uint64_t > result_inorder; // /< result stores the inorder
264
- // /< traversal of the binary tree
265
- std::vector<uint64_t > result_preorder; // /< result stores the preorder
266
- // /< traversal of the binary tree
267
- std::vector<uint64_t > result_postorder; // /< result stores the postorder
268
- // /< traversal of the binary tree
269
-
270
- uint64_t size = actual_result_inorder.size ();
277
+ std::vector<std::uint64_t > actual_result_inorder{4 , 2 , 1 , 7 , 5 , 8 , 3 , 6 };
278
+ std::vector<std::uint64_t > actual_result_preorder{1 , 2 , 4 , 3 , 5 , 7 , 8 , 6 };
279
+ std::vector<std::uint64_t > actual_result_postorder{4 , 2 , 7 , 8 , 5 , 6 , 3 , 1 };
280
+ std::vector<std::uint64_t >
281
+ result_inorder; // /< result stores the inorder
282
+ // /< traversal of the binary tree
283
+ std::vector<std::uint64_t >
284
+ result_preorder; // /< result stores the preorder
285
+ // /< traversal of the binary tree
286
+ std::vector<std::uint64_t >
287
+ result_postorder; // /< result stores the postorder
288
+ // /< traversal of the binary tree
289
+
290
+ std::uint64_t size = actual_result_inorder.size ();
271
291
272
292
// Calling inorder() function by passing a root node,
273
293
// and storing the inorder traversal in result_inorder.
@@ -297,6 +317,7 @@ void test2() {
297
317
std::cout << " Passed!" << std::endl;
298
318
299
319
std::cout << std::endl;
320
+ deleteAll (root);
300
321
}
301
322
302
323
/* *
@@ -311,17 +332,20 @@ void test3() {
311
332
root->left ->left = obj3.createNewNode (4 );
312
333
root->left ->right = obj3.createNewNode (5 );
313
334
314
- std::vector<uint64_t > actual_result_inorder{4 , 2 , 5 , 1 , 3 };
315
- std::vector<uint64_t > actual_result_preorder{1 , 2 , 4 , 5 , 3 };
316
- std::vector<uint64_t > actual_result_postorder{4 , 5 , 2 , 3 , 1 };
317
- std::vector<uint64_t > result_inorder; // /< result stores the inorder
318
- // /< traversal of the binary tree
319
- std::vector<uint64_t > result_preorder; // /< result stores the preorder
320
- // /< traversal of the binary tree
321
- std::vector<uint64_t > result_postorder; // /< result stores the postorder
322
- // /< traversal of the binary tree
323
-
324
- uint64_t size = actual_result_inorder.size ();
335
+ std::vector<std::uint64_t > actual_result_inorder{4 , 2 , 5 , 1 , 3 };
336
+ std::vector<std::uint64_t > actual_result_preorder{1 , 2 , 4 , 5 , 3 };
337
+ std::vector<std::uint64_t > actual_result_postorder{4 , 5 , 2 , 3 , 1 };
338
+ std::vector<std::uint64_t >
339
+ result_inorder; // /< result stores the inorder
340
+ // /< traversal of the binary tree
341
+ std::vector<std::uint64_t >
342
+ result_preorder; // /< result stores the preorder
343
+ // /< traversal of the binary tree
344
+ std::vector<std::uint64_t >
345
+ result_postorder; // /< result stores the postorder
346
+ // /< traversal of the binary tree
347
+
348
+ std::uint64_t size = actual_result_inorder.size ();
325
349
326
350
// Calling inorder() function by passing a root node,
327
351
// and storing the inorder traversal in result_inorder.
@@ -352,6 +376,7 @@ void test3() {
352
376
std::cout << " Passed!" << std::endl;
353
377
354
378
std::cout << std::endl;
379
+ deleteAll (root);
355
380
}
356
381
357
382
/* *
0 commit comments