Skip to content

Commit 53cd98b

Browse files
committed
update huffman
1 parent 4ec04c1 commit 53cd98b

File tree

16 files changed

+218
-216
lines changed

16 files changed

+218
-216
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package HuffmanCode;
1+
package Huffman;
22

33
import java.io.IOException;
44
import java.io.InputStream;
55

6+
/* BitInputStream using decorator pattern */
67
public final class BitInputStream {
78
private InputStream input;
89
private int nextBits;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package HuffmanCode;
1+
package Huffman;
22

33
import java.io.IOException;
44
import java.io.OutputStream;
55

6+
/* BitOutputStream using decorator pattern */
67
public final class BitOutputStream {
78

89
private OutputStream output;
File renamed without changes.
File renamed without changes.

Huffman/Huffman.java

Lines changed: 129 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,136 @@
11
package Huffman;
22

3-
import List.ListModule;
4-
import List.ListModule.List;
3+
import Heap.LeftistModule;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.FileReader;
8+
import java.io.FileWriter;
9+
import java.io.IOException;
510

6-
public class Huffman {
11+
public class Huffman{
12+
13+
int[] occ = new int[256];
14+
Node aC;
15+
LeftistModule.Leftist<Node> tas = LeftistModule.emptyLeftist();
16+
String[] code = new String[256];
17+
18+
public void encodeMessage(File in, File out) {
19+
countFrequency(in);
20+
buildLeftist();
21+
buildCodeTree();
22+
generateCodes();
23+
24+
try {
25+
FileReader fr = new FileReader(in);
26+
BitOutputStream fw = new BitOutputStream(new FileOutputStream(out));
27+
int lu;
28+
while ((lu = fr.read()) != -1) {
29+
fw.write(code[lu]);
30+
}
31+
serializeTree();
32+
fw.close();
33+
fr.close();
34+
} catch (IOException e) {
35+
}
36+
}
37+
38+
public void decodeMessage(File in, File out) {
39+
deserializeTree();
40+
try {
41+
BitInputStream fr = new BitInputStream(new FileInputStream(in));
42+
FileWriter fw = new FileWriter(out, true);
43+
int lu, count = aC.p.fr;
44+
Node tmp = aC;
45+
while ((lu = fr.read()) != -1 && count > 0) {
46+
if (tmp.isLeaf()) {
47+
fw.append((char) tmp.p.car);
48+
count--;
49+
tmp = aC;
50+
}
51+
52+
if (lu == 0) {
53+
tmp = tmp.left;
54+
} else {
55+
tmp = tmp.right;
56+
}
57+
58+
}
59+
60+
fw.close();
61+
fr.close();
62+
} catch (Exception ex) {
63+
64+
}
65+
}
66+
67+
public void countFrequency(File in) {
68+
try {
69+
FileReader fr = new FileReader(in);
70+
int lu;
71+
while (-1 != (lu = fr.read())) {
72+
occ[lu]++;
73+
}
74+
fr.close();
75+
} catch (IOException e) {
76+
System.out.println("file not found");
77+
}
78+
}
79+
80+
public void buildLeftist() {
81+
for (int i = 0; i < occ.length; i++) {
82+
if (occ[i] != 0) {
83+
tas = tas.insert(new Node(new Paire(occ[i], i)));
84+
}
85+
}
86+
}
87+
88+
public void buildCodeTree() {
89+
while (tas.size() > 1) {
90+
Node x = tas.min();
91+
tas = tas.delete();
92+
Node y = tas.min();
93+
tas = tas.delete();
94+
aC = Node.build(new Paire(x.p.fr + y.p.fr), x, y);
95+
tas = tas.insert(aC);
96+
}
97+
aC = tas.min();
98+
}
99+
100+
public void generateCodes() {
101+
construire(aC, "");
102+
}
103+
104+
public void construire(Node x, String s) {
105+
if (x.isLeaf()) {
106+
this.code[x.p.car] = s;
107+
} else {
108+
construire(x.left, s + "0");
109+
construire(x.right, s + "1");
110+
}
111+
}
112+
113+
public void serializeTree() {
114+
try {
115+
Node.serialaze(aC, "src/Huffman/data/tree");
116+
} catch (IOException ex) {
117+
}
118+
}
119+
120+
public void deserializeTree() {
121+
try {
122+
aC = Node.deserialaze("src/Huffman/data/tree");
123+
} catch (IOException ex) {
124+
}
125+
}
7126

8127
public static void main(String[] args) {
9-
String text = "There is no patch ...";
10-
List<Character> ascii = ListModule.emptyList();
11-
for (int i = 0; i < text.length(); i++) {
12-
ascii = ListModule.list(text.charAt(i), ascii);
13-
}
14-
CodeTree cTree = Fork.makeCodeTreeFromText(ascii);
15-
System.out.println("-------- CodeTree --------");
16-
System.out.println(cTree);
17-
18-
List<Integer> code = cTree.encode(ascii, ListModule.emptyList());
19-
System.out.println("-------- Binnary Code --------");
20-
System.out.println(code);
21-
22-
List<Character> plain = cTree.decode(code, ListModule.emptyList());
23-
System.out.println("-------- Plain Text --------");
24-
String result = plain.reduce("", (x, y) -> y + x);
25-
System.out.println(result);
128+
Huffman hf = new Huffman();
129+
130+
File in = new File("src/Huffman/data/plain");
131+
File out = new File("src/Huffman/data/code");
132+
133+
hf.encodeMessage(in, out);
134+
//hf.decodeMessage(out, in);
26135
}
27136
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package HuffmanCode;
1+
package Huffman;
22

33
import java.io.FileInputStream;
44
import java.io.FileOutputStream;

Huffman/Paire.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
package Huffman;
22

3-
import List.ListModule;
4-
import List.ListModule.List;
53
import java.io.Serializable;
64

75
public class Paire implements Comparable<Paire>, Serializable {
86

9-
private int fr;
10-
private List<Character> chars;
7+
int fr;
8+
int car;
119

12-
public Paire(int fr, List<Character> chars) {
10+
public Paire(int fr, int car) {
1311
this.fr = fr;
14-
this.chars = chars;
15-
}
16-
17-
public int getFr() {
18-
return fr;
19-
}
20-
21-
public List<Character> getChars() {
22-
return chars;
12+
this.car = car;
2313
}
2414

2515
public Paire(int fr) {
@@ -37,12 +27,7 @@ public int compareTo(Paire p) {
3727

3828
@Override
3929
public String toString() {
40-
return "("+chars+fr+")";
30+
return car+"("+fr+")";
4131
}
4232

43-
public static Paire merge(Paire a, Paire b){
44-
return new Paire(a.getFr()+b.getFr(), ListModule.merge(a.getChars(), b.getChars()));
45-
}
46-
47-
4833
}

HuffmanCode/Huffman.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

HuffmanCode/Paire.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)