0. Êý¾Ý½á¹¹Í¼ÎĽâÎöϵÁÐ
Êý¾Ý½á¹¹ÏµÁÐÎÄÕÂ
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºÊý×é¡¢µ¥Á´±í¡¢Ë«Á´±í½éÉܼ°C++Ä£°åʵÏÖ
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºÕ»µÄ¼ò½é¼°C++Ä£°åʵÏÖ
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£º¶ÓÁÐÏê½âÓëC++Ä£°åʵÏÖ
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºÊ÷µÄ¼ò½é¼°¶þ²æÅÅÐòÊ÷C++Ä£°åʵÏÖ.
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºAVLÊ÷Ïê½â¼°C++Ä£°åʵÏÖ
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£º¶þ²æ¶ÑÏê½â¼°C++Ä£°åʵÏÖ
Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£º¹þ·òÂüÊ÷Óë¹þ·òÂü±àÂëÏê½â¼°C++Ä£°åʵÏÖ
1. ¹þ·òÂü±àÂë¼ò½é¹þ·òÂü±àÂ루Huffman Coding£©ÊÇÒ»ÖÖ±àÂ뷽ʽ£¬Ò²³ÆΪ¡°ºÕ·òÂü±àÂ롱£¬ÊÇDavid A. Huffman1952Äê·¢Ã÷µÄÒ»ÖÖ¹¹½¨¼«Ð¡¶àÓà±àÂëµÄ·½·¨¡£
ÔÚ¼ÆËã»úÊý¾Ý´¦ÀíÖУ¬»ô·òÂü±àÂëʹÓñ䳤±àÂë±í¶ÔÔ´·ûºÅ½øÐбàÂ룬³öÏÖƵÂʽϸߵÄÔ´·ûºÅ²ÉÓý϶̵ıàÂ룬³öÏÖƵÂʽϸߵķûºÅ²ÉÓýϳ¤µÄ±àÂ룬ʹ±àÂëÖ®ºóµÄ×Ö·û´®×Ö·û´®µÄƽ¾ù³¤¶È ¡¢ÆÚÍûÖµ½µµÍ£¬ÒÔ´ïµ½ÎÞËðѹËõÊý¾ÝµÄÄ¿µÄ¡£
¾Ù¸öÀý×Ó£¬ÏÖÔÚÎÒÃÇÓÐÒ»×Ö·û´®£º
this is an example of a huffman tree
Õâ´®×Ö·û´®ÓÐ36¸ö×Ö·û£¬Èç¹û°´ÆÕͨ·½Ê½´æ´¢Õâ´®×Ö·û´®£¬Ã¿¸ö×Ö·ûÕ¼¾Ý1¸ö×Ö½Ú£¬Ôò¹²ÐèÒª36 * 1 * 8 = 288bit¡£
¾¹ý·ÖÎöÎÒÃÇ·¢ÏÖ£¬Õâ´®×Ö·û´®Öи÷×Öĸ³öÏÖµÄƵÂʲ»Í¬£¬Èç¹ûÎÒÃÇÄܹ»°´ÈçϱàÂ룺
×Öĸ ƵÂÊ ±àÂë --- ×Öĸ ƵÂÊ ±àÂë
space 7 111 s 2 1011
a 4 010 t 2 0110
e 4 000 l 1 11001
f 3 1101 o 1 00110
h 2 1010 p 1 10011
i 2 1000 r 1 11000
m 2 0111 u 1 00111
n 2 0010 x 1 10010
±àÂëÕâ´®×Ö·û´®£¬Ö»ÐèÒª£º
(7+4+4)x3 + (3+2+2+2+2+2+2)x4 + (1+1+1+1+1+1)x 5 = 45+60+30 = 135bit
±àÂëÕâ´®×Ö·û´®Ö»ÐèÒª135bit£¡µ¥µ¥Õâ´®×Ö·û´®£¬¾ÍѹËõÁË288-135 = 153bit¡£
ÄÇô£¬ÎÒÃÇÈçºÎ»ñȡÿ¸ö×Ö·û´®µÄ±àÂëÄØ£¿Õâ¾ÍÐèÒª¹þ·òÂüÊ÷ÁË¡£
Ô´×Ö·û±àÂëµÄ³¤¶ÌÈ¡¾öÓÚÆä³öÏÖµÄƵÂÊ£¬ÎÒÃÇ°ÑÔ´×Ö·û³öÏÖµÄƵÂʶ¨ÒåΪ¸Ã×Ö·ûµÄȨֵ¡£
¹þ·òÂüÓÖ³Æ×îÓŶþ²æÊ÷¡£ÊÇÒ»ÖÖ´øȨ·¾¶³¤¶È×î¶ÌµÄ¶þ²æÊ÷¡£ËüµÄ¶¨ÒåÈçÏ¡£
¹þ·òÂüÊ÷µÄ¶¨Òå¼ÙÉèÓÐn¸öȨֵ{w1,w2,w3,w4...,wn}£¬¹¹ÔìÒ»¿ÃÓÐn¸ö½ÚµãµÄ¶þ²æÊ÷£¬ÈôÊ÷µÄ´øȨ·¾¶×îС£¬ÔòÕâ¿ÅÊ÷³Æ×÷¹þ·òÂüÊ÷¡£ÕâÀïÃæÉæ¼°µ½¼¸¸ö¸ÅÄÎÒÃÇÓÉÒ»¿Ã¹þ·òÂüÊ÷À´½âÊÍ
/*¹þ·òÂüÊ÷µÄ½Úµã¶¨Òå*/ template <typename T> struct HuffmanNode { HuffmanNode(T k,HuffmanNode<T>*l=nullptr,HuffmanNode<T>* r=nullptr) :key(k),lchild(l), rchild(r){} ~HuffmanNode(){}; T key; //½ÚµãµÄȨֵ HuffmanNode<T>* lchild; //½Úµã×óº¢ HuffmanNode<T>* rchild; //½ÚµãÓÒº¢ };
3.2 ¹þ·òÂüÊ÷µÄ³éÏóÊý¾ÝÀàÐÍtemplate <typename T> class Huffman { public: Huffman(); ~Huffman(){}; postOrder(); destory(); : void preOrder(HuffmanNode<T>* pnode); void inOrder(HuffmanNode<T>* pnode); void postOrder(HuffmanNode<T>*pnode); void print(HuffmanNode<T>*pnode); void destroy(HuffmanNode<T>*pnode); private: HuffmanNode<T>* root; //¹þ·òÂüÊ÷¸ù½Úµã deque<HuffmanNode<T>*> forest;//ÉÁÖ };
¼ÙÉèÓÐn¸öȨֵ£¬Ôò¹¹Ôì³öµÄ¹þ·òÂüÊ÷ÓÐn¸öÒ¶×Ó½Úµã.n¸öȨֵ¼ÇΪ{w1,w2,w3...wn},¹þ·òÂüÊ÷µÄ¹¹Ôì¹ý³ÌΪ£º
ͼһµÄÊ÷bΪһ¿Ã¹þ·òÂüÊ÷£¬ËüµÄÒ¶×Ó½ÚµãΪ{10£¬20£¬30£¬40}£¬ÒÔÕâ4¸öȨֵ¹¹½¨Ê÷bµÄ¹ý³ÌΪ£º
Õâ¸ö¹ý³ÌºÜ±àÂëʵÏÖΪ:
/*´´½¨¹þ·òÂüÊ÷*/ template<typename T> void Huffman<T>::creat(T a[],int size) { for (int i = 0; i < size; i++) //ÿ¸ö½Úµã¶¼×÷Ϊһ¸öÉÁÖ { //Ϊ³õʼÐòÁеÄÔªËع¹½¨½Úµã¡£Ã¿¸ö½Úµã×÷Ϊһ¿ÃÊ÷¼ÓÈëÉÁÖÖС£ HuffmanNode<T>* ptr = new HuffmanNode<T>(a[i],nullptr,nullptr); forest.push_back(ptr); } for (int i = 0; i < size - 1; i++) { //ÅÅÐò£¬ÒÔÑ¡³ö¸ù½ÚµãȨֵ×îСÁ½¿ÃÊ÷ sort(forest.begin(), forest.end(), [](HuffmanNode<T>* a, HuffmanNode<T>*b){return a->key< b->key; }); HuffmanNode<T>*node = new HuffmanNode<T>(forest[0]->key + forest[1]->key, forest[0], forest[1]); //¹¹½¨Ð½ڵã forest.push_back(node); //нڵã¼ÓÈëÉÁÖÖÐ forest.pop_front(); //ɾ³ýÁ½¿ÃȨֵ×îСµÄÊ÷ forest.pop_front(); } root = forest.front(); forest.clear(); };
ÆäËû²Ù×÷ÔÚÇ°¼¸Æª²©ÎÄÖж¼ÓнéÉܹý£¬ÕâÀï¾Í²»ÔÙ†ªà£¬¿ÉÒÔÔÚÎÄÕµײ¿Á´½ÓÈ¡µÃÍêÕûµÄ¹¤³ÌÔ´Âë¡£
ÕâÀïÌù³ö²âÊÔʱÐèÒªµÄ´úÂ룺
¡¡