java实现文本的哈夫曼编码HuffmanTree(用数组的方式)

java数据结构实现文本的哈夫曼编码HuffmanTree(用数组的方式)

 •实验内容

1、读取文本Demo.txt。统计'a'-'z',空格,'.'的出现次数,并计算出权重。

2、根据字符及其权重构造Huffman树的节点  (Huffman节点需要的各个属性及相关方法)。

3、构造一个链表保存这些节点。

4、分两次从链表中找出权值最小的节点,生成一颗子树,并计算该子树的高度 (引入节点的Height属性)。

5、当链表中最后只剩一个节点时,就获得了Huffman树的根。

6、前序遍历Huffman树,遍历时构造Huffman编码(引入节点的编码属性)。

7、将特定文本翻译成Huffman编码对应的01串。

8、将上一步的01串通过Huffman树翻译成文本。

实现代码

1.设计Chars类

package 实验三;

public class Chars 
{
	char thechar;//文本中的字符
	float weight;//字符的权重
	int[] huffmancode;//哈弗曼树的编码
	int height;
	//构造方法,初始化,产生一个huffman编码
	Chars()
	{
		this.thechar='\0';
		this.weight=0.0f;
		this.huffmancode=null;
	}
	//获取字符
	public char getThechar() {
		return thechar;
	}
	//设置字符
	public void setThechar(char thechar) {
		this.thechar = thechar;
	}
	//获取权重
	public float getWeight() {
		return weight;
	}
	//设置权重
	public void setWeight(float weight) {
		this.weight = weight;
	}
	//获取huffmancode
	public int[] getHuffmancode() {
		return huffmancode;
	}
	//设置huffmancode
	public void setHuffmancode(int[] huffmancode) {
		this.huffmancode =huffmancode;
	}
	//toString()方法
	public String toString()
	{
		return "thechar:"+thechar+"	weight:"+weight+"	height:"+height+"	huffmancode:";
	}	
}

2.设计HuffmanList类 

package 实验三;

public class HuffmanList 
{
	HuffmanNode huffmanNode;
	float weight;
	int totalnodes;//连表头结点总数
	//链表中增加结点
	void addNode(HuffmanNode huffmanNode,float weight) 
	{
		this.huffmanNode=huffmanNode;
		this.weight=weight;
	}
}

3.设计HuffmanNode类 

package 实验三;

public class HuffmanNode implements Comparable//Huffman树的结点类描述
{
	public float weight;// 结点的数据域
	public int height;//哈夫曼树的高度
	public int flag;//结点是否加入哈夫曼树的标志 
	public HuffmanNode parent,lchild,rchild;//父结点及左右孩子结点域
	char letter;//读入的字符
	int[] huffmancode;//读入字符编码 
	public HuffmanNode()//构造一个空结点
	{
		this.weight=0.0f;
		this.lchild=null;
		this.rchild=null;
		this.height=0;
		this.letter='\0';
		this.huffmancode=null;
	}
	
	public float getWeight() 
	{
		return weight;
	}

	public void setWeight(float weight)
	{
		this.weight = weight;
	}

	public int getHeight() 
	{
		return height;
	}

	public void setHeight(int height)
	{
		this.height = height;
	}

	public HuffmanNode getParent() 
	{
		return parent;
	}

	public void setParent(HuffmanNode parent) 
	{
		this.parent = parent;
	}

	public HuffmanNode getLchild()
	{
		return lchild;
	}

	public void setLchild(HuffmanNode lchild)
	{
		this.lchild = lchild;
	}

	public HuffmanNode getRchild() 
	{
		return rchild;
	}

	public void setRchild(HuffmanNode rchild) 
	{
		this.rchild = rchild;
	}

	public char getLetter() 
	
	{
		return letter;
	}

	public void setLetter(char letter) 
	{
		this.letter = letter;
	}

	public int[] getHuffmancode()
	{
		return huffmancode;
	}

	public void setHuffmancode(int[] huffmancode)
	{
		this.huffmancode = huffmancode;
	}
	//构造一个左、右孩子域为空,具有权值的结点
	public HuffmanNode(int weight)
	{
		this.weight=weight;
		flag=0;
		parent=lchild=rchild=null;
	}
	//构造一个带有字符和权重的结点
	public HuffmanNode(char letter,float weight)
	{
		this.letter=letter;
		this.weight=weight;
	}

