思路:

传入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();
}
}
}

赫夫曼解码(day17)的更多相关文章

  1. 【算法】赫夫曼树(Huffman)的构建和应用(编码、译码)

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  2. 【数据结构】赫夫曼树的实现和模拟压缩(C++)

    赫夫曼(Huffman)树,由发明它的人物命名,又称最优树,是一类带权路径最短的二叉树,主要用于数据压缩传输. 赫夫曼树的构造过程相对比较简单,要理解赫夫曼数,要先了解赫夫曼编码. 对一组出现频率不同 ...

  3. 赫夫曼树JAVA实现及分析

    一,介绍 1)构造赫夫曼树的算法是一个贪心算法,贪心的地方在于:总是选取当前频率(权值)最低的两个结点来进行合并,构造新结点. 2)使用最小堆来选取频率最小的节点,有助于提高算法效率,因为要选频率最低 ...

  4. Android版数据结构与算法(七):赫夫曼树

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 近期忙着新版本的开发,此外正在回顾C语言,大部分时间没放在数据结构与算法的整理上,所以更新有点慢了,不过既然写了就肯定尽力将这部分完全整理好分享出 ...

  5. Java数据结构和算法(四)赫夫曼树

    Java数据结构和算法(四)赫夫曼树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 赫夫曼树又称为最优二叉树,赫夫曼树的一个 ...

  6. javascript实现数据结构: 树和二叉树的应用--最优二叉树(赫夫曼树),回溯法与树的遍历--求集合幂集及八皇后问题

    赫夫曼树及其应用 赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,有着广泛的应用. 最优二叉树(Huffman树) 1 基本概念 ① 结点路径:从树中一个结点到另一个结点的之间的分支 ...

  7. 重温经典之赫夫曼(Huffman)编码

    先看看赫夫曼树假设有n个权值{w1,w2,…,wn},构造一个有n个叶子结点的二叉树,每个叶子结点权值为wi,则其中带权路径长度WPL最小的二叉树称作赫夫曼树或最优二叉树. 赫夫曼树的构造,赫夫曼最早 ...

  8. c++实现哈夫曼树,哈夫曼编码,哈夫曼解码(字符串去重,并统计频率)

    #include <iostream> #include <iomanip> #include <string> #include <cstdlib> ...

  9. puk1521 赫夫曼树编码

    Description An entropy encoder is a data encoding method that achieves lossless data compression by ...

随机推荐

  1. 关于js返回上一页的实现方法

    以前在提交表单的时候,如果提交出错返回的时候信息内容全没了,我不知道要怎么保存,就开始了那种最愚蠢的做法,将填写的数据设置到session中,让后取出来用,不过没有试成功,总是有错,无意之中在我那本j ...

  2. UVA 125 统计路径条数 FLOYD

    这道题目折腾了我一个下午,本来我的初步打算是用SPFA(),进行搜索,枚举出发点,看看能到达某个点多少次,就是出发点到该点的路径数,如果出现环,则置为-1,关键在于这个判环过程,如果简单只找到某个点是 ...

  3. PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]

    题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...

  4. PAT Advanced 1070 Mooncake (25) [贪⼼算法]

    题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...

  5. python编程:从入门到实践----第五章>if 语句

    一.一个简单示例 假设有一个汽车列表,并想将其每辆汽车的名称打印出来.遇到汽车名‘bmw’,以全大写打印:其他汽车名,首字母大写 cars=['audi','bmw','subaru','toyota ...

  6. Matlab高级教程_第二篇:Matlab相见恨晚的模块_01_定时器

    MATLAB的定时器功能(timer函数): 1 从MATLAB6.5版本开始,MATLAB开始支持定时器.相对于传统的pause函数来说,定时器要强大的多,不仅可以等效实现pause的功能,还可以显 ...

  7. java合并一个文件夹下所有txt文件,输出到另一个txt

    最近写了个单元测试,递归调用方法,把同一个文件夹里所有的txt合并输出到一个txt文件.参考了两个博客,分别是已有的方法,还有个就是检测txt文件所用编码的技术贴.如果不检测txt文件的编码,那么转换 ...

  8. Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)

    题:https://codeforces.com/contest/1278/problem/B 思路:还是把1~n分配给俩个数,让他们最终相等 假设刚开始两个数字相等,然后一个数字向前走了abs(b- ...

  9. 爬取迷你mp4各个电影信息

    网站:www.minimp4.com # coding=utf-8 import requests from lxml import etree class Minimpe_moves(object) ...

  10. Solving ordinary differential equations I(Nonstiff Problems),Exercise 1.2:A wrong solution

    (Newton 1671, “Problema II, Solutio particulare”). Solve the total differential equation $$3x^2-2ax+ ...