哈夫曼编码的java代码 哈夫曼编码算法实现

哈夫曼编码译码器 java建议:用类似的方法,可以将某一学科的总分统计出来 , 并填入第48行相应的单元格中 。
哈夫曼编码与译码 javaclass HaffmanNode//哈夫曼树的结点类
{
int weight;//权值
int parent,left,right;//父母结点和左右孩子下标
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};//指定权值集合
HaffmanTree htree = new HaffmanTree(weight);
System.out.println("哈夫曼树的结点数组:\n" htree.toString());
String[] code = htree.haffmanCode();
System.out.println("哈夫曼编码:");
for (int i=0; icode.length; i)
System.out.println(code[i]);
}
}
哈夫曼编码的算法代码是什么?// 哈夫曼编码(算法)#include stdio.h
#include stdlib.h
#include string.htypedef char *HuffmanCode;//动态分配数组哈夫曼编码的java代码,存储哈夫曼编码typedef struct
{
unsigned int weight;//用来存放各个结点哈夫曼编码的java代码的权值
unsigned int parent,LChild,RChild;//指向双亲、孩子结点的指针
} HTNode, *HuffmanTree;//动态分配数组哈夫曼编码的java代码 , 存储哈夫曼树//选择两个parent为0,且weight最小的结点s1和s2
void Select(HuffmanTree *ht,int n,int *s1,int *s2)
【哈夫曼编码的java代码 哈夫曼编码算法实现】{
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==0i!=(*s1))
{
min=i;
break;
}
}
for(i=1; i=n; i)
{
if((*ht)[i].parent==0i!=(*s1))
{
if((*ht)[i].weight(*ht)[min].weight)
min=i;
}
}
*s2=min;
}//构造哈夫曼树ht 。w存放已知的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号存放叶子结点 , 初始化
{
(*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;
} //非叶子结点初始化
printf("\nHuffmanTree: \n");
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;
printf("%d (%d, %d)\n",(*ht)[i].weight,(*ht)[s1].weight,(*ht)[s2].weight);
}
printf("\n");
} //哈夫曼树建立完毕//从叶子结点到根 , 逆向求每个叶子结点对应的哈夫曼编码
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个编码的头指针
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);
}
free(cd);
for(i=1; i=n; i)
printf("HuffmanCode of = 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);
}
求助:用java实现哈夫曼编码压缩与解压缩算法 。你好,由于内容比较多,先概述一下先 。如图所示,为我写的一个压缩软件,原理是利用哈弗曼算法实现的 。我将资料整理好稍后就发到你邮箱,但在这里简要说明一下代码 。
请看我的空间
中的文章共5篇(太长了)
1.HuffmanTextEncoder类完成压缩功能,可直接运行,压缩测试用文本文件 。
2.HuffmanTextDecoder类完成解压缩功能 , 可直接运行,解压缩 压缩后的文本文件 。
3.BitReader,工具类,实现对BufferedInputStream的按位读取 。
4.BitWriter,工具类,实现按位写入的功能 。该类来自网络 。
5.MinHeapT ,模板工具类,实现了一个最小堆 。生成Huffman树时使用 。
用java实现哈夫曼编码只要自己再加个类Tree就可以哈夫曼编码的java代码了 。
代码如下哈夫曼编码的java代码:
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;
}
}
哈夫曼编码的java代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于哈夫曼编码算法实现、哈夫曼编码的java代码的信息别忘了在本站进行查找喔 。

    推荐阅读