免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
赫夫曼編碼
#include <stdio.h>
#include<conio.h>
#include<string.h>
#define N 10     /*待編碼字符的個(gè)數(shù),即樹(shù)中葉結(jié)點(diǎn)的最大個(gè)數(shù)*/
#define M 2*N-1  /*樹(shù)中總的結(jié)點(diǎn)數(shù)目*/
typedef struct{
  unsigned int weight;
  unsigned int parent,lchild,rchild;
  }HTNode;  /*樹(shù)中結(jié)點(diǎn)的結(jié)構(gòu)*/
typedef struct {
  char data;     /*待編碼的字符*/
  int weight;    /*字符的權(quán)值  */
  char code[N];  /*字符的編碼 */
} HTCode;
void Init(HTCode hc[],int *n){
  /*初始化,讀入待編碼字符的個(gè)數(shù)n,從鍵盤輸入n個(gè)字符和n個(gè)權(quán)值*/
int i;
  printf("\n輸入待編碼字符的個(gè)數(shù)n:n=");
  scanf("%d",&(*n));
  printf("\n依次輸入 %d個(gè)字符\n",*n);
  for (i=1;i<=*n;i++)
  { hc[i].data=getch();
  printf("%c",hc[i].data);}
  printf("\n依次輸入 %d 個(gè)字符的權(quán)值,以空格隔開(kāi)\n",*n);
  for (i=1;i<=*n;i++) scanf("%d",&(hc[i].weight));
}

void Select(HTNode ht[],int k,int *s1,int *s2){
/*ht[1…k]中選擇parent為0,并且weight最小的兩個(gè)結(jié)點(diǎn)
其序號(hào)由指針變量s1,s2指向*/
  int i;
  for (i=1;i<=k && ht[i].parent!=0 ;i++);
  *s1=i;
  for (i=1;i<=k;i++)
    if (ht[i].parent==0 && ht[i].weight<ht[*s1].weight) *s1=i;
  for (i=1; i<=k ; i++)
    if (ht[i].parent==0 && i!=*s1) break;
  *s2=i;
  for (i=1;i<=k;i++)
    if ( ht[i].parent==0 && i!=*s1 && ht[i].weight<ht[*s2].weight) *s2=i;
}
void HuffmanCoding(HTNode ht[],HTCode hc[],int n){
/*構(gòu)造Huffman樹(shù)ht,并求出n個(gè)字符的編碼*/
  char cd[N];
  int i,j,m,c,f,s1,s2,start;
  m=2*n-1;
  for (i=1;i<=m;i++){
   if (i<=n)  ht[i].weight=hc[i].weight;
   else ht[i].weight=0;
   ht[i].parent=ht[i].lchild=ht[i].rchild=0;
   }
 for (i=n+1;i<=m;i++){
    Select(ht,i-1,&s1,&s2);
    ht[s1].parent=i;   ht[s2].parent=i;
    ht[i].lchild=s1;   ht[i].rchild=s2;
    ht[i].weight=ht[s1].weight+ht[s2].weight;
  }
  cd[n-1]='\0';
  for (i=1;i<=n;i++) {
    start=n-1;
    for (c=i,f=ht[i].parent;f;c=f,f=ht[f].parent)
      if (ht[f].lchild==c) cd[--start]='0';
      else cd[--start]='1';
    strcpy(hc[i].code,&cd[start]);
  }
}
void main(){
 int i,m,n,w[N+1];
 HTNode ht[M+1];
 HTCode hc[N+1];
 Init(hc,&n);           /*初始化*/
 HuffmanCoding(ht,hc,n);/*構(gòu)造Huffman樹(shù),并形成字符的編碼*/
/*輸出字符的編碼*/
 for (i=1;i<=n;i++)printf("\n%c --- %s",hc[i].data,hc[i].code);
}
 
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
哈夫曼編碼
第16講n
霍夫曼編碼,以及最優(yōu)二叉樹(shù)(霍夫曼建樹(shù))(算法導(dǎo)論學(xué)習(xí))
搜搜問(wèn)問(wèn)
二叉樹(shù)的應(yīng)用詳解
C語(yǔ)言實(shí)現(xiàn)哈夫曼編碼與譯碼
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服