Skip to content

Commit de4469c

Browse files
Merge branch 'master' into fix/2509-added-test-case
2 parents 3bf2999 + b169269 commit de4469c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

data_structures/morrisinorder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,21 @@ void morrisInorder(Btree *root) {
8181
}
8282
}
8383

84+
void deleteAll(const Btree *const root) {
85+
if (root) {
86+
deleteAll(root->left);
87+
deleteAll(root->right);
88+
delete root;
89+
}
90+
}
91+
8492
int main() {
8593
// Testing morrisInorder funtion
8694
Btree *root = NULL;
8795
int i;
8896
for (i = 1; i <= 7; i++) insert(&root, i);
8997
cout << "Morris Inorder: ";
9098
morrisInorder(root);
99+
deleteAll(root);
91100
return 0;
92101
}

greedy_algorithms/huffman.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ struct MinHeapNode {
2323
}
2424
};
2525

26+
void deleteAll(const MinHeapNode* const root) {
27+
if (root) {
28+
deleteAll(root->left);
29+
deleteAll(root->right);
30+
delete root;
31+
}
32+
}
33+
2634
// For comparison of
2735
// two heap nodes (needed in min heap)
2836
struct compare {
@@ -85,6 +93,7 @@ void HuffmanCodes(char data[], int freq[], int size) {
8593
// Print Huffman codes using
8694
// the Huffman tree built above
8795
printCodes(minHeap.top(), "");
96+
deleteAll(minHeap.top());
8897
}
8998

9099
// Driver program to test above functions

operations_on_datastructures/reverse_binary_tree.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class BinaryTree {
9191
return pivot;
9292
}
9393

94+
BinaryTree(const BinaryTree&) = delete;
95+
BinaryTree& operator=(const BinaryTree&) = delete;
96+
9497
public:
9598
/**
9699
* @brief Creates a BinaryTree with a root pointing to NULL.
@@ -100,6 +103,21 @@ class BinaryTree {
100103
* @brief Creates a BinaryTree with a root with an initial value.
101104
*/
102105
explicit BinaryTree(int64_t data) { root = new Node(data); }
106+
107+
~BinaryTree() {
108+
std::vector<Node*> nodes;
109+
nodes.emplace_back(root);
110+
while (!nodes.empty()) {
111+
const auto cur_node = nodes.back();
112+
nodes.pop_back();
113+
if (cur_node) {
114+
nodes.emplace_back(cur_node->left);
115+
nodes.emplace_back(cur_node->right);
116+
delete cur_node;
117+
}
118+
}
119+
}
120+
103121
/**
104122
* @brief Adds a new Node to the Binary Tree
105123
*/

0 commit comments

Comments
 (0)