哈夫曼树以及哈夫曼编码

 

哈夫曼树

给定n个权值作为n个叶子节点,构造一棵二叉树,若该树的带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree)。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。

哈夫曼树代码实现

示例图:以字符串AAABBCDDDDD为例构建哈夫曼树

 哈夫曼节点类

public class HaffmanNode {
	int val; //值
	int weight;//权值
	char ch;//所放字符串
	HaffmanNode left;//左孩子
	HaffmanNode right;//右孩子

	public HaffmanNode(int val, char ch) {
		this.val = val;
		this.ch = ch;
	}
	@Override
	public String toString() {
		return "HaffmanNode [val=" + val + ", weight=" + weight + ", ch=" + ch + "]";
	}
}

哈夫曼树实现类


import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;

public class Haffman {
	HaffmanNode root;
	int count[] = new int[35];//统计字符出现的次数,这里只收集 A~Z的
	public void build(char[] arr) {
		//先统计字符数组中的字符出现的次数
		for (int i = 0; i < arr.length; i++) {
			count[arr[i] - 'A']++;
		}
		//创建优先队列,默认是优先级大的,先出队列,数值越小,优先级别越大
		//按照哈夫曼编码最小的两个节点先出,所以修改比较设置
		PriorityQueue<HaffmanNode> priorityQueue = new PriorityQueue<>(new Comparator<HaffmanNode>() {
			public int compare(HaffmanNode o1, HaffmanNode o2) {
				return o1.val - o2.val;
			}
		});
		//新建字符节点,并且将节点按照出现次数作为优先级放入优先队列中
		for (int i = 0; i < count.length; i++) {
			if (count[i] != 0) {
				priorityQueue.offer(new HaffmanNode(count[i], (char) (65 + i)));
			}
		}
		//当优先队列中的节点数大于1时,
		while (priorityQueue.size() != 1) {
			//取出两个最小的以及第二小的节点
			HaffmanNode left = priorityQueue.poll();
			HaffmanNode right = priorityQueue.poll();
			//创建一个新的节点,放入优先队列中,值为左孩子加右孩子的值
			HaffmanNode newHaffmanNode = new HaffmanNode(left.val + right.val, ' ');
			newHaffmanNode.left = left;
			newHaffmanNode.right = right;
			priorityQueue.add(newHaffmanNode);
		}
		//当优先队列中只剩一个时,此时的节点就是已经构建好的哈夫曼树
		root = priorityQueue.poll();
	}

}

更新权值

//map存储字符出现的权值
	private Map<Character, Integer> map1 = null;
	
	private void getWeight() {
		//更新权值
		map1 = new HashMap<>();
		count(root, 0);
	}
	//获取权值
	public Integer getWeight(char ch) {
		if (map1 == null) {
			getWeight();
		}
		return map1.get(ch);
	}
	
	public void count(HaffmanNode haffmanNode, int weight) {
		//递归更新权值
		if (haffmanNode != null && haffmanNode.ch != ' ') {
			map1.put(haffmanNode.ch, weight);
		}
		if (haffmanNode.left != null) {
			count(haffmanNode.left, weight + 1);
		}
		if (haffmanNode.right != null) {
			count(haffmanNode.right, weight + 1);
		}
	}

获取哈夫曼编码

//存储一个字符的哈夫曼编码后的编码集
	private Map<Character, String> map = new HashMap<>();

	public void bianma() {
		bianma(root, "");
	}

	private void bianma(HaffmanNode node, String str) {
		if (node.ch >= 'A' && node.ch <= 'Z') {
			//记录字符的哈夫曼编码
			map.put(node.ch, str);
		}
		if (node.left != null) {
			//往左侧走,编码为0
			bianma(node.left, str + "0");
		}
		if (node.right != null) {
			//往右侧走,编码为1
			bianma(node.right, str + "1");
		}
	}

应用

1.哈夫曼树又称为最优树,树的带权路径长度规定为所有叶子结点的带权路径长度之和,记为WPL。

2.数据压缩

猜你喜欢

转载自blog.csdn.net/qq_39290830/article/details/85076207