template<class T>
void huffman(MinHeap<TreeNode<T>*> heap, int n)
{
for(int i=0;i<n-1;i++)
{
TreeNode<T> *first = heap.pop();
TreeNode<T> *second = heap.pop();
TreeNode<T> *bt = new BinaryTreeNode<T>(first, second, first.data, second.data);
heap.push(bt);
}
}
In my Fundamentals of Data Structures in C++ textbook, it gave a 2 page definition of Huffman coding, and the code above. To me, the book wasn't enough detailed, so I've done the googling and I learned how the process of Huffman coding works. The textbook claims that at the end of the code above, a Huffman tree is made. But to me it seems wrong, because a Huffman tree, is not necessary a complete tree, but the code above seems to always give a complete tree because of the heap.push()
. So can someone explain to me how this piece of code is not wrong?