-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman.java
162 lines (151 loc) · 5.12 KB
/
Huffman.java
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
/**
* Created by paulp on 02.01.2016.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.PriorityQueue;
public class Huffman {
private Huffman() {
}
private static class Node {
public Node left, right;
public char ch;
public int mark;
public int freq;
public Node(Node left, Node right, char ch, int mark, int freq) {
this.left = left;
this.right = right;
this.ch = ch;
this.mark = mark;
this.freq = freq;
}
}
private static Node readTrie(boolean isRight) {
boolean bit = BinaryStdIn.readBoolean();
Node newNode = new Node(null, null, '\0', isRight ? 1 : 0, 0);
if (bit) {
newNode.ch = BinaryStdIn.readChar();
System.err.println("char = " + newNode.ch);
} else {
newNode.left = readTrie(false);
newNode.right = readTrie(true);
}
return newNode;
}
private static void writeTrie(Node x) {
if (x.left == null && x.right == null) {
BinaryStdOut.write(true);
BinaryStdOut.write(x.ch);
} else {
BinaryStdOut.write(false);
writeTrie(x.left);
writeTrie(x.right);
}
}
// handle empty input
public static void compress() {
HashMap<Character, Integer> freqTable = new HashMap<>();
ArrayList<Character> message = new ArrayList<>();
//System.err.println("Is empty ? " + BinaryStdIn.isEmpty());
//do {
while (!BinaryStdIn.isEmpty()) {
char ch = BinaryStdIn.readChar();
message.add(ch);
if (!freqTable.containsKey(ch)) freqTable.put(ch, 1);
else freqTable.put(ch, freqTable.get(ch) + 1);
//System.err.println("Numerical value of char = " + ((int) ch));
//System.err.println(ch);
//System.err.println("ITER");
}
//} while (!BinaryStdIn.isEmpty());
PriorityQueue<Node> minpq = new PriorityQueue<>((x, y) -> (x.freq - y.freq));
for (char c : freqTable.keySet()) {
//System.err.printf("%c{%d}\n", c, freqTable.get(c));
minpq.add(new Node(null, null, c, 3, freqTable.get(c)));
}
while (minpq.size() > 1) {
Node x = minpq.poll();
Node y = minpq.poll();
//System.err.println("x = " + x.ch);
//System.err.println("y = " + y.ch);
//System.err.println("");
x.mark = 0;
y.mark = 1;
Node newNode = new Node(x, y, '\0', 3, x.freq + y.freq);
minpq.add(newNode);
}
Node root = null;
if (minpq.size() != 0)
root = minpq.poll();
String[] codes = new String[256];
if (root != null)
buildTable(codes, "", root);
//System.err.println("codes:");
/*for (int i = 0; i < codes.length; i++) {
if (codes[i] != null)
// System.err.println((char) i + " > " + codes[i]);
}*/
//System.err.println("N = " + message.size());
BinaryStdOut.write(message.size());
if (root != null)
writeTrie(root);
for (char c : message) {
String code = codes[c];
for (char c1 : code.toCharArray()) {
BinaryStdOut.write(c1 == '1');
}
}
BinaryStdOut.flush();
BinaryStdOut.close();
}
private static void buildTable(String[] codes, String collected, Node x) {
if (x.left == null && x.right == null) {
codes[x.ch] = collected;
return;
}
buildTable(codes, collected + "0", x.left);
buildTable(codes, collected + "1", x.right);
}
public static void decompress() {
Node root = null;
int N = BinaryStdIn.readInt();
//System.err.println("N = " + N);
if (N == 0) return;
root = readTrie(false);
String[] codes = new String[256];
buildTable(codes, "", root);
//System.err.println("codes:");
/*for (int i = 0; i < codes.length; i++) {
if (codes[i] != null)
System.err.println((char) i + " > " + codes[i]);
}*/
for (int i = 0; i < N; i++) {
Node x = root;
while (x.left != null && x.right != null) {
boolean bit = BinaryStdIn.readBoolean();
if (bit) x = x.right;
else x = x.left;
}
//System.err.println(x.ch);
BinaryStdOut.write(x.ch);
}
BinaryStdOut.flush();
BinaryStdOut.close();
}
public static void main(String[] args) {
assert args.length == 1;
String option = args[0];
switch (option) {
case "+":
compress();
//System.err.println("successfully compressed");
break;
case "-":
decompress();
//System.err.println("successfully decompressed");
break;
default:
//System.err.println("unknown option");
}
}
}