赫夫曼树
- 路径
- 路径长度
- 节点的权
- 节点带权路径长度
- 树的带权路径长度
public class Demo{
public static void preOrder(Node root){
if(root!=null){
root.preOrder();
}else{
System.out.println("空树");
}
}
public static Node createHuffmanTree(int[] arr){
List<Node> nods = new ArrayList<Node>();
for(int value : arr){
node.add(new Node(value))
}
while(nodes.size()>1){
Collections.sort(nodes);
Node leftNode = nodes.get(0);
Node rightNode = nodes.get(1);
Node parent = new Node(leftNode.value+rightNode.value);
parent.left = leftNode;
praent.right = rightNode;
nodes.remove(leftNode);
nodes.remove(rightNode);
nodes.add(parent);
}
return nodes.get(0);
}
}
class Node implements Compareable<Node>{
int value;
Node left;
Node right;
public Node(int value){
this.value = value;
}
public int compareTo(Node o){
return this.value - o.value;
}
public void preOrder(){
System.out.println(this);
if(this.left!=null){
this.left.preOrder();
}
if(this.right!=null){
this.reight.preOrder();
}
}
}
数据压缩
public class HuffmanCode{
static Map<Byte,String> huffmanBCodes = new HashMap<Byte,String>();
static StringBuilder stringBuilder = new StringBuilder();
public static void main(String[] args){
String content = "xxxxxxxxxxxxxxxxxxxxx";
byte[] contentBytes = content.getBytes();
List<Node> nodes = getNodes(contentBytes);
Node huffmanTreeRoot = createHuffman(nodes);
preOrder(huffmanTreeRoot);
Map<Byte,String> huffmanCodes = getCodes(huffmanTreeRoot);
byte[] huffmanCodeBytes = zip(contentBytes,huffmanCodes)
}
private static byte[] huffmanXip(byte[] bytes){
List<Node> nodes = getNodes(bytes);
Node huffmanTreeRoot = createHuffman(nodes);
Map<Byte,String> huffmanCodes = getCodes(huffmanTreeRoot);
byte[] huffmanCodeBytes = zip(contentBytes,huffmanCodes)
return huffmanCodeBytes;
}
private static byte[] zip(byte[] bytes,Map<byte,String> huffmanCodes){
StringBuilder stringBuilder = new StringBuilder();
for(byte b : bytes){
stringBuilder.append(hyffmanCodes.get(b));
}
int len;
if(stringBuilder.length%8==0){
len = stringBuilder.length()/8;
}else{
len = stringBuilder.length()/8+1;
}
byte[] huffmanCodeBytes = new byte[len];
int index = 0;
for(int i=0;i<stringBuilder.length();i+=8){
String strByte;
if(i+8>stringBuilder.length()){
strByte = stringBuilder.subString(i)
}else{
strByte = stringBuilder.substring(i,i+8);
}
huffmanCodeBytes[index] = (byte)Integer.parseInt(strByte,2);
index++;
}
return huffmanCodeBytes;
}
private static Map<Byte,String> getCodes(Node root){
if(root==null){
return null;
}
getCodes(root.left,"0",stringBuilder);
getCOdes(root.right,"1",stringBuilder);
}
private static void getCodes(Node node,String code,StringBuilder stringBuilder){
StringBuilder stringBulder2 = new StringBuilder(stringBuilder);
stringBulder2.append(code);
if(node!=null){
if(node.data==null){
getCodes(node.left,"0",stringBuilder2);
getCodes(node.right,"1",stringBuilder2);
}else{
huffmanCodes.put(node.data,stringBuilder2.toString());
}
}
}
public static void preOrder(Node root){
if(root!=null){
root.preOrder();
}else{
System.out.println("空");
}
}
private static List<Node> getNodes(byte[] bytes){
ArrayList<Node> nodes = new ArrayList<Node>();
Map<Byte,Integer> counts = new HashMap();
for(byte b:bytes){
Integer count = counts.get(b);
if(count==null){
counts.put(b,1);
}else{
counts.put(b,count+1);
}
}
for(Map.Entry<Byte,Integer> entry:counts.entrySet()){
nodes.add(new Node(entry.getLKey(),entry.getValue()));
}
return nodes;
}
private static Node createHuffman(List<Node> nodes){
while(nodes.size()>1){
Collections.sort(nodes);
Node leftNode = nodes.get(0);
Node rightNode = nodes.get(1);
Node parent = new Node(null,leftNode.weight+rightNode.weight);
parent.left = leftNode;
parent.right = rightNode;
nodes.remove(leftNode);
nodes.remvoe(rightNode);
nodes.add(parent);
}
return nodes.get(0);
}
}
class Node{
Byte data;
int weight;
Node left;
Node right;
public Node(Byte data,int weight){
this.data = data;
this.weight = weight;
}
public int compareTo(Node o){
return this.weight - o.weight;
}
public String toString(){
return "Node[data="+data+"weight="+weight+"]";
}
public void preOrder(){
System.out.println(this);
if(this.left!=null){
this.left.preOrder();
}
if(this.right != null ){
this.right.preOrder();
}
}
}
解压
public static byte[] decode(Map<Byte,String> huffmanCodes,byte[] huffmanBytes){
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<huffmanBytes.length;i++){
boolean flag = (i==huffmanBytes.length-1);
stringBuilder.append(byteToBitString(!flag,huffmanBytes[i]));
}
Map<String,Byte> map = new HashMap<String,Byte>();
for(Map.Entry<Byte,String> entry:huffmanCodes.entrySet()){
map.put(entry.getValue(),entry.getKey());
}
List<Byte> list = new ArrayList<>();
for(int i=0;i<stringBuilder.length();i++){
int count = 1;
boolean flag = true;
Byte b = null;
while(flag){
String key = stringBuilder.substring(i,i+count);
b = map.get(key);
if(b==null){
count++;
}else{
flag = false;
}
}
list.add(b);
i+=count;
}
byte b = new byte[list.size];
for(int i=0;i<b.length;i++){
b[i] = list.get(i);
}
return b;
}
private static String byteToBitString(boolean flag,byte b){
int temp = b;
if(flag){
temp |= 256;
}
String str = Integer.toBinaryString(temp);
if(flag){
return str.substring(str.length()-8);
}else{
return str;
}
}
文件压缩
public static void zipFile(String srcFile,String dstFile){
OutputStream os = null;
ObjectOutputStream = null;
FileInputStream is = null;
try{
is = new FileInputStream(srcFile);
byte[] b = new byte[is.available()];
is.read(b);
byte[] huffmanZip = huffmanZip(b);
os = new FileOutputStream(dstFile);
oos = new ObjectOutputStream(os);
oos.writeObject(huffmanBytes);
oos.writeObject(huffmanCodes)
}catch(Exception e){
}finally{
try{
is.close();
oos.close();
os.clsoe();
}catch(Exception e){
}
}
}
解压文件
public static void unZipFile(Sring zipFile,String dstFile){
InputStream is = null;
ObjectInputStream ois = null;
OutputStream os = null;
try{
is = new FileInputStream(zipFile);
oos = new ObjectInputStream(is);
byte[] huffmanBytes = (byte[])ois.readObject();
Map<Byte,String> huffmanCodes = (Map<Byte,String>)ois.readObject();
byte[] bytes = decode(huffmanCodes,huffmanBytes);
os = new FileOutputStream(dsFile);
os.write(bytes);
}catch(Exception e){
}finally{
try{
os.close();
ois.close();
is.close();
}catcah(Exception e){
}
}
}