// 哈夫曼編碼(算法)#include stdio.h
10年積累的網(wǎng)站建設(shè)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有涼城免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
#include stdlib.h
#include string.htypedef char *HuffmanCode; //動(dòng)態(tài)分配數(shù)組,存儲(chǔ)哈夫曼編碼typedef struct
{
unsigned int weight; //用來(lái)存放各個(gè)結(jié)點(diǎn)的權(quán)值
unsigned int parent,LChild,RChild; //指向雙親、孩子結(jié)點(diǎn)的指針
} HTNode, *HuffmanTree; //動(dòng)態(tài)分配數(shù)組,存儲(chǔ)哈夫曼樹(shù)//選擇兩個(gè)parent為0,且weight最小的結(jié)點(diǎn)s1和s2
void Select(HuffmanTree *ht,int n,int *s1,int *s2)
{
int i,min;
for(i=1; i=n; i++)
{
if((*ht)[i].parent==0)
{
min=i;
break;
}
}
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;
break;
}
}
for(i=1; i=n; i++)
{
if((*ht)[i].parent==0 i!=(*s1))
{
if((*ht)[i].weight(*ht)[min].weight)
min=i;
}
}
*s2=min;
}//構(gòu)造哈夫曼樹(shù)ht。w存放已知的n個(gè)權(quán)值
void CrtHuffmanTree(HuffmanTree *ht,int *w,int n)
{
int m,i,s1,s2;
m=2*n-1;
*ht=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(i=1; i=n; i++) //1--n號(hào)存放葉子結(jié)點(diǎ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;
} //非葉子結(jié)點(diǎn)初始化
printf("\nHuffmanTree: \n");
for(i=n+1; i=m; i++) //創(chuàng)建非葉子結(jié)點(diǎn),建哈夫曼樹(shù)
{
//在(*ht)[1]~(*ht)[i-1]的范圍內(nèi)選擇兩個(gè)parent為0
//且weight最小的結(jié)點(diǎn),其序號(hào)分別賦值給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;
printf("%d (%d, %d)\n",(*ht)[i].weight,(*ht)[s1].weight,(*ht)[s2].weight);
}
printf("\n");
} //哈夫曼樹(shù)建立完畢//從葉子結(jié)點(diǎn)到根,逆向求每個(gè)葉子結(jié)點(diǎn)對(duì)應(yīng)的哈夫曼編碼
void CrtHuffmanCode(HuffmanTree *ht, HuffmanCode *hc, int n)
{
char *cd;
int i,start,p;
unsigned int c;
hc=(HuffmanCode *)malloc((n+1)*sizeof(char *)); //分配n個(gè)編碼的頭指針
cd=(char *)malloc(n*sizeof(char)); //分配求當(dāng)前編碼的工作空間
cd[n-1]='\0'; //從右向左逐位存放編碼,首先存放編碼結(jié)束符
for(i=1; i=n; i++) //求n個(gè)葉子結(jié)點(diǎn)對(duì)應(yīng)的哈夫曼編碼
{
start=n-1; //初始化編碼起始指針
for(c=i,p=(*ht)[i].parent; p!=0; c=p,p=(*ht)[p].parent) //從葉子到根結(jié)點(diǎn)求編碼
if( (*ht)[p].LChild==c)
cd[--start]='0'; //左分支標(biāo)0
else
cd[--start]='1'; //右分支標(biāo)1
hc[i]=(char *)malloc((n-start)*sizeof(char)); //為第i個(gè)編碼分配空間
strcpy(hc[i],cd);
}
free(cd);
for(i=1; i=n; i++)
printf("HuffmanCode of %3d is %s\n",(*ht)[i].weight,hc[i]);
printf("\n");
}void main()
{
HuffmanTree HT;
HuffmanCode HC;
int *w,i,n,wei,m;
printf("\nn = " );
scanf("%d",n);
w=(int *)malloc((n+1)*sizeof(int));
printf("\ninput the %d element's weight:\n",n);
for(i=1; i=n; i++)
{
printf("%d: ",i);
fflush(stdin);
scanf("%d",wei);
w[i]=wei;
}
CrtHuffmanTree(HT,w,n);
CrtHuffmanCode(HT,HC,n);
}
class HaffmanNode //哈夫曼樹(shù)的結(jié)點(diǎn)類
{
int weight; //權(quán)值
int parent,left,right; //父母結(jié)點(diǎn)和左右孩子下標(biāo)
public HaffmanNode(int weight)
{
this.weight = weight;
this.parent=-1;
this.left=-1;
this.right=-1;
}
public HaffmanNode()
{
this(0);
}
public String toString()
{
return this.weight+", "+this.parent+", "+this.left+", "+this.right;
}
return code;
}
public static void main(String[] args)
{
int[] weight={5,29,7,8,14,23,3,11}; //指定權(quán)值集合
HaffmanTree htree = new HaffmanTree(weight);
System.out.println("哈夫曼樹(shù)的結(jié)點(diǎn)數(shù)組:\n"+htree.toString());
String[] code = htree.haffmanCode();
System.out.println("哈夫曼編碼:");
for (int i=0; icode.length; i++)
System.out.println(code[i]);
}
}
只要自己再加個(gè)類Tree就可以了。
代碼如下:
public class Tree {
double lChild, rChild, parent;
public Tree (double lChild, double rChild, double parent) {
this.lChild = lChild;
this.rChild = rChild;
this.parent = parent;
}
public double getLchild() {
return lChild;
}
public void setLchild(double lChild) {
this.lChild = lChild;
}
public double getRchild() {
return rChild;
}
public void setRchild(double rChild) {
this.rChild = rChild;
}
public double getParents() {
return parent;
}
public void setParents(double root) {
this.parent = root;
}
}