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 Basics of Data Structures in C ++, the tutorial gave a 2-page definition of Huffman coding and the code above. For me, the book was not detailed enough, so I did a search on Google and I found out how the Huffman coding process works. The tutorial states that at the end of the above code, a Huffman tree is created. But to me, this seems wrong, because the Huffman tree is not necessarily a complete tree, but the code seems to always give a full tree because of heap.push(). So can someone explain to me how this piece of code is not wrong?
heap.push()
- , , node. , node node. .
, . ( MinHeap ), "" node, . , node . ; , , , - .
, , , , . , "n"; while (heap.size() > 1). , "", , .
while (heap.size() > 1)
Source: https://habr.com/ru/post/1755069/More articles:Setcookie task IE8 - phpComparing two lambdas / Procs in Ruby - ruby | fooobar.comЯвляется ли Facebooker for Rails актуальным и применимым для разработки приложений для facebook? - ruby-on-railsUsing IN in where - phpHow to remove the left pad in this layout? - cssNaming conventions for html / css + MVC? - javascriptМогу ли я передать результат шаблона xslt в качестве параметра в другой шаблон? - xsltRedirecting stdout with low api to C - cNeed help displaying a randomly selected image when loading activity and clicking a button - javaDynamically linking a Python extension (.pyd) to another extension - pythonAll Articles