HTML5¼¼Êõ

Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£º¹þ·òÂüÊ÷Óë¹þ·òÂü±àÂëÏê½â¼°C++Ä£°åʵÏÖ - melonstreet

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-02-12 14:00 ÎÒÒªÆÀÂÛ( )

0. Êý¾Ý½á¹¹Í¼ÎĽâÎöϵÁÐ Êý¾Ý½á¹¹ÏµÁÐÎÄÕ Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºÊý×é¡¢µ¥Á´±í¡¢Ë«Á´±í½éÉܼ°C++Ä£°åʵÏÖ Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºÕ»µÄ¼ò½é¼°C++Ä£°åʵÏÖ Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£º¶ÓÁÐÏê½âÓëC++Ä£°åʵÏÖ Êý¾Ý½á¹¹Í¼ÎĽâÎöÖ®£ºÊ÷µÄ¼ò½é¼°¶þ²æÅÅÐòÊ÷C++Ä£°åʵÏÖ. Êý

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¡£

ÄÇô£¬ÎÒÃÇÈçºÎ»ñȡÿ¸ö×Ö·û´®µÄ±àÂëÄØ£¿Õâ¾ÍÐèÒª¹þ·òÂüÊ÷ÁË¡£
Ô´×Ö·û±àÂëµÄ³¤¶ÌÈ¡¾öÓÚÆä³öÏÖµÄƵÂÊ£¬ÎÒÃÇ°ÑÔ´×Ö·û³öÏÖµÄƵÂʶ¨ÒåΪ¸Ã×Ö·ûµÄȨֵ¡£

2. ¹þ·òÂüÊ÷¼ò½é

¹þ·òÂüÓÖ³Æ×îÓŶþ²æÊ÷¡£ÊÇÒ»ÖÖ´øȨ·¾¶³¤¶È×î¶ÌµÄ¶þ²æÊ÷¡£ËüµÄ¶¨ÒåÈçÏ¡£

¹þ·òÂüÊ÷µÄ¶¨Òå

¼ÙÉèÓÐn¸öȨֵ{w1,w2,w3,w4...,wn}£¬¹¹ÔìÒ»¿ÃÓÐn¸ö½ÚµãµÄ¶þ²æÊ÷£¬ÈôÊ÷µÄ´øȨ·¾¶×îС£¬ÔòÕâ¿ÅÊ÷³Æ×÷¹þ·òÂüÊ÷¡£ÕâÀïÃæÉæ¼°µ½¼¸¸ö¸ÅÄÎÒÃÇÓÉÒ»¿Ã¹þ·òÂüÊ÷À´½âÊÍ

3. ¹¹Ôì¹þ·òÂüÊ÷ 3.1 ¹þ·òÂüÊ÷µÄ½Úµã½á¹¹

/*¹þ·òÂüÊ÷µÄ½Úµã¶¨Òå*/ 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;//É­ÁÖ };

  • root:¹þ·òÂüÊ÷µÄ¸ù½áµã¡£
  • forset : É­ÁÖ£¬ÕâÀïʹÓÃdequeÀ´´æ´¢É­ÁÖÖÐÊ÷µÄ¸ù½Úµã¡£
  • 3.3 ¹þ·òÂüÊ÷µÄ¹¹Ôì²½Öè

    ¼ÙÉèÓÐ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(); };

  • ÕâÀï½ö½öʾ·¶¹¹½¨¹þ·òÂüÊ÷µÄ¹ý³Ì£¬Ã»Óо­¹ýÐÔÄÜÉϵÄÓÅ»¯ºÍÍêÉƵÄÒì³£´¦Àí¡£ÕâÀïʹÓÃdequeË«¶Ë¶ÓÁÐÀ´´æ´¢É­ÁÖÖÐÊ÷¸ù½Úµã£¬Ê¹Óÿ⺯ÊýsortÀ´¶Ô¸ù½ÚµãÒÀȨֵÅÅÐò¡£ÕâÀïÒ²¿ÉÒÔʹÓÃÎÒÃÇ֮ǰ½éÉܵÄС¶¥¶ÑÀ´Íê³É¹¤×÷¡£
  • 3.4 ¹þ·òÂüÊ÷µÄÆäËû²Ù×÷

    ÆäËû²Ù×÷ÔÚÇ°¼¸Æª²©ÎÄÖж¼ÓнéÉܹý£¬ÕâÀï¾Í²»ÔÙ†ªà£¬¿ÉÒÔÔÚÎÄÕµײ¿Á´½ÓÈ¡µÃÍêÕûµÄ¹¤³ÌÔ´Âë¡£
    ÕâÀïÌù³ö²âÊÔʱÐèÒªµÄ´úÂ룺

    ¡¡

    1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

    Ïà¹ØÎÄÕÂ
    • URL½âÎö - ÃîÒôÌìÅ®

      URL½âÎö - ÃîÒôÌìÅ®

      2017-03-27 16:02

    • ¿ªÔ´µÄ.NET¶¨Ê±ÈÎÎñ×é¼þHangfire½âÎö - ÅíÔó0902

      ¿ªÔ´µÄ.NET¶¨Ê±ÈÎÎñ×é¼þHangfire½âÎö - ÅíÔó0902

      2017-03-22 13:00

    • ¿çƽ̨µÄ.NETÓʼþЭÒéMailKit×é¼þ½âÎö - ÅíÔó0902

      ¿çƽ̨µÄ.NETÓʼþЭÒéMailKit×é¼þ½âÎö - ÅíÔó0902

      2017-03-17 13:01

    • ÌÚѶÔÆÊý¾Ý¿âÍŶӣºSQL Server Êý¾Ý¼ÓÃܹ¦ÄܽâÎö - ÌÚѶÔƼ¼ÊõÉçÇø

      ÌÚѶÔÆÊý¾Ý¿âÍŶӣºSQL Server Êý¾Ý¼ÓÃܹ¦ÄܽâÎö - ÌÚѶÔƼ¼ÊõÉçÇø

      2017-03-17 10:00

    ÍøÓѵãÆÀ
    ¾