huffman樹和huffman編碼

2021-08-21 23:16:02 字數 1846 閱讀 7579

//huffman樹和huffman編碼 #include

#include

#include

#include

#define overflow -1

typedef struct

htnode,*huffmantree;

typedef char * *huffmancode;

void select(huffmantree &ht,int i,int &s1,int &s2)

for(j = 1; j < i; j++)

for(k = 1; k <= i; k++)

for(j = 1; j < i; j++) }

void huffmancoding(huffmantree &ht,huffmancode &hc,char *zi,int *w,int n)

for(;i<=m;++i,++p)

for(i=n+1;i<=m;++i)

hc=(huffmancode)malloc((n+1)*sizeof(char *));

cd=(char*)malloc(n*sizeof(char));/*臨時的code儲存*/

cd[n-1]='\0';

for(i=1;i<=n;++i)

free(cd);

}void main()

;int w[100];

char z;

char c[100];

z='a';

cout<>w[i];

z++;

}huffmancoding(ht,hc,zi,w,8);

cout<>c;

cout<<"the code is:";

for(i=0; i < strlen(c); i++)

/*根據字元的哈夫曼編碼,將輸入的文字(變數c表示的)翻譯成電碼。

*/cout<

cout<>c;

j=strlen(c);

yu=15;

i=1;

cout<<"the text is:";

while(i <= j)

if(c[i-1]== '1')

}/*顯示由部分電碼解碼得到的字元,並準備對後面的電碼進行解碼*/

cout<

//沒有出錯處理

輸入示例:

please input the weight for a:12

please input the weight for b:234

please input the weight for c:6

please input the weight for d:23

please input the weight for e:26

please input the weight for f:267

please input the weight for g:23

please input the weight for h:67

char weight huffmancode

a 12 0001

b 234 01

c 6 0000

d 23 1000

e 26 001

f 267 11

g 23 1001

h 67 101

please input the text:abcdefg

the code is:00010100001000001111001

enter the code:00010100001000001111001

the text is:abcdefg

press any key to continue

huffman樹(最優二叉樹 和huffman編碼

在許多實際應用中,數中結點常常被賦予乙個表示某種意義的數值,稱為該結點的權。從樹根結點到任意結點的路徑長度 經過的邊數 與該結點上權值的乘積稱為該結點的帶權路徑長度。數中所有葉結點的帶權路徑長度之和稱為該樹的帶權路徑長度 特點 1.每個初始結點最終都成為葉結點,且權值越小的結點到根節點的路徑長度越大...

Huffman樹和編碼

include include include define maxbit 100 define maxvalue 10000 define maxleaf 30 define maxnode maxleaf 2 1 typedef struct hcodetype 編碼結構體 typedef st...

Huffman樹與Huffman編碼

一.哈夫曼樹概念 路徑 祖先到節點之間的分支構成一條通往節點的路徑 路徑長度 路徑上分支的數目稱為路徑長度 節點的權 給樹中節點所設的物理意義的值 節點帶權路徑長度 從根到該點路徑長度與該點權值的乘機 huffman樹就是所有樹中每點帶權路徑加和最短的樹。二.huffman樹構造步驟 1.根據每個點...