Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6108e4

Browse files
authored
fix: remove memory leak from recursive_tree_traversal.cpp (TheAlgorithms#2721)
1 parent db182d5 commit b6108e4

File tree

1 file changed

+73
-48
lines changed

1 file changed

+73
-48
lines changed

‎others/recursive_tree_traversal.cpp

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

5454
#include <cassert> /// for assert
55+
#include <cstdint> /// for std::uint64_t
5556
#include <iostream> /// for I/O operations
5657
#include <vector> /// for vector
5758

@@ -76,33 +77,34 @@ namespace recursive_tree_traversals {
7677
* @param right follow up right subtree.
7778
*/
7879
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.
8283
};
8384
/**
8485
* @brief BT used to make the entire structure of the binary tree and the
8586
* functions associated with the binary tree
8687
*/
8788
class BT {
8889
public:
89-
std::vector<uint64_t>
90+
std::vector<std::uint64_t>
9091
inorder_result; // vector to store the inorder traversal of the tree.
91-
std::vector<uint64_t>
92+
std::vector<std::uint64_t>
9293
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.
9597

9698
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.
98100

99-
std::vector<uint64_t> inorder(
101+
std::vector<std::uint64_t> inorder(
100102
Node *); // function that takes root of the tree as an argument and
101103
// returns its inorder traversal.
102-
std::vector<uint64_t> preorder(
104+
std::vector<std::uint64_t> preorder(
103105
Node *); // function that takes root of the tree as an argument and
104106
// returns its preorder traversal.
105-
std::vector<uint64_t> postorder(
107+
std::vector<std::uint64_t> postorder(
106108
Node *); // function that takes root of the tree as an argument and
107109
// returns its postorder traversal.
108110
};
@@ -113,7 +115,7 @@ class BT {
113115
* @param data value that a particular node will contain.
114116
* @return pointer to the newly created node with assigned data.
115117
*/
116-
Node *BT::createNewNode(uint64_t data) {
118+
Node *BT::createNewNode(std::uint64_t data) {
117119
Node *node = new Node();
118120
node->data = data;
119121
node->left = node->right = nullptr;
@@ -127,7 +129,7 @@ Node *BT::createNewNode(uint64_t data) {
127129
* @param root head/root node of a tree
128130
* @return result that is containing the inorder traversal of a tree
129131
**/
130-
std::vector<uint64_t> BT::inorder(Node *root) {
132+
std::vector<std::uint64_t> BT::inorder(Node *root) {
131133
if (root == nullptr) { // return if the current node is empty
132134
return {};
133135
}
@@ -147,7 +149,7 @@ std::vector<uint64_t> BT::inorder(Node *root) {
147149
* @param root head/root node of a tree
148150
* @return result that is containing the preorder traversal of a tree
149151
*/
150-
std::vector<uint64_t> BT::preorder(Node *root) {
152+
std::vector<std::uint64_t> BT::preorder(Node *root) {
151153
if (root == nullptr) { // if the current node is empty
152154
return {};
153155
}
@@ -167,7 +169,7 @@ std::vector<uint64_t> BT::preorder(Node *root) {
167169
* @param root head/root node of a tree
168170
* @return result that is containing the postorder traversal of a tree
169171
*/
170-
std::vector<uint64_t> BT::postorder(Node *root) {
172+
std::vector<std::uint64_t> BT::postorder(Node *root) {
171173
if (root == nullptr) { // if the current node is empty
172174
return {};
173175
}
@@ -180,6 +182,14 @@ std::vector<uint64_t> BT::postorder(Node *root) {
180182
return postorder_result;
181183
}
182184

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

185195
} // namespace others
@@ -200,17 +210,23 @@ void test1() {
200210
root->left->right->right = obj1.createNewNode(11);
201211
root->right->right->left = obj1.createNewNode(4);
202212

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

215231
// Calling inorder() function by passing a root node,
216232
// and storing the inorder traversal in result_inorder.
@@ -240,6 +256,7 @@ void test1() {
240256
std::cout << "Passed!" << std::endl;
241257

242258
std::cout << std::endl;
259+
deleteAll(root);
243260
}
244261

245262
/**
@@ -257,17 +274,20 @@ void test2() {
257274
root->right->left->left = obj2.createNewNode(7);
258275
root->right->left->right = obj2.createNewNode(8);
259276

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

272292
// Calling inorder() function by passing a root node,
273293
// and storing the inorder traversal in result_inorder.
@@ -297,6 +317,7 @@ void test2() {
297317
std::cout << "Passed!" << std::endl;
298318

299319
std::cout << std::endl;
320+
deleteAll(root);
300321
}
301322

302323
/**
@@ -311,17 +332,20 @@ void test3() {
311332
root->left->left = obj3.createNewNode(4);
312333
root->left->right = obj3.createNewNode(5);
313334

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

326350
// Calling inorder() function by passing a root node,
327351
// and storing the inorder traversal in result_inorder.
@@ -352,6 +376,7 @@ void test3() {
352376
std::cout << "Passed!" << std::endl;
353377

354378
std::cout << std::endl;
379+
deleteAll(root);
355380
}
356381

357382
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /