/*´òÓ¡¹þ·òÂüÊ÷*/ template<typename T> void Huffman<T>::print(HuffmanNode<T>* pnode) { if (pnode != nullptr) { cout << "µ±Ç°½áµã£º" << pnode->key<<"."; if (pnode->lchild != nullptr) cout << "ËüµÄ×óº¢×Ó½ÚµãΪ£º" << pnode->lchild->key << "."; else cout << "ËüûÓÐ×óº¢×Ó."; if (pnode->rchild != nullptr) cout << "ËüµÄÓÒº¢×Ó½ÚµãΪ£º" << pnode->rchild->key << "."; else cout << "ËüûÓÐÓÒº¢×Ó."; cout << endl; print(pnode->lchild); print(pnode->rchild); } };
3.5 ¹þ·òÂüÊ÷´úÂë²âÊÔÎÒÃǹ¹½¨ÉÏͼÖеĹþ·òÂüÊ÷£¬ËüµÄËĸöȨֵ·Ö±ðΪ{10£¬20£¬30£¬40}£º
²âÊÔ´úÂ룺
int _tmain(int argc, _TCHAR* argv[]) { Huffman<int> huff; int a[] = { 10,20,30,40 }; huff.creat(a, 4); //¹¹½¨Ò»¿Ã¹þ·òÂüÊ÷ huff.print(); //´òÓ¡½Úµã¼ä¹Øϵ getchar(); return 0; }
²âÊÔ½á¹û£º
µ±Ç°½áµã£º100.ËüµÄ×óº¢×Ó½ÚµãΪ£º40.ËüµÄÓÒº¢×Ó½ÚµãΪ£º60. µ±Ç°½áµã£º40.ËüûÓÐ×óº¢×Ó.ËüûÓÐÓÒº¢×Ó. µ±Ç°½áµã£º60.ËüµÄ×óº¢×Ó½ÚµãΪ£º30.ËüµÄÓÒº¢×Ó½ÚµãΪ£º30. µ±Ç°½áµã£º30.ËüûÓÐ×óº¢×Ó.ËüûÓÐÓÒº¢×Ó. µ±Ç°½áµã£º30.ËüµÄ×óº¢×Ó½ÚµãΪ£º10.ËüµÄÓÒº¢×Ó½ÚµãΪ£º20. µ±Ç°½áµã£º10.ËüûÓÐ×óº¢×Ó.ËüûÓÐÓÒº¢×Ó. µ±Ç°½áµã£º20.ËüûÓÐ×óº¢×Ó.ËüûÓÐÓÒº¢×Ó.
¸ù¾Ý½Úµã¹Øϵ¿ÉÒÔ»³öÈç϶þ²æÊ÷£¬ÕýÊÇÉÏÃæÎÒÃǹ¹½¨µÄ¹þ·òÂüÊ÷¡£
Ϊ{10£¬20£¬30£¬40}ÕâËĸöȨֵ¹¹½¨Á˹þ·òÂü±àÂëºó£¬ÎÒÃÇ¿ÉÒÔÓÉÈçϹæÔò»ñµÃËüÃǵĹþ·òÂü±àÂ룺
£¨×Öĸ£©È¨Öµ ±àÂë
10 100
20 101
30 11
40 0
Óɴ˿ɼû£¬³öÏÖƵÂÊÔ½¸ßµÄ×Öĸ£¨Ò²¼´È¨ÖµÔ½´ó£©£¬Æä±àÂëÔ½¶Ì¡£Õâ±ãʹ±àÂëÖ®ºóµÄ×Ö·û´®µÄƽ¾ù³¤¶È¡¢ÆÚÍûÖµ½µµÍ£¬´Ó¶ø´ïµ½ÎÞËðѹËõÊý¾ÝµÄÄ¿µÄ¡£
5. ¹þ·òÂüÊ÷ÍêÕû´úÂë¹þ·òÂüÊ÷ÍêÕû´úÂ룺https://github.com/huanzheWu/Data-Structure/blob/master/Huffman/Huffman/Huffman.h
¸ü¶àÊý¾Ý½á¹¹C++ʵÏÖ´úÂ룺https://github.com/huanzheWu/Data-Structure
Ô´´ÎÄÕ£¬×ªÔØÇë×¢Ã÷³ö´¦£º
¡¡