`

Huffman编码

阅读更多

数组里面的权值对应了空格以及abcd......xyz,然后进行Huffman编码

 

package tree;

import java.util.ArrayList;

public class testHuffmanTree {
	public static void main(String[] args) {
		//the weight of leafNode
		int weight[]={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};	
		huffmanTree tree =new huffmanTree(weight);
		tree.HuffmanCoding();
		tree.printHuffmanCoding();
		
	}
}
class huffmanTreeNode{
	int weight;
	int parent,lchild,rchild;
	huffmanTreeNode(){}
	huffmanTreeNode(int weight,int parent,int lchild,int rchild){
    	this.weight=weight;	
    	this.parent=parent;
    	this.lchild=lchild;
    	this.rchild=rchild;
	}
}
class huffmanTree{
	private int n;//the number of leafNodes
	private int m;
	private int[] weight;
	/*the number of all Nodes
	 *不能在这里写m=2*n-1;否则m只是会等于1.
	*/
	//ArrayList  huffmanTree store the nodes of the tree
	private ArrayList<huffmanTreeNode>  huffmanTree=new ArrayList<huffmanTreeNode>();
	//Coding stores the coding of the huffmanTree.
	private char[][] Coding;
 	huffmanTree(){}
	huffmanTree(int[] weight){
		this.weight=weight;	
		//如果m,n 不在这里初始化,在其他函数中初始化是会随即销毁的
		n=weight.length;
		m=2*n-1;
		creatHuffman();
	} 
	public ArrayList<huffmanTreeNode> getHuffmanTree() {
		return huffmanTree;
	}
    /*to select  two nodes with the minimun weight 
     * (index ranges from 0 to endOfArrayList),
     * and these two nodes' parent  equals 0
	*/
    int Select(int endOfArrayList){
    	int minimun=1000000000;
    	int current=-1;
    	for(int i=0;i<=endOfArrayList;i++){
    		if(0!=huffmanTree.get(i).parent)
    			continue;
    		if(huffmanTree.get(i).weight<minimun){
    			minimun=huffmanTree.get(i).weight;	
    			current=i;  			
    		}  			
    	}
    	return current;	
    }
    //creatHuffman
	void creatHuffman(){		
		for(int i=0;i<n;i++){
			huffmanTree.add(new huffmanTreeNode(weight[i],0,0,0));			
		}	
		for(int i=n;i<m;i++){
			huffmanTree.add(new huffmanTreeNode(0,0,0,0));
		}
		for(int i=n;i<m;i++ ){
			int s1,s2;
			s1=Select(i-1);			
			huffmanTree.get(s1).parent=i;
			s2=Select(i-1);			
			huffmanTree.get(s2).parent=i;
			// s1 and s2 are the smallest 
			huffmanTree.get(i).lchild=s1;
			huffmanTree.get(i).rchild=s2;// in this case  lchild  is  not bigger than rchild
			huffmanTree.get(i).weight=
					huffmanTree.get(s1).weight+
					huffmanTree.get(s2).weight;	
		}    	
	}
	//getHuffmanCoding
    void  HuffmanCoding(){
    	Coding=new char[n][n]; 	
    	for(int i=0;i<n;i++){
    		int start=n-1;
    		for(int c=i, f=huffmanTree.get(i).parent;  f!=0;  c=f,f=huffmanTree.get(f).parent){
    			if(huffmanTree.get(f).lchild==c)
    				Coding[i][start--]='0';//左分支为‘0’
    			else 
    				Coding[i][start--]='1';	
    		}   		 		
    	}	
    }
    //printHuffmanCoding
    void printHuffmanCoding(){
    	char letter=97;//对应的字母
    	System.out.println("the codes  are as following ******" );
    	for(int i=0;i<n;i++){//第一个是空格,额外考虑
    		if(i!=0){
    			System.out.print(letter+"  : "  );
        		letter++; 			
    		}
    		else 
    			System.out.print("空格"+"  : "  );   		
    		for(int j=0;j<n;j++){
    			if(Coding[i][j]!='#')
    	    		System.out.print(Coding[i][j]+" ");
    		}
    		System.out.println();
    	}	
    }
}
 

 

2
0
分享到:
评论
1 楼 come_for_dream 2015-06-18  
     

相关推荐

    Huffman编码的java实现

    自己实现的Huffman编码,压缩率接近50%,使用字节流写入文件。解码时读取字节流,将字节流转化为二进制串,匹配字符解压。使用I have a dream作为测试文件。

    huffman编码和解码的简单实现

    使用文件保存初始的文本数据及最终的结果。  文件名为inputfile1.txt的文件保存的...统计inputfile1.txt中各字符的出现频率,并据此构造Huffman树,编制Huffman编码;根据已经得到的编码,对01形式的编码段进行译码。

    数据结构上机实验 Huffman编码(二叉树) C语言

    实验三、Huffman编码(二叉树)  实验目的:熟练掌握二叉树应用(Huffman编码)的基本算法实现。  实现功能:对输入的一串电文字符实现Huffman编码,再对Huffman编码生成的代码串进行译码,输出电文字符串。实现...

    huffman编码

    Huffman编码与解码 (选做)(Huffman编码、二叉树) [问题描述]  对一篇英文文章,统计各字符出现的次数,实现Huffman编码,以及对编码结果的解码。 [基本要求] (1) 输出每个字符出现的次数和编码,其中求最小权值...

    Huffman 编码图像无损压缩和解压缩 Python示例代码 哈夫曼编码

    本程序实现了利用 Huffman 编码对图像进行无损压缩和解压缩。Huffman 编码是一种基于字符出现频率构建相应前缀码的无损数据压缩算法。 使用方法: 1. 需要安装 OpenCV 和 Numpy 库: pip install opencv-python ...

    Huffman编码C++源代码

    Huffman编码C++源代码 Huffman编码C++源代码

    Quake3 自适应huffman编码分析

    收集来的Quake3 自适应huffman编码分析,备份一份

    图像的Huffman编码

    图像的Huffman编码 有注释 希望对大家有用!!!!

    Huffman编码以及其编码效率的计算

    c语言的huffman编码及编码效率计算,采用两种编码方式,可选择

    Huffman编码测试文件

    Huffman编码的测试文件 包括图像 文本 音频和压缩文件

    基于Matlab的图像Huffman编码的实现

    基于Matlab的图像huffman编码的实现,将图像转换为灰度图,并压缩,求其压缩比和时间

    Huffman编码C语言程序

    Huffman编码的程序代码, #include #include #include #include //极大值用于生成Huffman树 #define MAXSIZE 100000000 //用于生成相应叶子节点Huffman编码的二维字符数组 typedef char* HCode; //Huffman树节点 ...

    Huffman编码构成过程.files.rar

    简单图解释Huffman编码构成过程,简单易懂

    Huffman树 及 Huffman编码 演示程序

    Huffman树 及 Huffman编码 演示程序 以画图的方法形象的表示了树的构成,解决了普通控制台应用程序对树的结构表达不清的问题 编译器 VS2008

    Huffman编码(MFC版本)

    1.要求对文件进行Huffman编码的算法,以及对一编码文件进行解码的算法 2.熟练掌握二叉树的应用;具体要求如下: 最小冗余码/哈夫曼码

    Huffman编码C++实现

    代码中用C++实现了Huffman编码,经过测试通过

    huffman编码java实现

    包含huffman编码java实现源程序、该java程序的javadoc文档、huffman编码简单原理ppt

    数据结构Huffman编码

    韩英杰老师的数据结构中关于Huffman编码算法演示课件

Global site tag (gtag.js) - Google Analytics