哈夫曼树
package com.weeks.tree.huffmantree;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HuffmanTree {
public static void main(String[] args) {
int[] arr = {13, 7, 8, 3, 29, 6, 1};
Node root = createHuffmanTree(arr);
preOrder(root);
}
public static Node createHuffmanTree(int[] arr){
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
nodes.add(new Node(arr[i]));
}
while(nodes.size() > 1) {
Collections.sort(nodes);
Node leftNode = nodes.get(0);
Node rightNode = nodes.get(1);
Node parent = new Node(leftNode.getValue() + rightNode.getValue());
parent.setLeft(leftNode);
parent.setRight(rightNode);
nodes.remove(leftNode);
nodes.remove(rightNode);
nodes.add(parent);
}
return nodes.get(0);
}
public static void preOrder(Node root){
if(root == null){
System.out.println("空树不能遍历!!!");
}
root.preOrder();
}
}
class Node implements Comparable<Node> {
private int value;
private Node left;
private Node right;
public Node(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
@Override
public int compareTo(Node o) {
return this.value - o.value;
}
public void preOrder(){
if(this == null){
return;
}
System.out.println(this);
if(this.left != null){
this.left.preOrder();
}
if(this.right != null){
this.right.preOrder();
}
}
}
哈夫曼编码
package com.weeks.tree.huffmancode;
import java.util.*;
public class HuffmanCode {
public static void main(String[] args) {
String content = "i like like like java do you like a java";
List<Node> nodes = getNodes(content);
Node root = createHuffmanTree(nodes);
root.preOrder();
getCodes(root);
System.out.println(huffmanCodes);
}
public static List<Node> getNodes(String str){
byte[] bytes = str.getBytes();
List<Node> nodes = new ArrayList<>();
HashMap<Byte, Integer> map = new HashMap<>();
for(byte b : bytes){
Integer count = map.get(b);
if(count == null){
map.put(b, 1);
}else{
map.put(b, count + 1);
}
}
for (Map.Entry<Byte, Integer> entry: map.entrySet()){
nodes.add(new Node(entry.getKey(), entry.getValue()));
}
return nodes;
}
public static Node createHuffmanTree(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.getCount() + rightNode.getCount());
parent.setLeft(leftNode);
parent.setRight(rightNode);
nodes.remove(leftNode);
nodes.remove(rightNode);
nodes.add(parent);
}
return nodes.get(0);
}
public void preOrder(Node root){
if(root == null){
System.out.println("空树无法遍历");
}else{
root.preOrder();
}
}
static Map<Byte, String> huffmanCodes = new HashMap<>();
static StringBuilder stringBuilder = new StringBuilder();
public static void getCodes(Node node, String code, StringBuilder sb){
StringBuilder stringBuilder2 = new StringBuilder(sb);
stringBuilder2.append(code);
if(node != null){
if(node.getContent() == null){
getCodes(node.getLeft(), "0", stringBuilder2);
getCodes(node.getRight(), "1", stringBuilder2);
}else{
huffmanCodes.put(node.getContent(), stringBuilder2.toString());
}
}
}
public static Map<Byte, String> getCodes(Node root){
if(root == null){
return null;
}
getCodes(root.getLeft(), "0", stringBuilder);
getCodes(root.getRight(), "1", stringBuilder);
return huffmanCodes;
}
}
class Node implements Comparable<Node>{
private Byte content;
private int count;
private Node left;
private Node right;
public Node(Byte content, int count){
this.content = content;
this.count = count;
}
public Byte getContent() {
return content;
}
public void setContent(Byte content) {
this.content = content;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"content=" + content +
", count=" + count +
'}';
}
@Override
public int compareTo(Node o) {
return this.getCount() - o.getCount();
}
public void preOrder(){
System.out.println(this);
if(this.getLeft() != null){
this.getLeft().preOrder();
}
if(this.getRight() != null){
this.getRight().preOrder();
}
}
}
哈夫曼编码压缩
package com.weeks.tree.huffmancode;
import java.util.*;
public class HuffmanCode {
public static void main(String[] args) {
String content = "i like like like java do you like a java";
byte[] bytes = content.getBytes();
byte[] huffmanZipBytes = huffmanZip(bytes);
System.out.println("压缩后的字节数组:" + Arrays.toString(huffmanZipBytes));
}
public static List<Node> getNodes(byte[] bytes){
List<Node> nodes = new ArrayList<>();
HashMap<Byte, Integer> map = new HashMap<>();
for(byte b : bytes){
Integer count = map.get(b);
if(count == null){
map.put(b, 1);
}else{
map.put(b, count + 1);
}
}
for (Map.Entry<Byte, Integer> entry: map.entrySet()){
nodes.add(new Node(entry.getKey(), entry.getValue()));
}
return nodes;
}
public static Node createHuffmanTree(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.getCount() + rightNode.getCount());
parent.setLeft(leftNode);
parent.setRight(rightNode);
nodes.remove(leftNode);
nodes.remove(rightNode);
nodes.add(parent);
}
return nodes.get(0);
}
public void preOrder(Node root){
if(root == null){
System.out.println("空树无法遍历");
}else{
root.preOrder();
}
}
static Map<Byte, String> huffmanCodes = new HashMap<>();
static StringBuilder stringBuilder = new StringBuilder();
public static void getCodes(Node node, String code, StringBuilder sb){
StringBuilder stringBuilder2 = new StringBuilder(sb);
stringBuilder2.append(code);
if(node != null){
if(node.getContent() == null){
getCodes(node.getLeft(), "0", stringBuilder2);
getCodes(node.getRight(), "1", stringBuilder2);
}else{
huffmanCodes.put(node.getContent(), stringBuilder2.toString());
}
}
}
public static Map<Byte, String> getCodes(Node root){
if(root == null){
return null;
}
getCodes(root.getLeft(), "0", stringBuilder);
getCodes(root.getRight(), "1", stringBuilder);
return huffmanCodes;
}
public static byte[] zip(byte[] bytes, Map<Byte, String> huffmanCodes){
StringBuilder huffmanCodeStr = new StringBuilder();
for(byte b : bytes){
huffmanCodeStr.append(huffmanCodes.get(b));
}
int len = 0;
if(huffmanCodeStr.length() % 8 == 0){
len = huffmanCodeStr.length() / 8;
}else{
len = huffmanCodeStr.length() / 8 + 1;
}
byte[] transform = new byte[len];
int index = 0;
for (int i = 0; i < huffmanCodeStr.length(); i+=8) {
if(i + 8 <= huffmanCodeStr.length()){
transform[index++] = (byte)Integer.parseInt(huffmanCodeStr.substring(i, i + 8));
}else{
transform[index++] = (byte)Integer.parseInt(huffmanCodeStr.substring(i));
}
}
return transform;
}
public static byte[] huffmanZip(byte[] bytes){
List<Node> nodes = getNodes(bytes);
Node huffmanTree = createHuffmanTree(nodes);
Map<Byte, String> codes = getCodes(huffmanTree);
byte[] zip = zip(bytes, codes);
return zip;
}
}
class Node implements Comparable<Node>{
private Byte content;
private int count;
private Node left;
private Node right;
public Node(Byte content, int count){
this.content = content;
this.count = count;
}
public Byte getContent() {
return content;
}
public void setContent(Byte content) {
this.content = content;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"content=" + content +
", count=" + count +
'}';
}
@Override
public int compareTo(Node o) {
return this.getCount() - o.getCount();
}
public void preOrder(){
System.out.println(this);
if(this.getLeft() != null){
this.getLeft().preOrder();
}
if(this.getRight() != null){
this.getRight().preOrder();
}
}
}