	@Override
	//两结点权重比较,返回值为1或0或-1
	public int compareTo(HuffmanNode node) //比较权重是否相同
	{
		int result = 1;//初始result设置为1
		if(this.getWeight()>node.getWeight())//若当前权重大于比较的那个的权重,输出1
		{
			result=1;
		}
		else if(this.getWeight()node.getHeight())//若当前深度大于比较的那个的深度,输出1
				result=1;
			else if(this.getHeight()

4.设计HuffmanTree 

package 实验三;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class HuffmanTree 
{
	char Chars[];//存放字符权重的数组
	HuffmanList Hlist;//链表
	
	//计算每个字符的最终权重(该字母出现次数/所有字符出现次数的总和),并存入HuffmanList中
    static void computeWeight(Chars[] chars)
    {
    	HuffmanList Hlink=new HuffmanList();//创建列表
		float totalWeight=0.0f;//总的权重
		for(int i=0;i='a')&&(inputChar<='z'))
		    	{
		    		chars[inputChar-'a'].setThechar((char) inputChar);
		    		chars[inputChar-'a'].setWeight(chars[inputChar-'a'].getWeight()+1);//获取权重
		    	}
		    	//当字符为空格
		    	else if(inputChar==' ')
		    	{
		    		chars[26].setThechar(' ');
		    		chars[26].setWeight(chars[26].getWeight()+1);
		    	}
		    	//当字符为'.'
		    	else if(inputChar=='.')
		    	{
		    		chars[27].setThechar('.');
		    		chars[27].setWeight(chars[27].getWeight()+1);
		    	}
		    	//当字符为','
		    	else if(inputChar==',')
		    	{
		    		chars[28].setThechar(',');
		    		chars[28].setWeight(chars[28].getWeight()+1);
		    	}	
	        }
			computeWeight(chars);//算出权重
	    	//循环输出
			HuffmanTree T=new HuffmanTree();//构造哈夫曼树
	    	T.conHuffmanTree(chars);//求哈夫曼编码
	    	System.out.println("哈夫曼编码为:");
	    	for(int i=0;i

5.测试类 

package 实验三;

import java.io.File;
import java.io.FileReader;


public class Test 
{
	 public static void main(String[] args)
	    {
	    	HuffmanTree Huff=new HuffmanTree();
			Chars[] chars=new Chars[29];
			for(int i=0;i='a')&&(inputChar<='z'))
			    	{
			    		chars[inputChar-'a'].setThechar((char) inputChar);
			    		chars[inputChar-'a'].setWeight(chars[inputChar-'a'].getWeight()+1);//获取权重
			    	}
			    	//当字符为空格
			    	else if(inputChar==' ')
			    	{
			    		chars[26].setThechar(' ');
			    		chars[26].setWeight(chars[26].getWeight()+1);
			    	}
			    	//当字符为'.'
			    	else if(inputChar=='.')
			    	{
			    		chars[27].setThechar('.');
			    		chars[27].setWeight(chars[27].getWeight()+1);
			    	}
			    	//当字符为','
			    	else if(inputChar==',')
			    	{
			    		chars[28].setThechar(',');
			    		chars[28].setWeight(chars[28].getWeight()+1);
			    	}	
		        }
				Huff.computeWeight(chars);//算出权重
		    	//循环输出
				HuffmanTree T=new HuffmanTree();//构造哈夫曼树
		    	T.conHuffmanTree(chars);//求哈夫曼编码
		    	System.out.println("哈夫曼编码为:");
		    	for(int i=0;i

 运行结果:

java实现文本的哈夫曼编码HuffmanTree(用数组的方式)_第1张图片

java实现文本的哈夫曼编码HuffmanTree(用数组的方式)_第2张图片

java实现文本的哈夫曼编码HuffmanTree(用数组的方式)_第3张图片

  一枚平平无奇的小白!~

  欢迎相互学习!~ 

你可能感兴趣的:(课程实验呀,java,数据结构,程序人生,其他,经验分享)