-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHuffman.java
More file actions
136 lines (116 loc) · 3.38 KB
/
Huffman.java
File metadata and controls
136 lines (116 loc) · 3.38 KB
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
package Huffman;
import Heap.LeftistModule;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Huffman{
int[] occ = new int[256];
Node aC;
LeftistModule.Leftist<Node> tas = LeftistModule.emptyLeftist();
String[] code = new String[256];
public void encodeMessage(File in, File out) {
countFrequency(in);
buildLeftist();
buildCodeTree();
generateCodes();
try {
FileReader fr = new FileReader(in);
BitOutputStream fw = new BitOutputStream(new FileOutputStream(out));
int lu;
while ((lu = fr.read()) != -1) {
fw.write(code[lu]);
}
serializeTree();
fw.close();
fr.close();
} catch (IOException e) {
}
}
public void decodeMessage(File in, File out) {
deserializeTree();
try {
BitInputStream fr = new BitInputStream(new FileInputStream(in));
FileWriter fw = new FileWriter(out, true);
int lu, count = aC.p.fr;
Node tmp = aC;
while ((lu = fr.read()) != -1 && count > 0) {
if (tmp.isLeaf()) {
fw.append((char) tmp.p.car);
count--;
tmp = aC;
}
if (lu == 0) {
tmp = tmp.left;
} else {
tmp = tmp.right;
}
}
fw.close();
fr.close();
} catch (Exception ex) {
}
}
public void countFrequency(File in) {
try {
FileReader fr = new FileReader(in);
int lu;
while (-1 != (lu = fr.read())) {
occ[lu]++;
}
fr.close();
} catch (IOException e) {
System.out.println("file not found");
}
}
public void buildLeftist() {
for (int i = 0; i < occ.length; i++) {
if (occ[i] != 0) {
tas = tas.insert(new Node(new Paire(occ[i], i)));
}
}
}
public void buildCodeTree() {
while (tas.size() > 1) {
Node x = tas.min();
tas = tas.delete();
Node y = tas.min();
tas = tas.delete();
aC = Node.build(new Paire(x.p.fr + y.p.fr), x, y);
tas = tas.insert(aC);
}
aC = tas.min();
}
public void generateCodes() {
construire(aC, "");
}
public void construire(Node x, String s) {
if (x.isLeaf()) {
this.code[x.p.car] = s;
} else {
construire(x.left, s + "0");
construire(x.right, s + "1");
}
}
public void serializeTree() {
try {
Node.serialaze(aC, "src/Huffman/data/tree");
} catch (IOException ex) {
}
}
public void deserializeTree() {
try {
aC = Node.deserialaze("src/Huffman/data/tree");
} catch (IOException ex) {
}
}
public static void main(String[] args) {
Huffman hf = new Huffman();
File in = new File("src/Huffman/data/plain");
File out = new File("src/Huffman/data/code");
hf.encodeMessage(in, out);
//hf.decodeMessage(out, in);
}
}