Skip to content

Commit

Permalink
fix: memory leak in huffman.cpp (#2728)
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Sep 11, 2024
1 parent 15e3fed commit d74f4d3
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions greedy_algorithms/huffman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ struct MinHeapNode {
}
};

void deleteAll(const MinHeapNode* const root) {
if (root) {
deleteAll(root->left);
deleteAll(root->right);
delete root;
}
}

// For comparison of
// two heap nodes (needed in min heap)
struct compare {
Expand Down Expand Up @@ -85,6 +93,7 @@ void HuffmanCodes(char data[], int freq[], int size) {
// Print Huffman codes using
// the Huffman tree built above
printCodes(minHeap.top(), "");
deleteAll(minHeap.top());
}

// Driver program to test above functions
Expand Down

0 comments on commit d74f4d3

Please sign in to comment.