Heffman (optimal binary tree)

Source: Internet
Author: User

。 An empty statement semicolon is missing when the minimum s1s2 is selected: Commissioning for a long time. Hang Daddy.

This is the best coding strategy to achieve the fewest coding

1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <math.h>5#include <queue>6#include <algorithm>7 using namespacestd;8typedefstruct9 {TenUnsignedintweight; OneUnsignedintParent,lchild,rchild; A}htnode,*Huffmantree; -typedefChar*Huffmancode; - void Select(Huffmantree *ht,intNint*S1,int*S2)//Huffman Tree Pointer the { -     inti; -      for(i=1;i<=n&& (*HT) [i].parent!=0; i++);//!!!!!!!! Pit Daddy -*s1=i; +      for(i=1; i<=n;i++) -         if((*HT) [i].parent==0&& ((*HT) [i].weight< (*HT) [*s1].weight)) +*s1=i; A      for(i=1; i<=n;i++) at         if((*HT) [i].parent==0&&i!=*S1) -              Break; -*s2=i; -      for(i=1; i<=n;i++) -         if((*HT) [i].parent==0&&i!=*s1&& ((*HT) [i].weight< (*HT) [*s2].weight)) -*s2=i; in } - voidHuffmancoding (Huffmantree *ht,huffmancode *HC,int*w,intN) to { +     Char*cd; -     intm,i,start,f,s1,s2; theUnsignedintC; *     if(n<=1)return; $m=2*n-1;Panax Notoginseng*ht= (Huffmantree)malloc((m+1)*sizeof(Htnode)); -      for(i=1; i<=n;i++) the     { +(*HT) [i].weight=W[i]; A(*HT) [i].lchild=0; the(*HT) [i].rchild=0; +(*HT) [i].parent=0; -}//Assign Value $      for(i=n+1; i<=m;i++)//back space Clear 0 $     { -(*HT) [i].weight=0; -(*HT) [i].parent=0; the(*HT) [i].lchild=0; -(*HT) [i].rchild=0;Wuyi     } the      for(i=n+1; i<=m;i++) -     { Wu         Select(ht,i-1, &AMP;S1,&AMP;S2);//find the smallest s1,s2 junction. -(*HT) [S1].parent=i; (*HT) [s2].parent=i; About(*HT) [I].lchild=s1; (*HT) [i].rchild=S2; $(*HT) [I].weight= (*HT) [S1].weight+ (*HT) [S2].weight; -     } -Hc= (huffmancode*)malloc((n+1)*sizeof(Char*));//N-Coded head pointers -Cd= (Char*)malloc(nsizeof(Char));//Current encoding Space Acd[n-1]=' /';//right-to-left coding, first put end of end flag +      for(i=1; i<=n;i++)//find the Huffman code of Knot point the     { -start=n-1; $          for(c=i,f= (*HT) [i].parent;f!=0; c=f,f= (*HT) [F].parent] the         { the             if((*HT) [f].lchild==c) thecd[--start]='0'; the             Else -cd[--start]='1'; in         } theHc[i]= (Char*)malloc((N-start) *sizeof(Char)); thestrcpy (hc[i],&Cd[start]); About     } the      Free(CD); the      for(i=1; i<= -; i++) theprintf"%c---%s\n", i+ the, Hc[i]); + } - Chars[ +]; the inta[ -]={0};Bayi intMain () the { the Huffmancode HC; - Huffmantree ht; -     intN,t,i=0, J,l,r,len; the     int*b; the     floatnum=0; the gets (s); thet=strlen (s); -B= (int*)malloc( -*sizeof(int)); the      while(i<t) the     { the        if(s[i]>='A'&&s[i]<='Z')94{a[s[i]- -]++;num++;} the        if(s[i]>='a'&&s[i]<='Z') the{a[s[i]- the]++;num++;} thei++;98     } About      for(i=1; i<= -; i++) -     {101b[i]= (a[i]/num) *10000;102printf"%c:%d%.3f---%d\n", i+ the, a[i],a[i]/num,b[i]);103     }104Huffmancoding (&ht,&hc,b, -); the     return 0;106}

Heffman (optimal binary tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.