當前位置:首頁 » 操作系統 » 哈夫曼演算法

哈夫曼演算法

發布時間: 2022-02-09 18:45:24

Ⅰ 設計程序以實現構造哈夫曼樹的哈夫曼演算法(C++),要求如下:

#include <stdio.h>
#include <stdlib.h>/*哈夫曼樹建立、哈夫曼編碼演算法的實現*/
#include <string.h>
typedef char* HuffmanCode;/*動態分配數組,存儲哈夫曼編碼*/
typedef struct
{
unsigned int weight ; /* 用來存放各個結點的權值*/
unsigned int parent, LChild,RChild ; /*指向雙親、孩子結點的指針*/
}HTNode, * HuffmanTree; /*動態分配數組,存儲哈夫曼樹*/
void select(HuffmanTree *ht,int n, int *s1, int *s2)
{
int i;
int min;
for(i=1; i<=n; i++)
{
if((*ht)[i].parent == 0)
{
min = i;
i = n+1;
}
}
for(i=1; i<=n; i++)
{
if((*ht)[i].parent == 0)
{
if((*ht)[i].weight < (*ht)[min].weight)
min = i;
}
}
*s1 = min;
for(i=1; i<=n; i++)
{
if((*ht)[i].parent == 0 && i!=(*s1))
{
min = i;
i = n+1;
}
}
for(i=1; i<=n; i++)
{
if((*ht)[i].parent == 0 && i!=(*s1))
{
if((*ht)[i].weight < (*ht)[min].weight)
min = i;
}
}
*s2 = min;
}
void CrtHuffmanTree(HuffmanTree *ht , int *w, int n)
{ /* w存放已知的n個權值,構造哈夫曼樹ht */
int m,i;
int s1,s2;
m=2*n-1;
*ht=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); /*0號單元未使用*/
for(i=1;i<=n;i++)
{/*1-n號放葉子結點,初始化*/
(*ht)[i].weight = w[i];
(*ht)[i].LChild = 0;
(*ht)[i].parent = 0;
(*ht)[i].RChild = 0;
}
for(i=n+1;i<=m;i++)
{
(*ht)[i].weight = 0;
(*ht)[i].LChild = 0;
(*ht)[i].parent = 0;
(*ht)[i].RChild = 0;
} /*非葉子結點初始化*/
/* ------------初始化完畢!對應演算法步驟1---------*/

for(i=n+1;i<=m;i++) /*創建非葉子結點,建哈夫曼樹*/
{ /*在(*ht)[1]~(*ht)[i-1]的范圍內選擇兩個parent為0且weight最小的結點,其序號分別賦值給s1、s2返回*/
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;
}
}/*哈夫曼樹建立完畢*/
void outputHuffman(HuffmanTree HT, int m)
{
if(m!=0)
{
printf("%d ", HT[m].weight);
outputHuffman(HT,HT[m].LChild);
outputHuffman(HT,HT[m].RChild);
}
}
void CrtHuffmanCode(HuffmanTree *ht, HuffmanCode *hc, int n)
/*從葉子結點到根,逆向求每個葉子結點對應的哈夫曼編碼*/
{
char *cd;
int i;
unsigned int c;
int start;
int p;
hc=(HuffmanCode *)malloc((n+1)*sizeof(char *)); /*分配n個編碼的頭指針*/
cd=(char * )malloc(n * sizeof(char )); /*分配求當前編碼的工作空間*/
cd[n-1]='\0'; /*從右向左逐位存放編碼,首先存放編碼結束符*/
for(i=1;i<=n;i++) /*求n個葉子結點對應的哈夫曼編碼*/
{
start=n-1; /*初始化編碼起始指針*/
for(c=i,p=(*ht)[i].parent; p!=0; c=p,p=(*ht)[p].parent) /*從葉子到根結點求編碼*/
if( (*ht)[p].LChild == c)
cd[--start]='0'; /*左分支標0*/
else
cd[--start]='1'; /*右分支標1*/
hc[i]=(char *)malloc((n-start)*sizeof(char)); /*為第i個編碼分配空間*/
strcpy(hc[i],&cd[start]);
}
free(cd);
for(i=1;i<=n;i++)
printf("%d編碼為%s\n",(*ht)[i].weight,hc[i]);
}

