排序:
取出根节点权值最小的两棵二叉树
组成一棵新的二叉树,前面取出来的两棵二叉树是新的两个子树
根节点的权值是前两取出来的两棵二叉树的根节点的权值之和
一、哈夫曼树
public class Node implements Comparable<Node>{
int value;
Node leftNode;
Node rightNode;
public Node(int value) {
this.value = value;
}
@Override
public int compareTo(Node o) {
return this.value-o.value;
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestHuffuManTree {
public static void main(String[] args) {
int[] arr = {3,7,8,29,5,11,23,14};
Node node = createHuffmanTree(arr);
System.out.println(node);
}
//创建哈夫曼树
public static Node createHuffmanTree(int[] arr) {
//先使用数组中所有的元素创建若干个二叉树(只有一个节点)
List<Node> nodes = new ArrayList<>();
for(int value:arr) {
nodes.add(new Node(value));
}
//循环处理
while(nodes.size() > 1) {
//排序
Collections.sort(nodes);
//取出来权值最小的两个二叉树
//取出权值最小的二叉树
Node left = nodes.get(nodes.size()-1);
//取出权值次小的二叉树
Node right = nodes.get(nodes.size()-2);
//创建一棵新的二叉树
Node parent = new Node(left.value+right.value);
//把取出来的两个二叉树移除
nodes.remove(left);
nodes.remove(right);
//放入原来的二叉树集合中
nodes.add(parent);
}
return nodes.get(0);
}
}
二、数据压缩
/**
* 哈夫曼树
*
* @author Mona
*
*/
public class Node implements Comparable<Node>{ //实现Comparable接口才可以进行排序
Byte data;
int weight;
Node left;
Node right;
public Node(Byte data,int weight) {
this.data = data;
this.weight = weight;
}
@Override
public int compareTo(Node o) {
return o.weight-this.weight; //排出的是倒序
}
@Override
public String toString() {
return "Node [data=" + data + ", weight=" + weight + "]";
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestHuffmanCode {
public static void main(String[] args) {
String msg = "can you can a can as a can canner can a can";
byte[] bytes = msg.getBytes();
//进行哈夫曼编码
byte[] b = huffmanZip(bytes);
}
/**
* 进行哈夫曼编码压缩的方法
*
* @param bytes
* @return
*/
private static byte[] huffmanZip(byte[] bytes) {
//先统计每一个byte出现的次数,并放入一个集合中
List<Node> nodes = getNodes(bytes);
//创建一棵哈夫曼树
Node tree = createHuffmanTree(nodes);
System.out.println(tree);
System.out.println(tree.left);
System.out.println(tree.right);
//创建一个哈夫曼编码表
//编码
return null;
}
//创建哈夫曼树
private static Node createHuffmanTree(List<Node> nodes) {
while(nodes.size() > 1) {
//排序
Collections.sort(nodes);
//取出两个权值最低的二叉树
Node left = nodes.get(nodes.size()-1);
Node right = nodes.get(nodes.size()-2);
//创建一棵新的二叉树
Node parent = new Node(null,left.weight+right.weight);
//把之前取出来的两棵二叉树设置为新创建的二叉树的子树
parent.left = left;
parent.right = right;
//把前面取出来的两棵二叉树删除
nodes.remove(left);
nodes.remove(right);
//把新创建的二叉树放入集合中
nodes.add(parent);
}
return nodes.get(0);
}
/**
* 把byte数组转为node集合
*
* @param bytes
* @return
*/
private static List<Node> getNodes(byte[] bytes) {
List<Node> nodes = new ArrayList<>();
//存储每一个byte出现了多少次
Map<Byte,Integer> counts = new HashMap<>();
//统计每一个byte出现的次数
for(byte b:bytes) {
Integer count = counts.get(b);
if(count == null) {
counts.put(b, 1);
}else {
counts.put(b, count+1);
}
}
//System.out.println(counts);
//把每一个键值对转为一个node对象
for(Map.Entry<Byte, Integer> entry:counts.entrySet()) {
nodes.add(new Node(entry.getKey(), entry.getValue()));
}
return nodes;
}
}