Huffman Coding in C++ I have written this code after studying from Introduction to Algorithm and from GeeksForGeeks. I know there is a lot to improve because I don't know much C++11. Please help me to improve this code.

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;queue&gt;
#include &lt;string&gt;

class HuffmanCodes
{
 struct Node
 {
  char data;
  size_t freq;
  Node* left;
  Node* right;
  Node(char data, size_t freq) : data(data),
                                 freq(freq),
                                 left(nullptr),
                                 right(nullptr)
                                 {}
 ~Node()
 {
   delete left;
   delete right;
 }
 };

 struct compare
 {
  bool operator()(Node* l, Node* r)
  {
    return (l-&gt;freq &gt; r-&gt;freq);
  }
};

Node* top;

void printCode(Node* root, std::string str)
{
  if(root == nullptr)
   return;

 if(root-&gt;data == '$')
 {
  printCode(root-&gt;left, str + "0");
  printCode(root-&gt;right, str + "1");
 }

 if(root-&gt;data != '$')
 {
   std::cout &lt;&lt; root-&gt;data &lt;&lt; " : " &lt;&lt; str &lt;&lt; "\n";
   printCode(root-&gt;left, str + "0");
   printCode(root-&gt;right, str + "1");
 }
}

public:
  HuffmanCodes() {};
  ~HuffmanCodes()
  {
    delete top;
  }
  void GenerateCode(std::vector&lt;char&gt;&amp; data, std::vector&lt;size_t&gt;&amp; freq, size_t size)
  {
   Node* left;
   Node* right;
   std::priority_queue&lt;Node*, std::vector&lt;Node*&gt;, compare &gt; minHeap;

   for(size_t i = 0; i &lt; size; ++i)
   {
      minHeap.push(new Node(data[i], freq[i]));
   }

    while(minHeap.size() != 1)
    {
      left = minHeap.top();
      minHeap.pop();

      right = minHeap.top();
      minHeap.pop();

      top = new Node('$', left-&gt;freq + right-&gt;freq);
      top-&gt;left  = left;
      top-&gt;right = right;
      minHeap.push(top);
     }
    printCode(minHeap.top(), "");
 }
};

 int main()
 {
  HuffmanCodes set1;
  std::vector&lt;char&gt; data({'d', 'e', 'b', 'c', 'a', 'f'});
  std::vector&lt;size_t&gt; freq({16, 9, 13, 12, 45, 5});
  size_t size = data.size();
  set1.GenerateCode(data, freq, size);

  return 0;
}
When you declare a class, initialize all member variables

char data = '\0';
size_t freq = 0;
...


(depending on your compiler you may need to do that in the
constructor instead)

especially pointers, since a delete on an uninitialized pointer is undefined behavior but a delete on a nullptr is OK (NOP).



void GenerateCode(std::vector&lt;char&gt;&amp; data, std::vector&lt;size_t&gt;&amp; freq, size_t size)


is your intention to modify 'data'? if not, then write const in front

void GenerateCode(const std::vector&lt;char&gt;&amp; data, std::vector&lt;size_t&gt;&amp; freq)




why do you pass 'size'? 'data' already has a size: data.size()



you have an unitialized member variable called 'top', you use it in GenerateCode and it
will be set there, but if you never call GenerateCode, just a simple class declaration will
invoke undefined behavior since delete top is called in HuffManCodes destructor.



memory ownership is not easy to follow in your code

avoid using raw pointers. By using smart pointers ownership is always
clear and you dont need to bother about where to delete.

struct Node
{
    std::unique_ptr&lt;Node&gt; left;
    std::unique_ptr&lt;Node&gt; right;  // here it's clear that 'right' owns what it points to
    ...
}