Java实现哈夫曼编码
Posted 初寒~修
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现哈夫曼编码相关的知识,希望对你有一定的参考价值。
package com.zerohua.work;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class Node{
char letter;
int power;
Node left_son;
Node right_son;
public Node() { super();}
public Node(int power) {
this.power = power;
}
}
char letter;
int power;
Node left_son;
Node right_son;
public Node() { super();}
public Node(int power) {
this.power = power;
}
}
public class CurrDesign {
static Map<Character, String> map = new HashMap<Character, String>();
public static void main(String[] args) {
List <Node>list = codingTable();//输入
Node tree_root = creatHFMtree(list);
creatTable(tree_root,"");
display();
String text = inputText();
encode(text);
String code = inputCode();
decode(code,tree_root);
}
public static String[] encode(String text) {
String []s = new String[100];
char []key = text.toCharArray();
System.out.println("编码结果为:");
for(int i=0;i<key.length;i++) {
s[i]=map.get(key[i]);
System.out.print(s[i]+" ");
}
System.out.println();
return s;
}
public static void decode(String s,Node tree_root) {
System.out.println("译码结果为:");
char []num = s.toCharArray();
Node node = tree_root;
for(int i=0;i<num.length;i++) {
node = check(node,num[i]);
if(node==null) {
node = check(tree_root,num[i]);
}
}
System.out.println(node.letter);
}
public static Node check(Node node,char c) {
if(node.left_son!=null && c==‘0‘)
return node.left_son;
else if(node.right_son!=null && c==‘1‘){
return node.right_son;
}
System.out.print(node.letter);
return null;
}
public static Object getKey(Object value){
for(Object key: map.keySet()){
if(map.get(key).equals(value)){
return key;
}
}
return null;
}
public static String inputText() {
Scanner in = new Scanner(System.in);
System.out.println("请输入要编码的文本,只含大写字母:");
String text = in.next();
return text;
}
public static String inputCode() {
Scanner in = new Scanner(System.in);
System.out.println("请输入编码:");
String code = in.next();
in.close();
return code;
}
public static List<Node> codingTable(){
List <Node>list = new ArrayList<Node>();
int []num = {186,64,13,22,32,103,21,15,47,57,1,2,32,20,57,63,15,1,48,51,80,23,8,18,1,16};
Node node = null;
for(int n=0;n<26;n++) {
node = new Node();
node.letter = (char)(‘A‘+n);
node.power = num[n];
list.add(node);
}
return list;
}
public static void display() {
System.out.println("The huffman coding table are:");
System.out.println(map.toString());
}
public static Node creatHFMtree(List<Node> list){
Node p = null;
int n = 0;
while(!list.isEmpty()) {
if(n!=0) {
list.add(p);
}
Node min1 = new Node(1000);
Node min2 = new Node(999);
for(Node node:list) {
if(node.power < min1.power) {
if(node.power < min2.power) {
min1 = min2;
min2 = node;
}else {
min1 = node;
}
}
}
p = new Node(min1.power+min2.power);
p.left_son = min2;
p.right_son = min1;
list.remove(min1);
list.remove(min2);
n++;
}
return p;
}
public static void creatTable(Node node,String coding){
if(node.left_son!=null) {
creatTable(node.left_son,coding+"0");
}
if(node.right_son!=null) {
creatTable(node.right_son,coding+"1");
}
if(node.left_son==null && node.right_son==null) {
map.put(node.letter, coding);
}
}
}
static Map<Character, String> map = new HashMap<Character, String>();
public static void main(String[] args) {
List <Node>list = codingTable();//输入
Node tree_root = creatHFMtree(list);
creatTable(tree_root,"");
display();
String text = inputText();
encode(text);
String code = inputCode();
decode(code,tree_root);
}
public static String[] encode(String text) {
String []s = new String[100];
char []key = text.toCharArray();
System.out.println("编码结果为:");
for(int i=0;i<key.length;i++) {
s[i]=map.get(key[i]);
System.out.print(s[i]+" ");
}
System.out.println();
return s;
}
public static void decode(String s,Node tree_root) {
System.out.println("译码结果为:");
char []num = s.toCharArray();
Node node = tree_root;
for(int i=0;i<num.length;i++) {
node = check(node,num[i]);
if(node==null) {
node = check(tree_root,num[i]);
}
}
System.out.println(node.letter);
}
public static Node check(Node node,char c) {
if(node.left_son!=null && c==‘0‘)
return node.left_son;
else if(node.right_son!=null && c==‘1‘){
return node.right_son;
}
System.out.print(node.letter);
return null;
}
public static Object getKey(Object value){
for(Object key: map.keySet()){
if(map.get(key).equals(value)){
return key;
}
}
return null;
}
public static String inputText() {
Scanner in = new Scanner(System.in);
System.out.println("请输入要编码的文本,只含大写字母:");
String text = in.next();
return text;
}
public static String inputCode() {
Scanner in = new Scanner(System.in);
System.out.println("请输入编码:");
String code = in.next();
in.close();
return code;
}
public static List<Node> codingTable(){
List <Node>list = new ArrayList<Node>();
int []num = {186,64,13,22,32,103,21,15,47,57,1,2,32,20,57,63,15,1,48,51,80,23,8,18,1,16};
Node node = null;
for(int n=0;n<26;n++) {
node = new Node();
node.letter = (char)(‘A‘+n);
node.power = num[n];
list.add(node);
}
return list;
}
public static void display() {
System.out.println("The huffman coding table are:");
System.out.println(map.toString());
}
public static Node creatHFMtree(List<Node> list){
Node p = null;
int n = 0;
while(!list.isEmpty()) {
if(n!=0) {
list.add(p);
}
Node min1 = new Node(1000);
Node min2 = new Node(999);
for(Node node:list) {
if(node.power < min1.power) {
if(node.power < min2.power) {
min1 = min2;
min2 = node;
}else {
min1 = node;
}
}
}
p = new Node(min1.power+min2.power);
p.left_son = min2;
p.right_son = min1;
list.remove(min1);
list.remove(min2);
n++;
}
return p;
}
public static void creatTable(Node node,String coding){
if(node.left_son!=null) {
creatTable(node.left_son,coding+"0");
}
if(node.right_son!=null) {
creatTable(node.right_son,coding+"1");
}
if(node.left_son==null && node.right_son==null) {
map.put(node.letter, coding);
}
}
}
以上是关于Java实现哈夫曼编码的主要内容,如果未能解决你的问题,请参考以下文章