Skip to content

Commit 56d17d9

Browse files
Merge branch 'master' into cstdint
2 parents e6bafa6 + b6108e4 commit 56d17d9

File tree

1 file changed

+73
-49
lines changed

1 file changed

+73
-49
lines changed

others/recursive_tree_traversal.cpp

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353

5454
#include <cassert> /// for assert
55-
#include <cstdint> /// for integral typedefs
55+
#include <cstdint> /// for std::uint64_t
5656
#include <iostream> /// for I/O operations
5757
#include <vector> /// for vector
5858

@@ -77,33 +77,34 @@ namespace recursive_tree_traversals {
7777
* @param right follow up right subtree.
7878
*/
7979
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.
8383
};
8484
/**
8585
* @brief BT used to make the entire structure of the binary tree and the
8686
* functions associated with the binary tree
8787
*/
8888
class BT {
8989
public:
90-
std::vector<uint64_t>
90+
std::vector<std::uint64_t>
9191
inorder_result; // vector to store the inorder traversal of the tree.
92-
std::vector<uint64_t>
92+
std::vector<std::uint64_t>
9393
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.
9697

9798
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.
99100

100-
std::vector<uint64_t> inorder(
101+
std::vector<std::uint64_t> inorder(
101102
Node *); // function that takes root of the tree as an argument and
102103
// returns its inorder traversal.
103-
std::vector<uint64_t> preorder(
104+
std::vector<std::uint64_t> preorder(
104105
Node *); // function that takes root of the tree as an argument and
105106
// returns its preorder traversal.
106-
std::vector<uint64_t> postorder(
107+
std::vector<std::uint64_t> postorder(
107108
Node *); // function that takes root of the tree as an argument and
108109
// returns its postorder traversal.
109110
};
@@ -114,7 +115,7 @@ class BT {
114115
* @param data value that a particular node will contain.
115116
* @return pointer to the newly created node with assigned data.
116117
*/
117-
Node *BT::createNewNode(uint64_t data) {
118+
Node *BT::createNewNode(std::uint64_t data) {
118119
Node *node = new Node();
119120
node->data = data;
120121
node->left = node->right = nullptr;
@@ -128,7 +129,7 @@ Node *BT::createNewNode(uint64_t data) {
128129
* @param root head/root node of a tree
129130
* @return result that is containing the inorder traversal of a tree
130131
**/
131-
std::vector<uint64_t> BT::inorder(Node *root) {
132+
std::vector<std::uint64_t> BT::inorder(Node *root) {
132133
if (root == nullptr) { // return if the current node is empty
133134
return {};
134135
}
@@ -148,7 +149,7 @@ std::vector<uint64_t> BT::inorder(Node *root) {
148149
* @param root head/root node of a tree
149150
* @return result that is containing the preorder traversal of a tree
150151
*/
151-
std::vector<uint64_t> BT::preorder(Node *root) {
152+
std::vector<std::uint64_t> BT::preorder(Node *root) {
152153
if (root == nullptr) { // if the current node is empty
153154
return {};
154155
}
@@ -168,7 +169,7 @@ std::vector<uint64_t> BT::preorder(Node *root) {
168169
* @param root head/root node of a tree
169170
* @return result that is containing the postorder traversal of a tree
170171
*/
171-
std::vector<uint64_t> BT::postorder(Node *root) {
172+
std::vector<std::uint64_t> BT::postorder(Node *root) {
172173
if (root == nullptr) { // if the current node is empty
173174
return {};
174175
}
@@ -181,6 +182,14 @@ std::vector<uint64_t> BT::postorder(Node *root) {
181182
return postorder_result;
182183
}
183184

185+
void deleteAll(const Node *const root) {
186+
if (root) {
187+
deleteAll(root->left);
188+
deleteAll(root->right);
189+
delete root;
190+
}
191+
}
192+
184193
} // namespace recursive_tree_traversals
185194

186195
} // namespace others
@@ -201,17 +210,23 @@ void test1() {
201210
root->left->right->right = obj1.createNewNode(11);
202211
root->right->right->left = obj1.createNewNode(4);
203212

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();
215230

216231
// Calling inorder() function by passing a root node,
217232
// and storing the inorder traversal in result_inorder.
@@ -241,6 +256,7 @@ void test1() {
241256
std::cout << "Passed!" << std::endl;
242257

243258
std::cout << std::endl;
259+
deleteAll(root);
244260
}
245261

246262
/**
@@ -258,17 +274,20 @@ void test2() {
258274
root->right->left->left = obj2.createNewNode(7);
259275
root->right->left->right = obj2.createNewNode(8);
260276

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();
272291

273292
// Calling inorder() function by passing a root node,
274293
// and storing the inorder traversal in result_inorder.
@@ -298,6 +317,7 @@ void test2() {
298317
std::cout << "Passed!" << std::endl;
299318

300319
std::cout << std::endl;
320+
deleteAll(root);
301321
}
302322

303323
/**
@@ -312,17 +332,20 @@ void test3() {
312332
root->left->left = obj3.createNewNode(4);
313333
root->left->right = obj3.createNewNode(5);
314334

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();
326349

327350
// Calling inorder() function by passing a root node,
328351
// and storing the inorder traversal in result_inorder.
@@ -353,6 +376,7 @@ void test3() {
353376
std::cout << "Passed!" << std::endl;
354377

355378
std::cout << std::endl;
379+
deleteAll(root);
356380
}
357381

358382
/**

0 commit comments

Comments
 (0)