void main()
{
HuffmanTree HT;
HuffmanCode HC;
int *w;
int i,n; // the number of elements;
int wei; // the weight of a element;
int m;

printf("input the total number of the Huffman Tree:" );
scanf("%d",&n);
w=(int *)malloc((n+1)*sizeof(int));
for(i=1;i<=n;i++)
{
printf("input the %d element's weight:",i);
fflush(stdin);
scanf("%d",&wei);
w[i]=wei;
}
CrtHuffmanTree(&HT,w,n);
m = 2*n-1;
outputHuffman(HT,m);
printf("\n");
CrtHuffmanCode(&HT,&HC,n);

}

Ⅱ 哈夫曼演算法

計算過程如圖所示:

Ⅲ 哈夫曼樹與哈夫曼碼

哈夫曼樹

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXNODE 100
#define MAXNUM 50
#define MAXINT 60000

char hc[MAXNUM-1][MAXNUM];

typedef struct HtNode
{
int weight;
int parent, lchild, rchild;
}HtNode;

typedef struct HtTree
{
struct HtNode ht[MAXNODE+1];
int root;
}HtTree, *PHtTree;

void Select (PHtTree pht, int pos, int *x1, int *x2)
{
int j;
int m1 = MAXINT, m2 = MAXINT;
for (j=1; j<pos; j++)
{
if (pht->ht[j].weight<=m1 && pht->ht[j].parent == 0)
{
m2 = m1;
*x2 = *x1;
m1 = pht->ht[j].weight;
*x1 = j;
}
else if (pht->ht[j].weight<m2 && pht->ht[j].parent == 0)
{
m2 = pht->ht[j].weight;
*x2 = j;
}
}
}

PHtTree huffman(int n, int *w)
{
PHtTree pht;
int i, x1=0, x2=0;
pht = (PHtTree) malloc (sizeof (struct HtTree));
for( i=1; i<=2*n - 1; i++ )
{
pht->ht[i].lchild = 0;
pht->ht[i].rchild = 0;
pht->ht[i].parent = 0;
if (i<=n) pht->ht[i].weight = w[i];
else pht->ht[i].weight = 0;
}
for( i=1; i < n ; i++ )
{
Select(pht, n+i, &x1, &x2);
pht->ht[x1].parent = n + i;
pht->ht[x2].parent = n + i;
pht->ht[n+i].weight = pht->ht[x1].weight + pht->ht[x2].weight;
pht->ht[n+i].lchild = x1;
pht->ht[n+i].rchild = x2;
pht->root = n+i;
}
return pht;
}

void huffmancode(PHtTree huff,int *w,char *ch,int n)
{
int start,c,i,j,f;
char code[MAXNUM];

printf("\n各個結點的哈夫曼編碼為:\n\n");
code[n-1]='\0';
for(i=1;i<=n;i++)
{
start=n-1;
for(c=i,f=huff->ht[i].parent;f!=0;c=f,f=huff->ht[f].parent)
{
if(huff->ht[f].lchild==c)
code[--start]='0';
else
code[--start]='1';
}
strcpy(hc[i],&code[start]);
printf(" %c --> %s",ch[i],hc[i]);
if(i%2==0)
printf("\n");
else
for(j=1;j<=4-(int)(strlen(hc[i])/8);j++)
printf("\t");
}
printf("\n\n");
}

void tohuffmancode(char *ch,int n)
{
int i=0,j;
int charnum[MAXNUM]={0};
char c;
printf("\n請輸入需要編碼的句子:\n");
c=getchar();
if(c==32) c='_';
while(c!=10)
{
for (j=1;j<=n;j++)
if(c==ch[j])
charnum[++i]=j;
c=getchar();
if(c==32) c='_';

}
printf("\n其哈夫曼編碼為:\n");
for (j=1;j<=i;j++)
{
printf("%s",hc[charnum[j]]);
}
printf("\n");
}

void decode(PHtTree huff,char *ch)
{
int p;
char c;
p=huff->root;
printf("\n請輸入需要解碼的一串哈夫曼編碼:\n");
c=getchar();
while(c!=10)
{
if(c=='0')
{
p=huff->ht[p].lchild;
if(huff->ht[p].lchild==0||huff->ht[p].rchild==0)
{
printf("%c",ch[p]);
p=huff->root;
}
}
if(c=='1')
{
p=huff->ht[p].rchild;
if(huff->ht[p].lchild==0||huff->ht[p].rchild==0)
{
printf("%c",ch[p]);
p=huff->root;
}
}
c=getchar();
}
printf("\n");
}

