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