{"id":161,"date":"2019-12-27T08:10:00","date_gmt":"2019-12-27T08:10:00","guid":{"rendered":"https:\/\/imwarming.com\/?p=161"},"modified":"2019-12-27T08:10:00","modified_gmt":"2019-12-27T08:10:00","slug":"%e5%93%88%e5%a4%ab%e6%9b%bc%e6%a0%91","status":"publish","type":"post","link":"https:\/\/imwarming.com\/?p=161","title":{"rendered":"\u54c8\u592b\u66fc\u6811"},"content":{"rendered":"<div class=\"cnblogs_code\">\n<pre>#include&lt;bits\/stdc++.h&gt;\n<span style=\"color: #0000ff;\">#define<\/span> maxsize 100\n<span style=\"color: #0000ff;\">using<\/span> <span style=\"color: #0000ff;\">namespace<\/span><span style=\"color: #000000;\"> std;\n\n<\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u54c8\u592b\u66fc\u6811\u7ed3\u70b9\u7ed3\u6784<\/span>\ntypedef <span style=\"color: #0000ff;\">struct<\/span><span style=\"color: #000000;\"> {\n    <\/span><span style=\"color: #0000ff;\">int<\/span> weight;<span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u7ed3\u70b9\u6743\u91cd<\/span>\n    <span style=\"color: #0000ff;\">int<\/span> parent, left, right;<span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u7236\u7ed3\u70b9\u3001\u5de6\u5b69\u5b50\u3001\u53f3\u5b69\u5b50\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u4e0b\u6807<\/span>\n}htnode, *<span style=\"color: #000000;\">huffmantree;\n<\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5b58\u50a8\u54c8\u592b\u66fc\u7f16\u7801<\/span>\ntypedef <span style=\"color: #0000ff;\">char<\/span> **<span style=\"color: #000000;\"> huffmancode;\n\n<\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">ht\u6570\u7ec4\u4e2d\u5b58\u653e\u7684\u54c8\u592b\u66fc\u6811\uff0cend\u8868\u793aht\u6570\u7ec4\u4e2d\u5b58\u653e\u7ed3\u70b9\u7684\u6700\u7ec8\u4f4d\u7f6e\uff0cs1\u548cs2\u4f20\u9012\u7684\u662fht\u6570\u7ec4\u4e2d\u6743\u91cd\u503c\u6700\u5c0f\u7684\u4e24\u4e2a\u7ed3\u70b9\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e<\/span>\n<span style=\"color: #0000ff;\">void<\/span> select(huffmantree ht, <span style=\"color: #0000ff;\">int<\/span> end, <span style=\"color: #0000ff;\">int<\/span> *s1, <span style=\"color: #0000ff;\">int<\/span> *<span style=\"color: #000000;\">s2)\n{\n    <\/span><span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> min1, min2;\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u904d\u5386\u6570\u7ec4\u521d\u59cb\u4e0b\u6807\u4e3a 1<\/span>\n    <span style=\"color: #0000ff;\">int<\/span> i = <span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u627e\u5230\u8fd8\u6ca1\u6784\u5efa\u6811\u7684\u7ed3\u70b9<\/span>\n    <span style=\"color: #0000ff;\">while<\/span>(ht[i].parent != <span style=\"color: #800080;\">0<\/span> &amp;&amp; i &lt;=<span style=\"color: #000000;\"> end){\n        i<\/span>++<span style=\"color: #000000;\">;\n    }\n    min1 <\/span>=<span style=\"color: #000000;\"> ht[i].weight;\n    <\/span>*s1 =<span style=\"color: #000000;\"> i;\n   \n    i<\/span>++<span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">while<\/span>(ht[i].parent != <span style=\"color: #800080;\">0<\/span> &amp;&amp; i &lt;=<span style=\"color: #000000;\"> end){\n        i<\/span>++<span style=\"color: #000000;\">;\n    }\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5bf9\u627e\u5230\u7684\u4e24\u4e2a\u7ed3\u70b9\u6bd4\u8f83\u5927\u5c0f\uff0cmin2\u4e3a\u5927\u7684\uff0cmin1\u4e3a\u5c0f\u7684<\/span>\n    <span style=\"color: #0000ff;\">if<\/span>(ht[i].weight &lt;<span style=\"color: #000000;\"> min1){\n        min2 <\/span>=<span style=\"color: #000000;\"> min1;\n        <\/span>*s2 = *<span style=\"color: #000000;\">s1;\n        min1 <\/span>=<span style=\"color: #000000;\"> ht[i].weight;\n        <\/span>*s1 =<span style=\"color: #000000;\"> i;\n    }<\/span><span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">{\n        min2 <\/span>=<span style=\"color: #000000;\"> ht[i].weight;\n        <\/span>*s2 =<span style=\"color: #000000;\"> i;\n    }\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u4e24\u4e2a\u7ed3\u70b9\u548c\u540e\u7eed\u7684\u6240\u6709\u672a\u6784\u5efa\u6210\u6811\u7684\u7ed3\u70b9\u505a\u6bd4\u8f83<\/span>\n    <span style=\"color: #0000ff;\">for<\/span>(<span style=\"color: #0000ff;\">int<\/span> j=i+<span style=\"color: #800080;\">1<\/span>; j &lt;= end; j++<span style=\"color: #000000;\">)\n    {\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u6709\u7236\u7ed3\u70b9\uff0c\u76f4\u63a5\u8df3\u8fc7\uff0c\u8fdb\u884c\u4e0b\u4e00\u4e2a<\/span>\n        <span style=\"color: #0000ff;\">if<\/span>(ht[j].parent != <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">){\n            <\/span><span style=\"color: #0000ff;\">continue<\/span><span style=\"color: #000000;\">;\n        }\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u6bd4\u6700\u5c0f\u7684\u8fd8\u5c0f\uff0c\u5c06min2=min1\uff0cmin1\u8d4b\u503c\u65b0\u7684\u7ed3\u70b9\u7684\u4e0b\u6807<\/span>\n        <span style=\"color: #0000ff;\">if<\/span>(ht[j].weight &lt;<span style=\"color: #000000;\"> min1){\n            min2 <\/span>=<span style=\"color: #000000;\"> min1;\n            min1 <\/span>=<span style=\"color: #000000;\"> ht[j].weight;\n            <\/span>*s2 = *<span style=\"color: #000000;\">s1;\n            <\/span>*s1 =<span style=\"color: #000000;\"> j;\n        }\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u4ecb\u4e8e\u4e24\u8005\u4e4b\u95f4\uff0cmin2\u8d4b\u503c\u4e3a\u65b0\u7684\u7ed3\u70b9\u7684\u4f4d\u7f6e\u4e0b\u6807<\/span>\n        <span style=\"color: #0000ff;\">else<\/span> <span style=\"color: #0000ff;\">if<\/span>(ht[j].weight &gt;= min1 &amp;&amp; ht[j].weight &lt;<span style=\"color: #000000;\"> min2){\n            min2 <\/span>=<span style=\"color: #000000;\"> ht[j].weight;\n            <\/span>*s2 =<span style=\"color: #000000;\"> j;\n        }\n    }\n}\n\n<\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">ht\u4e3a\u5730\u5740\u4f20\u9012\u7684\u5b58\u50a8\u54c8\u592b\u66fc\u6811\u7684\u6570\u7ec4\uff0cw\u4e3a\u5b58\u50a8\u7ed3\u70b9\u6743\u91cd\u503c\u7684\u6570\u7ec4\uff0cn\u4e3a\u7ed3\u70b9\u4e2a\u6570<\/span>\n<span style=\"color: #0000ff;\">void<\/span> createhuffmantree(huffmantree *ht, <span style=\"color: #0000ff;\">int<\/span> *w, <span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> n)\n{\n    <\/span><span style=\"color: #0000ff;\">if<\/span>(n&lt;=<span style=\"color: #800080;\">1<\/span>) <span style=\"color: #0000ff;\">return<\/span>; <span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\"> \u5982\u679c\u53ea\u6709\u4e00\u4e2a\u7f16\u7801\u5c31\u76f8\u5f53\u4e8e0<\/span>\n    <span style=\"color: #0000ff;\">int<\/span> m = <span style=\"color: #800080;\">2<\/span>*n-<span style=\"color: #800080;\">1<\/span>; <span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\"> \u54c8\u592b\u66fc\u6811\u603b\u8282\u70b9\u6570\uff0cn\u5c31\u662f\u53f6\u5b50\u7ed3\u70b9<\/span>\n    *ht = (huffmantree) <span style=\"color: #0000ff;\">malloc<\/span>((m+<span style=\"color: #800080;\">1<\/span>) * <span style=\"color: #0000ff;\">sizeof<\/span>(htnode)); <span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\"> 0\u53f7\u4f4d\u7f6e\u4e0d\u7528<\/span>\n    huffmantree p = *<span style=\"color: #000000;\">ht;\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\"> \u521d\u59cb\u5316\u54c8\u592b\u66fc\u6811\u4e2d\u7684\u6240\u6709\u7ed3\u70b9<\/span>\n    <span style=\"color: #0000ff;\">for<\/span>(<span style=\"color: #0000ff;\">int<\/span> i = <span style=\"color: #800080;\">1<\/span>; i &lt;= n; i++<span style=\"color: #000000;\">)\n    {\n        (p<\/span>+i)-&gt;weight = *(w+i-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">);\n        (p<\/span>+i)-&gt;parent = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n        (p<\/span>+i)-&gt;left = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n        (p<\/span>+i)-&gt;right = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n    }\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u4ece\u6811\u7ec4\u7684\u4e0b\u6807 n+1 \u5f00\u59cb\u521d\u59cb\u5316\u54c8\u592b\u66fc\u6811\u4e2d\u9664\u53f6\u5b50\u7ed3\u70b9\u5916\u7684\u7ed3\u70b9<\/span>\n    <span style=\"color: #0000ff;\">for<\/span>(<span style=\"color: #0000ff;\">int<\/span> i = n+<span style=\"color: #800080;\">1<\/span>; i &lt;= m; i++<span style=\"color: #000000;\">)\n    {\n        (p<\/span>+i)-&gt;weight = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n        (p<\/span>+i)-&gt;parent = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n        (p<\/span>+i)-&gt;left = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n        (p<\/span>+i)-&gt;right = <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n    }\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u6784\u5efa\u54c8\u592b\u66fc\u6811<\/span>\n    <span style=\"color: #0000ff;\">for<\/span>(<span style=\"color: #0000ff;\">int<\/span> i = n+<span style=\"color: #800080;\">1<\/span>; i &lt;= m; i++<span style=\"color: #000000;\">)\n    {\n        <\/span><span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> s1, s2;\n        select(<\/span>*ht, i-<span style=\"color: #800080;\">1<\/span>, &amp;s1, &amp;<span style=\"color: #000000;\">s2);\n        (<\/span>*ht)[s1].parent = (*ht)[s2].parent =<span style=\"color: #000000;\"> i;\n        (<\/span>*ht)[i].left =<span style=\"color: #000000;\"> s1;\n        (<\/span>*ht)[i].right =<span style=\"color: #000000;\"> s2;\n        (<\/span>*ht)[i].weight = (*ht)[s1].weight + (*<span style=\"color: #000000;\">ht)[s2].weight;\n    }\n}\n\n<\/span><span style=\"color: #008000;\">\/*<\/span><span style=\"color: #008000;\">\u4f7f\u7528\u7a0b\u5e8f\u6c42\u54c8\u592b\u66fc\u7f16\u7801\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\n1.\u4ece\u53f6\u5b50\u7ed3\u70b9\u4e00\u76f4\u627e\u5230\u6839\u7ed3\u70b9\uff0c\u9006\u5411\u8bb0\u5f55\u9014\u4e2d\u7ecf\u8fc7\u7684\u6807\u8bb0\u3002\n\u4f8b\u5982\uff0c\u56fe 3 \u4e2d\u5b57\u7b26 c \u7684\u54c8\u592b\u66fc\u7f16\u7801\u4ece\u7ed3\u70b9 c \u5f00\u59cb\u4e00\u76f4\u627e\u5230\u6839\u7ed3\u70b9\uff0c\n\u7ed3\u679c\u4e3a\uff1a0 1 1 \uff0c\u6240\u4ee5\u5b57\u7b26 c \u7684\u54c8\u592b\u66fc\u7f16\u7801\u4e3a\uff1a1 1 0\uff08\u9006\u5e8f\u8f93\u51fa\uff09\u3002\n2.\u4ece\u6839\u7ed3\u70b9\u51fa\u53d1\uff0c\u4e00\u76f4\u5230\u53f6\u5b50\u7ed3\u70b9\uff0c\u8bb0\u5f55\u9014\u4e2d\u7ecf\u8fc7\u7684\u6807\u8bb0\u3002\n\u4f8b\u5982\uff0c\u6c42\u56fe 3 \u4e2d\u5b57\u7b26 c \u7684\u54c8\u592b\u66fc\u7f16\u7801\uff0c\u5c31\u4ece\u6839\u7ed3\u70b9\u5f00\u59cb\uff0c\u4f9d\u6b21\u4e3a\uff1a1 1 0\u3002\n<\/span><span style=\"color: #008000;\">*\/<\/span>\n<span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">ht\u4e3a\u54c8\u592b\u66fc\u6811\uff0chc\u4e3a\u5b58\u50a8\u7ed3\u70b9\u54c8\u592b\u66fc\u7f16\u7801\u7684\u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4\uff0cn\u4e3a\u7ed3\u70b9\u7684\u4e2a\u6570<\/span>\n<span style=\"color: #0000ff;\">void<\/span> huffmancoding(huffmantree ht, huffmancode *hc,<span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> n){\n    <\/span>*hc = (huffmancode) <span style=\"color: #0000ff;\">malloc<\/span>((n+<span style=\"color: #800080;\">1<\/span>) * <span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #0000ff;\">char<\/span> *<span style=\"color: #000000;\">));\n    <\/span><span style=\"color: #0000ff;\">char<\/span> *cd = (<span style=\"color: #0000ff;\">char<\/span> *)<span style=\"color: #0000ff;\">malloc<\/span>(n*<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #0000ff;\">char<\/span>)); <span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5b58\u653e\u7ed3\u70b9\u54c8\u592b\u66fc\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u6570\u7ec4<\/span>\n    cd[n-<span style=\"color: #800080;\">1<\/span>] = <span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">\u0000<\/span><span style=\"color: #800000;\">'<\/span>;<span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26<\/span>\n   \n    <span style=\"color: #0000ff;\">for<\/span>(<span style=\"color: #0000ff;\">int<\/span> i=<span style=\"color: #800080;\">1<\/span>; i&lt;=n; i++<span style=\"color: #000000;\">){\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u4ece\u53f6\u5b50\u7ed3\u70b9\u51fa\u53d1\uff0c\u5f97\u5230\u7684\u54c8\u592b\u66fc\u7f16\u7801\u662f\u9006\u5e8f\u7684\uff0c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u9006\u5e8f\u5b58\u653e<\/span>\n        <span style=\"color: #0000ff;\">int<\/span> start = n-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5f53\u524d\u7ed3\u70b9\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e<\/span>\n        <span style=\"color: #0000ff;\">int<\/span> c =<span style=\"color: #000000;\"> i;\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5f53\u524d\u7ed3\u70b9\u7684\u7236\u7ed3\u70b9\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e<\/span>\n        <span style=\"color: #0000ff;\">int<\/span> j =<span style=\"color: #000000;\"> ht[i].parent;\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\"> \u4e00\u76f4\u5bfb\u627e\u5230\u6839\u7ed3\u70b9<\/span>\n        <span style=\"color: #0000ff;\">while<\/span>(j != <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">){\n            <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\"> \u5982\u679c\u8be5\u7ed3\u70b9\u662f\u7236\u7ed3\u70b9\u7684\u5de6\u5b69\u5b50\u5219\u5bf9\u5e94\u8def\u5f84\u7f16\u7801\u4e3a0\uff0c\u5426\u5219\u4e3a\u53f3\u5b69\u5b50\u7f16\u7801\u4e3a1<\/span>\n            <span style=\"color: #0000ff;\">if<\/span>(ht[j].left ==<span style=\"color: #000000;\"> c)\n                cd[<\/span>--start] = <span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">0<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #000000;\">;\n            <\/span><span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">\n                cd[<\/span>--start] = <span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">1<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #000000;\">;\n            <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u4ee5\u7236\u7ed3\u70b9\u4e3a\u5b69\u5b50\u7ed3\u70b9\uff0c\u7ee7\u7eed\u671d\u6811\u6839\u7684\u65b9\u5411\u904d\u5386<\/span>\n            c =<span style=\"color: #000000;\"> j;\n            j <\/span>=<span style=\"color: #000000;\"> ht[j].parent;\n        }\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u8df3\u51fa\u5faa\u73af\u540e\uff0ccd\u6570\u7ec4\u4e2d\u4ece\u4e0b\u6807 start \u5f00\u59cb\uff0c\u5b58\u653e\u7684\u5c31\u662f\u8be5\u7ed3\u70b9\u7684\u54c8\u592b\u66fc\u7f16\u7801<\/span>\n        (*hc)[i] = (<span style=\"color: #0000ff;\">char<\/span> *)<span style=\"color: #0000ff;\">malloc<\/span>((n-start)*<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #0000ff;\">char<\/span><span style=\"color: #000000;\">));\n        strcpy((<\/span>*hc)[i], &amp;<span style=\"color: #000000;\">cd[start]);\n    }\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u4f7f\u7528malloc\u7533\u8bf7\u7684cd\u52a8\u6001\u6570\u7ec4\u9700\u8981\u624b\u52a8\u91ca\u653e<\/span>\n    <span style=\"color: #0000ff;\">free<\/span><span style=\"color: #000000;\">(cd);\n}\n\n<\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">ht\u4e3a\u54c8\u592b\u66fc\u6811\uff0chc\u4e3a\u5b58\u50a8\u7ed3\u70b9\u54c8\u592b\u66fc\u7f16\u7801\u7684\u4e8c\u7ef4\u52a8\u6001\u6570\u7ec4\uff0cn\u4e3a\u7ed3\u70b9\u7684\u4e2a\u6570<\/span>\n<span style=\"color: #0000ff;\">void<\/span> huffmancoding1(huffmantree ht, huffmancode *hc,<span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> n){\n    <\/span>*hc = (huffmancode) <span style=\"color: #0000ff;\">malloc<\/span>((n+<span style=\"color: #800080;\">1<\/span>) * <span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #0000ff;\">char<\/span> *<span style=\"color: #000000;\">));\n    <\/span><span style=\"color: #0000ff;\">int<\/span> m=<span style=\"color: #800080;\">2<\/span>*n-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">int<\/span> p=<span style=\"color: #000000;\">m;\n    <\/span><span style=\"color: #0000ff;\">int<\/span> cdlen=<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000ff;\">char<\/span> *cd = (<span style=\"color: #0000ff;\">char<\/span> *)<span style=\"color: #0000ff;\">malloc<\/span>(n*<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #0000ff;\">char<\/span><span style=\"color: #000000;\">));\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5c06\u5404\u4e2a\u7ed3\u70b9\u7684\u6743\u91cd\u7528\u4e8e\u8bb0\u5f55\u8bbf\u95ee\u7ed3\u70b9\u7684\u6b21\u6570\uff0c\u9996\u5148\u521d\u59cb\u5316\u4e3a0<\/span>\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">int<\/span> i=<span style=\"color: #800080;\">1<\/span>; i&lt;=m; i++<span style=\"color: #000000;\">) {\n        ht[i].weight<\/span>=<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n    }\n    <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u4e00\u5f00\u59cb p \u521d\u59cb\u5316\u4e3a m\uff0c\u4e5f\u5c31\u662f\u4ece\u6811\u6839\u5f00\u59cb\u3002\u4e00\u76f4\u5230p\u4e3a0<\/span>\n    <span style=\"color: #0000ff;\">while<\/span><span style=\"color: #000000;\"> (p) {\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u5f53\u524d\u7ed3\u70b9\u4e00\u6b21\u6ca1\u6709\u8bbf\u95ee\uff0c\u8fdb\u5165\u8fd9\u4e2aif\u8bed\u53e5<\/span>\n        <span style=\"color: #0000ff;\">if<\/span> (ht[p].weight==<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">) {\n            ht[p].weight<\/span>=<span style=\"color: #800080;\">1<\/span>;<span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u91cd\u7f6e\u8bbf\u95ee\u6b21\u6570\u4e3a1\n            <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u6709\u5de6\u5b69\u5b50\uff0c\u5219\u8bbf\u95ee\u5de6\u5b69\u5b50\uff0c\u5e76\u4e14\u5b58\u50a8\u8d70\u8fc7\u7684\u6807\u8bb0\u4e3a0<\/span>\n            <span style=\"color: #0000ff;\">if<\/span> (ht[p].left!=<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">) {\n                p<\/span>=<span style=\"color: #000000;\">ht[p].left;\n                cd[cdlen<\/span>++]=<span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">0<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #000000;\">;\n            }\n            <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5f53\u524d\u7ed3\u70b9\u6ca1\u6709\u5de6\u5b69\u5b50\uff0c\u4e5f\u6ca1\u6709\u53f3\u5b69\u5b50\uff0c\u8bf4\u660e\u4e3a\u53f6\u5b50\u7ed3\u70b9\uff0c\u76f4\u63a5\u8bb0\u5f55\u54c8\u592b\u66fc\u7f16\u7801<\/span>\n            <span style=\"color: #0000ff;\">else<\/span> <span style=\"color: #0000ff;\">if<\/span>(ht[p].right==<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">){\n                (<\/span>*hc)[p]=(<span style=\"color: #0000ff;\">char<\/span>*)<span style=\"color: #0000ff;\">malloc<\/span>((cdlen+<span style=\"color: #800080;\">1<\/span>)*<span style=\"color: #0000ff;\">sizeof<\/span>(<span style=\"color: #0000ff;\">char<\/span><span style=\"color: #000000;\">));\n                cd[cdlen]<\/span>=<span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">\u0000<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #000000;\">;\n                strcpy((<\/span>*<span style=\"color: #000000;\">hc)[p], cd);\n            }\n        }\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679cweight\u4e3a1\uff0c\u8bf4\u660e\u8bbf\u95ee\u8fc7\u4e00\u6b21\uff0c\u5373\u662f\u4ece\u5176\u5de6\u5b69\u5b50\u8fd4\u56de\u7684<\/span>\n        <span style=\"color: #0000ff;\">else<\/span> <span style=\"color: #0000ff;\">if<\/span>(ht[p].weight==<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">){\n            ht[p].weight<\/span>=<span style=\"color: #800080;\">2<\/span>;<span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u8bbe\u7f6e\u8bbf\u95ee\u6b21\u6570\u4e3a2\n            <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u6709\u53f3\u5b69\u5b50\uff0c\u904d\u5386\u53f3\u5b69\u5b50\uff0c\u8bb0\u5f55\u6807\u8bb0\u503c 1<\/span>\n            <span style=\"color: #0000ff;\">if<\/span> (ht[p].right!=<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">) {\n                p<\/span>=<span style=\"color: #000000;\">ht[p].right;\n                cd[cdlen<\/span>++]=<span style=\"color: #800000;\">'<\/span><span style=\"color: #800000;\">1<\/span><span style=\"color: #800000;\">'<\/span><span style=\"color: #000000;\">;\n            }\n        }\n        <\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u5982\u679c\u8bbf\u95ee\u6b21\u6570\u4e3a 2\uff0c\u8bf4\u660e\u5de6\u53f3\u5b69\u5b50\u90fd\u904d\u5386\u5b8c\u4e86\uff0c\u8fd4\u56de\u7236\u7ed3\u70b9<\/span>\n        <span style=\"color: #0000ff;\">else<\/span><span style=\"color: #000000;\">{\n            ht[p].weight<\/span>=<span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n            p<\/span>=<span style=\"color: #000000;\">ht[p].parent;\n            <\/span>--<span style=\"color: #000000;\">cdlen;\n        }\n    }\n}\n\n<\/span><span style=\"color: #008000;\">\/\/<\/span><span style=\"color: #008000;\">\u6253\u5370\u54c8\u592b\u66fc\u7f16\u7801\u7684\u51fd\u6570<\/span>\n<span style=\"color: #0000ff;\">void<\/span> printhuffmancode(huffmancode htable,<span style=\"color: #0000ff;\">int<\/span> *w,<span style=\"color: #0000ff;\">int<\/span><span style=\"color: #000000;\"> n)\n{\n    printf(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">huffman code : n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #000000;\">);\n    <\/span><span style=\"color: #0000ff;\">for<\/span>(<span style=\"color: #0000ff;\">int<\/span> i = <span style=\"color: #800080;\">1<\/span>; i &lt;= n; i++<span style=\"color: #000000;\">)\n        printf(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #800000;\">%d code = %sn<\/span><span style=\"color: #800000;\">\"<\/span>,w[i-<span style=\"color: #800080;\">1<\/span><span style=\"color: #000000;\">], htable[i]);\n}\n<\/span><span style=\"color: #0000ff;\">int<\/span> main(<span style=\"color: #0000ff;\">void<\/span><span style=\"color: #000000;\">)\n{\n    <\/span><span style=\"color: #0000ff;\">int<\/span> w[<span style=\"color: #800080;\">5<\/span>] = {<span style=\"color: #800080;\">2<\/span>, <span style=\"color: #800080;\">8<\/span>, <span style=\"color: #800080;\">7<\/span>, <span style=\"color: #800080;\">6<\/span>, <span style=\"color: #800080;\">5<\/span><span style=\"color: #000000;\">};\n    <\/span><span style=\"color: #0000ff;\">int<\/span> n = <span style=\"color: #800080;\">5<\/span><span style=\"color: #000000;\">;\n    huffmantree htree;\n    huffmancode htable;\n    createhuffmantree(<\/span>&amp;<span style=\"color: #000000;\">htree, w, n);\n    huffmancoding(htree, <\/span>&amp;<span style=\"color: #000000;\">htable, n);\n    printhuffmancode(htable,w, n);\n    <\/span><span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #800080;\">0<\/span><span style=\"color: #000000;\">;\n}<\/span><\/pre>\n<\/div>\n<pre>huffman<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include&lt;bits\/stdc++.h&gt; #define maxsize 100  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u54c8\u592b\u66fc\u6811 - imwarming<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/imwarming.com\/?p=161\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u54c8\u592b\u66fc\u6811 - imwarming\" \/>\n<meta property=\"og:description\" content=\"#include&lt;bits\/stdc++.h&gt; #define maxsize 100 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/imwarming.com\/?p=161\" \/>\n<meta property=\"og:site_name\" content=\"imwarming\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-27T08:10:00+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"warming\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/imwarming.com\/#website\",\"url\":\"https:\/\/imwarming.com\/\",\"name\":\"imwarming\",\"description\":\"\u6c38\u8fdc\u5e74\u8f7b\uff0c\u6c38\u8fdc\u70ed\u6cea\u76c8\u7736\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/imwarming.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/imwarming.com\/?p=161#webpage\",\"url\":\"https:\/\/imwarming.com\/?p=161\",\"name\":\"\u54c8\u592b\u66fc\u6811 - imwarming\",\"isPartOf\":{\"@id\":\"https:\/\/imwarming.com\/#website\"},\"datePublished\":\"2019-12-27T08:10:00+00:00\",\"dateModified\":\"2019-12-27T08:10:00+00:00\",\"author\":{\"@id\":\"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea\"},\"breadcrumb\":{\"@id\":\"https:\/\/imwarming.com\/?p=161#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/imwarming.com\/?p=161\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/imwarming.com\/?p=161#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/imwarming.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u54c8\u592b\u66fc\u6811\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea\",\"name\":\"warming\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/imwarming.com\/#personlogo\",\"inLanguage\":\"zh-Hans\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g\",\"caption\":\"warming\"},\"sameAs\":[\"https:\/\/imwarming.com\"],\"url\":\"https:\/\/imwarming.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u54c8\u592b\u66fc\u6811 - imwarming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/imwarming.com\/?p=161","og_locale":"zh_CN","og_type":"article","og_title":"\u54c8\u592b\u66fc\u6811 - imwarming","og_description":"#include&lt;bits\/stdc++.h&gt; #define maxsize 100 [&hellip;]","og_url":"https:\/\/imwarming.com\/?p=161","og_site_name":"imwarming","article_published_time":"2019-12-27T08:10:00+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"warming","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/imwarming.com\/#website","url":"https:\/\/imwarming.com\/","name":"imwarming","description":"\u6c38\u8fdc\u5e74\u8f7b\uff0c\u6c38\u8fdc\u70ed\u6cea\u76c8\u7736","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/imwarming.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"https:\/\/imwarming.com\/?p=161#webpage","url":"https:\/\/imwarming.com\/?p=161","name":"\u54c8\u592b\u66fc\u6811 - imwarming","isPartOf":{"@id":"https:\/\/imwarming.com\/#website"},"datePublished":"2019-12-27T08:10:00+00:00","dateModified":"2019-12-27T08:10:00+00:00","author":{"@id":"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea"},"breadcrumb":{"@id":"https:\/\/imwarming.com\/?p=161#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/imwarming.com\/?p=161"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/imwarming.com\/?p=161#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/imwarming.com\/"},{"@type":"ListItem","position":2,"name":"\u54c8\u592b\u66fc\u6811"}]},{"@type":"Person","@id":"https:\/\/imwarming.com\/#\/schema\/person\/9d76869a558bac6dd0d6d58f420ee8ea","name":"warming","image":{"@type":"ImageObject","@id":"https:\/\/imwarming.com\/#personlogo","inLanguage":"zh-Hans","url":"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c4a913eed88f7601b76bbf2b103472621195b6fa2f742af89b5ea185b60e7cff?s=96&d=mm&r=g","caption":"warming"},"sameAs":["https:\/\/imwarming.com"],"url":"https:\/\/imwarming.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":0,"href":"https:\/\/imwarming.com\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imwarming.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}