{"id":169,"date":"2019-11-28T16:36:00","date_gmt":"2019-11-28T08:36:00","guid":{"rendered":"http:\/\/ff.mhrooz.xyz\/?p=169"},"modified":"2020-01-16T18:21:54","modified_gmt":"2020-01-16T10:21:54","slug":"haffman_shu","status":"publish","type":"post","link":"https:\/\/blog.mhrooz.xyz\/index.php\/2019\/11\/28\/haffman_shu\/","title":{"rendered":"Haffman\u6811"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7b97\u6cd5\u5982\u4e0b<\/h2>\n\n\n\n<p>(1)\u627e\u5230\u961f\u5217\u91cc\u6743\u503c\u6700\u5c0f\u7684\u4e24\u4e2a\u8282\u70b9\uff0c\u6700\u5c0f\u7684\u4e3a\u5de6\u513f\u5b50\uff0c\u6b21\u5c0f\u7684\u4e3a\u53f3\u513f\u5b50\u3002<\/p>\n\n\n\n<p>(2)\u65b0\u5efa\u8282\u70b9\uff0c\u5c06\u5de6\u53f3\u513f\u5b50\u8d4b\u503c\u540e\uff0c\u5c06\u5176\u6743\u503c\u8d4b\u503c\u4e3a\u5de6\u53f3\u4e4b\u548c\u3002<\/p>\n\n\n\n<p>(3)\u5c06\u8282\u70b9\u52a0\u5165\u961f\u5217<\/p>\n\n\n\n<p>(4)\u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u9000\u51fa\u5faa\u73af\uff0c\u5efa\u7acb\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<p>\u6311\u9009\u6700\u5c0f\u7684\u4e24\u4e2a\u53ef\u4ee5\u4ea4\u7ed9\u4f18\u5148\u961f\u5217\u5224\u65ad\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">typedef struct BiTnode<br>{<br> &nbsp; &nbsp;TElemType data;<br> &nbsp; &nbsp;struct BiTnode * lchild, * rchild;<br> &nbsp; &nbsp;bool type = false;<br> &nbsp; &nbsp;int val;<br>} * BiTree;<br>struct cmp{<br> &nbsp; &nbsp;bool operator()(BiTnode *&amp;a, BiTnode *&amp;b) const<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;return a-&gt;data&gt;b-&gt;data;<br> &nbsp;  }<br>};<\/pre>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void Huffmantree(int cnt, BiTree &amp;T)<br>{<br> &nbsp; &nbsp;int Num[30];string s;<br> &nbsp; &nbsp;memset(Num,0,sizeof(Num));<br> &nbsp; &nbsp;srand((int)time(nullptr));<br>\u200b<br> &nbsp; &nbsp;for(int i=0;i&lt;cnt;i++) &nbsp;s+=(char)('a'+rand()%26);\/\/\u968f\u673a\u751f\u6210\u4e32<br> &nbsp; &nbsp;for(char i : s) &nbsp; &nbsp; &nbsp; &nbsp; Num[i-'a']++;<br> &nbsp; &nbsp;for(int i=0;i&lt;26;i++) &nbsp; cout&lt;&lt;Num[i]&lt;&lt;' ';<br> &nbsp; &nbsp;cout&lt;&lt;endl&lt;&lt;s&lt;&lt;endl;<br> &nbsp; &nbsp;priority_queue&lt;BiTree,vector&lt;BiTree&gt;,cmp&gt;que;\/\/\u5efa\u7acb\u4f18\u5148\u961f\u5217<br> &nbsp; &nbsp;for(int i=0;i&lt;26;i++)<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;auto tmp = (BiTree)malloc(sizeof(BiTnode));\/\/\u5efa\u7acb\u53f6\u5b50\u8282\u70b9<br> &nbsp; &nbsp; &nbsp; &nbsp;tmp -&gt; data = Num[i];tmp-&gt;type = true;\/\/\u4f9d\u7167\u51fa\u73b0\u7684\u6b21\u6570\u4e3a\u6743\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;tmp -&gt; val = i;<br> &nbsp; &nbsp; &nbsp; &nbsp;tmp-&gt;lchild = nullptr; &nbsp; &nbsp; &nbsp; &nbsp;tmp-&gt;rchild = nullptr;<br> &nbsp; &nbsp; &nbsp; &nbsp;if(tmp-&gt;data) &nbsp; &nbsp; &nbsp; &nbsp;que.push(tmp);\/\/\u5982\u679c\u51fa\u73b0\u4e86\uff0c\u5219\u52a0\u5165\u961f\u5217<br> &nbsp;  }<br> &nbsp; &nbsp;cout&lt;&lt;\"=====================\"&lt;&lt;endl;<br> &nbsp; &nbsp;auto now = (BiTree)malloc(sizeof(BiTnode));\/\/\u65b0\u5efa\u5934\u8282\u70b9<br> &nbsp; &nbsp;while(!que.empty())<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;BiTree q1 = que.top();que.pop();if(que.empty()){break;}\/\/\u7279\u5224\u5982\u679c\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u9000\u51fa<br> &nbsp; &nbsp; &nbsp; &nbsp;BiTree q2 = que.top();que.pop();<br> &nbsp; &nbsp; &nbsp; &nbsp;now-&gt;lchild = q1; now-&gt;rchild = q2; now-&gt;data = q1-&gt;data+q2-&gt;data;<br> &nbsp; &nbsp; &nbsp; &nbsp;que.push(now); &nbsp; T = now;<br> &nbsp; &nbsp; &nbsp; &nbsp;now = (BiTree)malloc(sizeof(BiTnode));<br> &nbsp;  }<br> &nbsp; struct no<br> &nbsp; {<br> &nbsp; &nbsp; &nbsp; &nbsp;BiTree node;<br> &nbsp; &nbsp; &nbsp; &nbsp;string way;<br> &nbsp; };<br> &nbsp; &nbsp;queue&lt;no&gt;Q; &nbsp; Q.push((no){T,\"\"});\/\/\u89e3\u7801<br> &nbsp; &nbsp;while(!Q.empty())<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;no tmp = Q.front();Q.pop();<br> &nbsp; &nbsp; &nbsp; &nbsp;if(tmp.node-&gt;lchild)<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Q.push((no){tmp.node-&gt;lchild,tmp.way+\"0\"});\/\/\u5de6\u513f\u5b50\u8def\u5f84\u52a0\u51650\uff0c????\u513f\u5b50\u8def\u5f84\u52a0\u51651<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Q.push((no){tmp.node-&gt;rchild,tmp.way+\"1\"});<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout&lt;&lt;tmp.way&lt;&lt;' '&lt;&lt;(char)(tmp.node-&gt;val+'a')&lt;&lt;endl;<br> &nbsp;  }<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b97\u6cd5\u5982\u4e0b (1)\u627e\u5230\u961f\u5217\u91cc\u6743\u503c\u6700\u5c0f\u7684\u4e24\u4e2a\u8282\u70b9\uff0c\u6700\u5c0f\u7684\u4e3a\u5de6\u513f\u5b50\uff0c\u6b21\u5c0f\u7684\u4e3a\u53f3\u513f\u5b50\u3002 (2)\u65b0\u5efa\u8282\u70b9\uff0c\u5c06\u5de6\u53f3\u513f\u5b50\u8d4b\u503c<a class=\"more-link\" href=\"https:\/\/blog.mhrooz.xyz\/index.php\/2019\/11\/28\/haffman_shu\/\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">&#8220;Haffman\u6811&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":170,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":1,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions\/171"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/media\/170"}],"wp:attachment":[{"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mhrooz.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}