void print(void)
{
printf("\n\n\t****************************************************************\n\n");
printf("\t請選擇要進行的操作:\n\n\n");
printf("\t\t1----------------------------譯 碼\n");
printf("\t\t2----------------------------解 碼\n");
printf("\t\t3----------------------------重新輸入權值\n");
printf("\t\t0----------------------------退 出\n");
printf("\n\n\t>>>");
}

void main()
{
int i,choose,num,n,*w;
char *ch;
int weight2[MAXNUM]={0},weight1[28]={0,186,64,13,22,32,103,21,15,47,57,1,5,32,20,57,63,15,1,48,51,80,23,8,18,1,16,1};
char char2[MAXNUM]={' '},chars1[28]={' ','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',};
PHtTree huff;

num=27;

XX: printf("\n\n\t\t\t\t哈夫曼編碼\n\n");
printf("\n\t******************************************************************\n\n");
printf("\t請選擇權值輸入的方式:\n\n");
printf("\t1.默認結點與權值,其中包括空格(程序輸出時將以""_""代替)和小寫字母a~b\n");
printf("\t2.逐個輸入結點和結點的權值\n");
printf("\n\t>>>");

scanf("%d",&choose);
switch(choose)
{
case 1:
n=num;w=weight1;ch=chars1;break;
case 2:
{
w=weight2;ch=char2;
printf("請輸入結點個數:");
scanf("%d",&n);
getchar();
for(i=1;i<=n;i++)
{
printf("\n請輸入第%d個結點:",i);
scanf("%c",&char2[i]);
if(char2[i]==32)
char2[i]='_';
printf("\n請輸入第%d個結點的權值:",i);
scanf("%d",&weight2[i]);
getchar();
}
}
break;
}
huff=huffman(n,w);
huffmancode(huff,w,ch,n);
print();
scanf("%d",&choose);
getchar();
while(choose)
{
switch(choose)
{
case 1: tohuffmancode(ch,n);break;
case 2: decode(huff,ch);break;
case 3: goto XX;break;
}
print();
scanf("%d",&choose);
getchar();
}

}

Ⅳ 哈夫曼樹及哈夫曼編碼的C程序實現(數據結構題)

去年做的課程設計,有什麼不合要求的自己改改

#include<string.h>
#include<stdlib.h>
#include<stdio.h>

int m,s1,s2;

