package test.赫夫曼压缩;
import java.util.*;
public class demo {
public static void main(String[] args) {
String str = "你好我是赵冀超";
Tree tree = new Tree();
HashMap<String,String> hashMap = tree.creatTree(str);
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (hashMap.get(str.charAt(i)+"").contains("null")){
stringBuffer.append((hashMap.get(str.charAt(i)+"").replace("null","")));
}
else{
stringBuffer.append(hashMap.get(str.charAt(i)+""));
}
}
System.out.println(stringBuffer);
System.out.println(tree.encode(stringBuffer.toString()));
}
}
class node{
String character;
int weight;
String code;
node right;
node left;
public node(String character, int weight) {
this.character = character;
this.weight = weight;
}
public void mid(){
if (this.left!=null){
this.left.mid();
}
System.out.println(this.character);
if (this.right!=null){
this.right.mid();
}
}
public void midGetCode(HashMap<String,String>hashMap) {
if (this.left!=null){
this.left.code = this.code+this.left.code;
this.left.midGetCode(hashMap);
}
if (this.left==null&&this.right==null){
hashMap.put(this.character,this.code);
}
if (this.right!=null){
this.right.code = this.code+this.right.code;
this.right.midGetCode(hashMap);
}
}
}
class Tree{
node root;
HashMap<String,String> hashMap;
public HashMap<String,String> creatTree(String str){
int length = str.length();
ArrayList<node> arrayList = new ArrayList<>();
char[] arr = str.toCharArray();
Set<String> strings =new HashSet<>();
for (char n:arr){
strings.add(n+"");
}
HashMap<String,Integer> hashMap = new HashMap<>();
for (String s:strings){
int sum = 0;
for (int i = 0; i < length; i++) {
if (s.equals(str.charAt(i)+"")){
sum++;
}
hashMap.put(str.charAt(i)+"",sum);
}
}
for (int i = 0; i < length; i++) {
arrayList.add(new node(str.charAt(i)+"",hashMap.get(str.charAt(i)+"")));
}
while (arrayList.size()>1){
arrayList.sort(new Comparator<node>() {
@Override
public int compare(node o1, node o2) {
return o1.weight-o2.weight;
}
});
node left = arrayList.get(0);
node right = arrayList.get(1);
left.code = "0";
right.code = "1";
node parent = new node("", left.weight+right.weight);
parent.left = left;
parent.right = right;
arrayList.remove(left);
arrayList.remove(right);
arrayList.add(parent);
}
root = arrayList.get(0);
this.hashMap = getCode();
return this.hashMap;
}
public HashMap<String,String> getCode(){
HashMap<String,String> hashMap = new HashMap<>();
if (root!=null){
root.midGetCode(hashMap);
return hashMap;
}else{
return null;
}
}
public String encode(String str){
HashMap<String,String> hashMap = this.hashMap;
Set<String> keys = hashMap.keySet();
for (String s:keys){
if (hashMap.get(s).contains("null")){
hashMap.put(s,hashMap.get(s).replace("null",""));
}
}
int right = 0;
int left = 0;
String n = "";
Collection<String> collections = hashMap.values();
HashMap<String,String> hashMap1 = new HashMap<>();
for (String s:keys){
hashMap1.put(hashMap.get(s),s);
}
while (right<=str.length()&&right>=left){
String s = str.substring(left,right);
int temp = 0;
for (String m:collections){
if (s.equals(m)){
n+=hashMap1.get(m);
left = right;
temp++;
}
}
if (temp!=0){
}else{
right++;
}
}
return n;
}
}