赫夫曼解码(day17)

思路:

传入map(字节与对应字节出现的次数)和最后生成的要传送的字节。
将他们先转换成对应的二进制字节,再转换成原来的字符串。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
public class HuffumanTreeCode {
public static void main(String[] args) {
String content = " i like like like java do you like a java"; //待转化的字符串
byte[] contentbytes=content.getBytes(); //得到字符串对应的字节数组
System.out.println(Arrays.toString(huffumanzip(contentbytes)));

byte[] sourceBytes = decode(stringMap, huffumanzip(contentbytes));
System.out.println("原来的字符串="+ new String(sourceBytes));

}
public static void preOrder(Node node){
if (node!=null){
node.pre();
}else {
System.out.println("赫夫曼树为空!");
}
}
public static byte[] huffumanzip(byte[] bytes){
List<Node> list=getNodes(bytes); //把byte转化成list存储
Node node=createHuffmanTree(list); //构建哈夫曼树
Map<Byte,String> map=getCodes(node);//对应的赫夫曼编码(根据赫夫曼树)
byte[] bytes1=finallyCode(bytes,map); //压缩编码
return bytes1;
}
//得到字符串对应的list
public static List<Node> getNodes(byte[] bytes){
//创建一个ArrayList
List<Node> list=new ArrayList<>();
//遍历bytes;统计每一个byte出现的次数->map[key ,value]
Map<Byte,Integer> map=new HashMap<>();
for (Byte b:bytes){
Integer conut=map.get(b);
if (conut==null){ // Map还没有这个字符数据,第一次
map.put(b,1);
}else {
map.put(b,conut+1);
}
}
//把每一个键值对转成-个Node对象,并加入到nodes集合
//遍历map
for (Map.Entry<Byte,Integer> map1:map.entrySet()){
list.add(new Node(map1.getKey(),map1.getValue()));
}

return list;

}
//最终发送的赫夫曼编码(最终要发送的字节数组)
public static byte[] finallyCode(byte[] bytes,Map<Byte,String> map){
int len;
int index=0;
String str;
StringBuffer stringBuffer=new StringBuffer();
for (byte b:bytes){大专栏  赫夫曼解码(day17)n>
stringBuffer.append(map.get(b));
}
// System.out.println("ffffffff"+stringBuffer.toString());
//将哈夫曼编码按照8个为一组进行合并,达到压缩的目的
if (stringBuffer.length()%8==0){ //计算新的数组的大小
len=stringBuffer.length()/8;
}else {
len=stringBuffer.length()/8+1;
}
byte[] bytes1=new byte[len];
for (int i=0;i<stringBuffer.length();i+=8){
if (i+8>stringBuffer.length()){
str=stringBuffer.substring(i);
}else {
str=stringBuffer.substring(i,i+8);
}
bytes1[index]=(byte) Integer.parseInt(str,2);
index++;
}
return bytes1;
}

//可以通过List创建对应的赫夫曼树
public static Node createHuffmanTree(List<Node> nodes){
while (nodes.size()>1){
Collections.sort(nodes); //排序,从小到大
Node leftNode=nodes.get(0);
Node rightNode=nodes.get(1);
//创建一颗新的二叉树,它的根节点没有data,只有权值
Node parent=new Node(null,leftNode.weight+rightNode.weight);
parent.left=leftNode;
parent.right=rightNode;
nodes.remove(leftNode);
nodes.remove(rightNode);
nodes.add(parent);
}
return nodes.get(0); //nodes最后的结点,就是赫夫曼树的根结点
}

public static Map<Byte,String> getCodes(Node node){
if (node==null){
System.out.println("传的为空值!");
}else {
getCodes(node,"",stringBuffer);
}
return stringMap;
}

//生成赫夫曼树对应的赫夫曼编码
//思路:
//1.将赫夫曼编码表存放在Map<Byte ,String>形式
//32->01 97->100 100->11000等等[形式]
static Map<Byte,String> stringMap=new HashMap<>();
//创建StringBuffer对象,用于字符串的拼接
static StringBuffer stringBuffer=new StringBuffer();
// @param node传入结点
// @param code路径: 左子结点是0,右子结点1
// @param stringBuilder 用于拼接路径
public static void getCodes(Node node,String code,StringBuffer stringBuffer){
StringBuffer stringBuffer1=new StringBuffer(stringBuffer);
stringBuffer1.append(code);
if (node!=null){
//判断当前node是叶子结点还是非叶子结点
if (node.data==null){
getCodes(node.left,"0",stringBuffer1); //向左递归
getCodes(node.right,"1",stringBuffer1); //向右递归
}else { //说明是一个叶子结点
stringMap.put(node.data,stringBuffer1.toString());
}
}
}
private static byte[] decode(Map<Byte,String> huffmanCodes, byte[] huffmanBytes) { //前一个参数是编码表,后一个参数是最后的转化结果
//1.先得到huffmanBytes对应的二进制的字符串,形式1010100010111...
StringBuilder stringBuilder = new StringBuilder();
//将byte数组转成二进制的字符串
for (int i = 0; i < huffmanBytes.length; i++) {
byte b = huffmanBytes[i];
//判断是不是最后一个字节
boolean flag = (i == huffmanBytes.length - 1);
stringBuilder.append(byteToBitString(!flag, b));
}
// System.out .println("赫夫曼字节数组对应的二进制字符串="+ stringBuilder.toString());
//把字符串安装指定的赫夫曼编码进行解码
//把赫夫员编码表进行调换,因为反向查询a->100100->a
Map<String, Byte> map = new HashMap<String, Byte>();
for (Map.Entry<Byte, String> entry : huffmanCodes.entrySet()) {
map.put(entry.getValue(), entry.getKey());
}
//创建要给集合,存放byte
List<Byte> list = new ArrayList<>();
//i可以理 解成就足索引,扫描 stringBuilder
for (int i = 0; i < stringBuilder.length(); i++ ) {
int count = 1; //小的计数器
boolean flag = true;
Byte b = null;
while (flag) {
//1010100010111...
// 递增的职出key 1
String key = stringBuilder.substring(i,i+count);//i 不动,让count移动,指定匹配到一个字符
b = map.get(key);
if (b == null) {//说明没有匹配到
count++;
} else {
//匹配到
flag = false;
}
list.add(b);
i += count;//i 百接移动到count
}
}
//当for循环结束后,我们list中就存放了所有的字符"i like like like java do you like a java
//把list中的数据放入到byte[]并返回
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) {
//使用变量保存b
int temp = b; //将b转成int
//如果是正数我们还存在补高位
if(flag) {
temp |= 256; //按位与256 1 0000 0000| 0000 0001 => 1 0000 0001
}
String str = Integer.toBinaryString(temp); //返回的是temp对应的二进制的补码
if(flag) {
return str.substring(str.length()-8);
} else {
return str;
}
}

}
//创建Node,存放待数据和权值
class Node implements Comparable<Node>{
Byte data; // 存放数据(字符)本身, 比如'a'=>97
int weight; //权值,表示字符出现的次数
Node right; //右子树
Node left; //左子树
public Node(Byte data,int weight){
this.data=data;
this.weight=weight;
}

@Override
public int compareTo(Node o) {
return this.weight-o.weight; //权值按照从小到大排列
}

@Override
public String toString() {
return "Node{" +
"data=" + data +
", weight=" + weight +
'}';
}

//前序遍历
public void pre(){
System.out.println(this);
if (this.left!=null){
this.left.pre();
}
if (this.right!=null){
this.right.pre();
}
}
}

猜你喜欢

转载自www.cnblogs.com/lijianming180/p/12366603.html