typedef struct {
unsigned int weight;
unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree; //動態分配數組存儲哈夫曼樹
typedef char *HuffmanCode; //動態分配數組存儲哈夫曼編碼表

void Select(HuffmanTree HT,int n) {
int i,j;
for(i = 1;i <= n;i++)
if(!HT[i].parent){s1 = i;break;}
for(j = i+1;j <= n;j++)
if(!HT[j].parent){s2 = j;break;}
for(i = 1;i <= n;i++)
if((HT[s1].weight>HT[i].weight)&&(!HT[i].parent)&&(s2!=i))s1=i;
for(j = 1;j <= n;j++)
if((HT[s2].weight>HT[j].weight)&&(!HT[j].parent)&&(s1!=j))s2=j;
}

void HuffmanCoding(HuffmanTree &HT, HuffmanCode HC[], int *w, int n) {
// 演算法6.13
// w存放n個字元的權值(均>0),構造哈夫曼樹HT,
// 並求出n個字元的哈夫曼編碼HC
int i, j;
char *cd;
int p;
int cdlen;

if (n<=1) return;
m = 2 * n - 1;
HT = (HuffmanTree)malloc((m+1) * sizeof(HTNode)); // 0號單元未用
for (i=1; i<=n; i++) { //初始化
HT[i].weight=w[i-1];
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for (i=n+1; i<=m; i++) { //初始化
HT[i].weight=0;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
puts("\n哈夫曼樹的構造過程如下所示:");
printf("HT初態:\n 結點 weight parent lchild rchild");
for (i=1; i<=m; i++)
printf("\n%4d%8d%8d%8d%8d",i,HT[i].weight,
HT[i].parent,HT[i].lchild, HT[i].rchild);
printf(" 按任意鍵,繼續 ...");
getchar();
for (i=n+1; i<=m; i++) { // 建哈夫曼樹
// 在HT[1..i-1]中選擇parent為0且weight最小的兩個結點,
// 其序號分別為s1和s2。
Select(HT, i-1);
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;
printf("\nselect: s1=%d s2=%d\n", s1, s2);
printf(" 結點 weight parent lchild rchild");
for (j=1; j<=i; j++)
printf("\n%4d%8d%8d%8d%8d",j,HT[j].weight,
HT[j].parent,HT[j].lchild, HT[j].rchild);
printf(" 按任意鍵,繼續 ...");
getchar();
}

//------無棧非遞歸遍歷哈夫曼樹,求哈夫曼編碼
cd = (char *)malloc(n*sizeof(char)); // 分配求編碼的工作空間
p = m; cdlen = 0;
for (i=1; i<=m; ++i) // 遍歷哈夫曼樹時用作結點狀態標志
HT[i].weight = 0;
while (p) {
if (HT[p].weight==0) { // 向左
HT[p].weight = 1;
if (HT[p].lchild != 0) { p = HT[p].lchild; cd[cdlen++] ='0'; }
else if (HT[p].rchild == 0) { // 登記葉子結點的字元的編碼
HC[p] = (char *)malloc((cdlen+1) * sizeof(char));
cd[cdlen] ='\0'; strcpy(HC[p], cd); // 復制編碼(串)
}
} else if (HT[p].weight==1) { // 向右
HT[p].weight = 2;
if (HT[p].rchild != 0) { p = HT[p].rchild; cd[cdlen++] ='1'; }
} else { // HT[p].weight==2,退回退到父結點,編碼長度減1
HT[p].weight = 0; p = HT[p].parent; --cdlen;
}
}
} // HuffmanCoding
void main() {
HuffmanTree HT;HuffmanCode *HC;int *w,n,i;
puts("輸入結點數:");
scanf("%d",&n);
HC = (HuffmanCode *)malloc(n*sizeof(HuffmanCode));
w = (int *)malloc(n*sizeof(int));
printf("輸入%d個結點的權值\n",n);
for(i = 0;i < n;i++)
scanf("%d",&w[i]);
HuffmanCoding(HT,HC,w,n);
puts("\n各結點的哈夫曼編碼:");
for(i = 1;i <= n;i++)
printf("%2d(%4d):%s\n",i,w[i-1],HC[i]);
getchar();
}

Ⅳ 哈夫曼樹的構造演算法

/*-------------------------------------------------------------------------
*Name:哈夫曼編碼源代碼。
*Date:2011.04.16
*Author:JeffreyHill+Jezze(解碼部分)
*在Win-TC下測試通過
*實現過程:著先通過HuffmanTree()函數構造哈夫曼樹,然後在主函數main()中
*自底向上開始(也就是從數組序號為零的結點開始)向上層層判斷,若在
*父結點左側,則置碼為0,若在右側,則置碼為1。最後輸出生成的編碼。
*------------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>

#defineMAXBIT100
#defineMAXVALUE10000
#defineMAXLEAF30
#defineMAXNODEMAXLEAF*2-1

typedefstruct
{
intbit[MAXBIT];
intstart;
}HCodeType;/*編碼結構體*/
typedefstruct
{
intweight;
intparent;
intlchild;
intrchild;
intvalue;
}HNodeType;/*結點結構體*/

/*構造一顆哈夫曼樹*/
voidHuffmanTree(HNodeTypeHuffNode[MAXNODE],intn)
{
/*i、j:循環變數,m1、m2:構造哈夫曼樹不同過程中兩個最小權值結點的權值,
x1、x2:構造哈夫曼樹不同過程中兩個最小權值結點在數組中的序號。*/
inti,j,m1,m2,x1,x2;
/*初始化存放哈夫曼樹數組HuffNode[]中的結點*/
for(i=0;i<2*n-1;i++)
{
HuffNode[i].weight=0;//權值
HuffNode[i].parent=-1;
HuffNode[i].lchild=-1;
HuffNode[i].rchild=-1;
HuffNode[i].value=i;//實際值,可根據情況替換為字母
}/*endfor*/

/*輸入n個葉子結點的權值*/
for(i=0;i<n;i++)
{
printf("Pleaseinputweightofleafnode%d: ",i);
scanf("%d",&HuffNode[i].weight);
}/*endfor*/

/*循環構造Huffman樹*/
for(i=0;i<n-1;i++)
{
m1=m2=MAXVALUE;/*m1、m2中存放兩個無父結點且結點權值最小的兩個結點*/
x1=x2=0;
/*找出所有結點中權值最小、無父結點的兩個結點,並合並之為一顆二叉樹*/
for(j=0;j<n+i;j++)
{
if(HuffNode[j].weight<m1&&HuffNode[j].parent==-1)
{
m2=m1;
x2=x1;
m1=HuffNode[j].weight;
x1=j;
}
elseif(HuffNode[j].weight<m2&&HuffNode[j].parent==-1)
{
m2=HuffNode[j].weight;
x2=j;
}
}/*endfor*/
/*設置找到的兩個子結點x1、x2的父結點信息*/
HuffNode[x1].parent=n+i;
HuffNode[x2].parent=n+i;
HuffNode[n+i].weight=HuffNode[x1].weight+HuffNode[x2].weight;
HuffNode[n+i].lchild=x1;
HuffNode[n+i].rchild=x2;

printf("x1.weightandx2.weightinround%d:%d,%d ",i+1,HuffNode[x1].weight,HuffNode[x2].weight);/*用於測試*/
printf(" ");
}/*endfor*/
/*for(i=0;i<n+2;i++)
{
printf("Parents:%d,lchild:%d,rchild:%d,value:%d,weight:%d ",HuffNode[i].parent,HuffNode[i].lchild,HuffNode[i].rchild,HuffNode[i].value,HuffNode[i].weight);
}*///測試
}/*endHuffmanTree*/

//解碼
voiddecodeing(charstring[],HNodeTypeBuf[],intNum)
{
inti,tmp=0,code[1024];
intm=2*Num-1;
char*nump;
charnum[1024];
for(i=0;i<strlen(string);i++)
{
if(string[i]=='0')
num[i]=0;
else
num[i]=1;
}
i=0;
nump=&num[0];

while(nump<(&num[strlen(string)]))
{tmp=m-1;
while((Buf[tmp].lchild!=-1)&&(Buf[tmp].rchild!=-1))
{

if(*nump==0)
{
tmp=Buf[tmp].lchild;
}
elsetmp=Buf[tmp].rchild;
nump++;

}

printf("%d",Buf[tmp].value);
}


}


intmain(void)
{

HNodeTypeHuffNode[MAXNODE];/*定義一個結點結構體數組*/
HCodeTypeHuffCode[MAXLEAF],cd;/*定義一個編碼結構體數組,同時定義一個臨時變數來存放求解編碼時的信息*/
inti,j,c,p,n;
charpp[100];
printf("Pleaseinputn: ");
scanf("%d",&n);
HuffmanTree(HuffNode,n);


for(i=0;i<n;i++)
{
cd.start=n-1;
c=i;
p=HuffNode[c].parent;
while(p!=-1)/*父結點存在*/
{
if(HuffNode[p].lchild==c)
cd.bit[cd.start]=0;
else
cd.bit[cd.start]=1;
cd.start--;/*求編碼的低一位*/
c=p;
p=HuffNode[c].parent;/*設置下一循環條件*/
}/*endwhile*/

/*保存求出的每個葉結點的哈夫曼編碼和編碼的起始位*/
for(j=cd.start+1;j<n;j++)
{HuffCode[i].bit[j]=cd.bit[j];}
HuffCode[i].start=cd.start;
}/*endfor*/

/*輸出已保存好的所有存在編碼的哈夫曼編碼*/
for(i=0;i<n;i++)
{
printf("%d'sHuffmancodeis:",i);
for(j=HuffCode[i].start+1;j<n;j++)
{
printf("%d",HuffCode[i].bit[j]);
}
printf("start:%d",HuffCode[i].start);

printf(" ");

}
/*for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
printf("%d",HuffCode[i].bit[j]);
}
printf(" ");
}*/
printf("Decoding?PleaseEntercode: ");
scanf("%s",&pp);
decodeing(pp,HuffNode,n);
getch();
return0;
}

http://www.cnblogs.com/Jezze/archive/2011/12/23/2299884.html

Ⅵ 哈夫曼樹問題

(2+3+4)*2+(1+3)*3=27,斜體部分為權值

我建的樹是這樣的

12

5 7

3 2 3 4

1 2

Ⅶ 哈夫曼編碼的演算法代碼

//本程序根據26個英文字母出現的頻度,得到了它們的一種哈夫曼編碼方案 //by jirgal 2005.4.18 #include<iostream.h> #include<iomanip.h> #define NUM 26 //字母數 #define TNUM 51 // #define LTH 15 //編碼最大長度 class Node { public: char data; int weight; int parent; int lchild; int rchild; }; void main() { char ch[NUM]={'a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};//26個字母 int weit[NUM]={856,139,279,378,1304,289,199,528,627,13,42, 339,249,707,797,199,12,677,607,1045,249,92,149,17,199,8};//出現頻率 Node nodes[TNUM]; //用對象數組存儲哈夫曼樹 int i,j,one,two,a,b; int hc[NUM][LTH]; //用於存儲編碼 int m,n; //初始化數組 for(i=0;i<NUM;i++) { nodes[i].data=ch[i]; nodes[i].weight=weit[i]; nodes[i].parent=-1; nodes[i].lchild=-1; nodes[i].rchild=-1; } for(i=NUM;i<TNUM;i++) { nodes[i].data='@'; nodes[i].weight=-1; nodes[i].parent=-1; nodes[i].lchild=-1; nodes[i].rchild=-1; } //建立哈夫曼樹 for(i=NUM;i<TNUM;i++) { a=b=-1; one=two=10000; //最大權數 for(j=0;j<i;j++) { if(nodes[j].parent==-1) if(nodes[j].weight<=two) one=two; two=nodes[j].weight; a=b; b=j; } else if(nodes[j].weight>two&&nodes[j].weight<=one) { one=nodes[j].weight; a=j; } } }//for語句得到 parent=-1(即尚沒有父結點)且weight最小的兩個結點 nodes[a].parent=i; nodes[b].parent=i; nodes[i].lchild=a; nodes[i].rchild=b; nodes[i].weight=nodes[a].weight+nodes[b].weight; } //初始化hc for(i=0;i<LTH;i++) { for(j=0;j<NUM;j++) { hc[j][i]=7; } } //編碼 for(i=0;i<NUM;i++) { j=LTH-1; for(m=i,n=nodes[i].parent;m!=-1;m=n,n=nodes[n].parent) { if(nodes[n].lchild==m) { hc[i][j]=0; } else { hc[i][j]=1; } j--; } } //輸出 nodes cout<<"HuffmanTree:"<<endl; cout<<setw(4)<<"NO."<<setw(6)<<"data"<<setw(8)<<"weight"<<setw(6) <<"parnt"<<setw(6)<<"lchd"<<setw(6)<<"rchd"<<endl; for(i=0;i<TNUM;i++) { cout<<setw(4)<<i<<setw(6)<<nodes[i].data<<setw(8)<<nodes[i].weight<<setw(6) <<nodes[i].parent<<setw(6)<<nodes[i].lchild<<setw(6)<<nodes[i].rchild<<endl; } //輸出編碼 cout<<endl<<"Result:"<<endl; cout<<setw(6)<<"char"<<setw(10)<<"frequency"<<setw(16)<<"huffmancode\n"; for(i=0;i<NUM;i++) { cout<<setw(6)<<ch[i]<<setw(8)<<weit[i]; cout<<" "; for(j=0;j<LTH;j++) { if(hc[i][j]!=7) { cout<<hc[i][j]; } } cout<<endl; } cout<<"\nDone.\n"<<endl; }

Ⅷ 哈夫曼樹解碼的演算法思路,語言描述即可

你講的是實現時的語言注釋,還是他的實現過程

Ⅸ C++哈夫曼樹

我沒有上到那兒去啊

Ⅹ 哈夫曼演算法的定義

定義:它是由n個帶權葉子結點構成的所有二叉樹中帶權路徑長度最短的二叉樹。
因為這種樹最早由哈夫曼(Huffman)研究,所以稱為哈夫曼樹,又叫最優二叉樹。

熱點內容
在哪裡找到sim卡設置密碼 發布:2024-11-15 06:51:47 瀏覽:392
細說phppdf 發布:2024-11-15 06:38:35 瀏覽:276
征途PK腳本 發布:2024-11-15 06:37:51 瀏覽:680
vbs打不開編譯器錯誤 發布:2024-11-15 06:35:12 瀏覽:344
深海迷航密碼在哪裡 發布:2024-11-15 06:30:23 瀏覽:303
伺服器日誌怎麼分析 發布:2024-11-15 06:22:04 瀏覽:525
字體目錄在哪個文件夾 發布:2024-11-15 06:20:28 瀏覽:181
php種子怎麼打開 發布:2024-11-15 06:07:01 瀏覽:346
密碼箱的密碼忘記了如何開鎖 發布:2024-11-15 06:04:41 瀏覽:956
安卓軟體和蘋果系統哪個好 發布:2024-11-15 05:48:32 瀏覽:284