From d74f4d33ea84ef2ff618f09d592aa05effecb3d1 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Wed, 11 Sep 2024 03:25:42 +0200 Subject: [PATCH] fix: memory leak in `huffman.cpp` (#2728) --- greedy_algorithms/huffman.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/greedy_algorithms/huffman.cpp b/greedy_algorithms/huffman.cpp index 21c8295f3dd..a63007422dd 100644 --- a/greedy_algorithms/huffman.cpp +++ b/greedy_algorithms/huffman.cpp @@ -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 { @@ -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