Skip to content

Commit d74f4d3

Browse files
authored
fix: memory leak in huffman.cpp (#2728)
1 parent 15e3fed commit d74f4d3

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

greedy_algorithms/huffman.cpp

+9
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

0 commit comments

Comments
 (0)