{"id":128,"date":"2011-10-25T21:03:47","date_gmt":"2011-10-25T15:03:47","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=128"},"modified":"2011-10-25T21:03:47","modified_gmt":"2011-10-25T15:03:47","slug":"huffman-coding","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=128","title":{"rendered":"Huffman coding"},"content":{"rendered":"<p>[code]<br \/>\n\/\/Originally FOUND in Internet<br \/>\n\/\/by RIT2009061  vikesh iiita<\/p>\n<p>\/\/Author: Ujjal Suttra DHar  [CSE&#8217;08,RUET]<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;algorithm&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>struct node {<br \/>\n\tint weight;<br \/>\n\tchar value;<br \/>\n\tconst node *child0;<br \/>\n\tconst node *child1;<\/p>\n<p>\tnode( int c, int i ) {<br \/>\n\t\tvalue = c;<br \/>\n\t\tweight = i;<br \/>\n\t\tchild0 = 0;<br \/>\n\t\tchild1 = 0;<br \/>\n\t}<\/p>\n<p>\tnode( const node* c0, const node* c1 ) {<br \/>\n\t\tvalue = 0;<br \/>\n\t\tweight = c0-&gt;weight + c1-&gt;weight;<br \/>\n\t\tchild0 = c0;<br \/>\n\t\tchild1 = c1;<br \/>\n\t}<\/p>\n<p>\tbool operator&lt;( const node &amp;a ) const {<br \/>\n\t\tif(weight ==a.weight)<br \/>\n\t\t       return value&gt;a.value;<br \/>\n       else<br \/>\n\t\t     return weight &gt;a.weight;<br \/>\n\t}<\/p>\n<p>void traverse(string code=&quot;&quot;) const{<\/p>\n<p>\tif ( child0 ) {<br \/>\n\t\tchild0-&gt;traverse( code + &#8216;0&#8217; );<br \/>\n\t\tchild1-&gt;traverse( code + &#8216;1&#8217; );<br \/>\n\t} else {<br \/>\n\t\tcout &lt;&lt;&quot; &quot; &lt;&lt;value &lt;&lt;&quot;\t  &quot;;<br \/>\n\t\tcout &lt;&lt;weight;<br \/>\n\t\tcout &lt;&lt;&quot;\t &quot; &lt;&lt;code &lt;&lt;endl;<br \/>\n\t}<br \/>\n}<\/p>\n<p>};<\/p>\n<p>\/*<br \/>\nvoid count_chars( int *counts )<br \/>\n{<br \/>\n\tfor ( int i = 0 ; i &lt;256 ; i++ )<br \/>\n\t\tcounts[ i ] = 0;<\/p>\n<p>freopen(&quot;input.dat&quot;,&quot;r&quot;,stdin);<\/p>\n<p>char c;<br \/>\nwhile((c=getchar())!=EOF)<br \/>\n   counts[c]++;<br \/>\n}<\/p>\n<p>*\/<\/p>\n<p>int main()<br \/>\n{<\/p>\n<p>freopen(&quot;input.dat&quot;,&quot;r&quot;,stdin);<\/p>\n<p>\tint counts[ 256 ];<br \/>\n\tchar a[1000];<\/p>\n<p>gets(a);<\/p>\n<p>for ( int i = 0 ; i &lt;256 ; i++ )<br \/>\n\t\tcounts[ i ] = 0;<\/p>\n<p>for(int i=0;i&lt;strlen(a);i++)<br \/>\n\tcounts[a[i]]++;<\/p>\n<p>\tpriority_queue &lt; node &gt; q;<\/p>\n<p>\tfor (int i = 0 ; i &lt;256 ; i++ )<br \/>\n\t\tif ( counts[ i ] )<br \/>\n\t\t\tq.push( node( i, counts[ i ] ) );<\/p>\n<p>\/\/if you wanna show the queue<br \/>\n\/*while(!q.empty()){<br \/>\n    cout&lt;&lt;q.top().value&lt;&lt;endl;<br \/>\n    q.pop();<br \/>\n    }<br \/>\n*\/<\/p>\n<p>\twhile ( q.size() &gt;1 ) {<br \/>\n\t\tnode *child0 = new node( q.top() );<br \/>\n\t\tq.pop();<br \/>\n\t\tnode *child1 = new node( q.top() );<br \/>\n\t\tq.pop();<br \/>\n\t\tq.push( node( child0, child1 ) );<br \/>\n\t}<\/p>\n<p>\tcout &lt;&lt;&quot;CHAR  FREQUENCY  HOFFMAN-CODE&quot; &lt;&lt;endl;<br \/>\n\tq.top().traverse();<br \/>\n\treturn 0;<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[code] \/\/Originally FOUND in Internet \/\/by RIT2009061 vikesh iiita \/\/Author: Ujjal Suttra DHar [CSE&#8217;08,RUET] #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;queue&gt; #include &lt;algorithm&gt; using namespace std; struct node { int weight; char value; const node *child0; const node *child1; node( int c, int i ) { value = c; weight = i; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=128\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Huffman coding&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-programming-with-c-2"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":0,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}