|
1 | 1 | package Huffman; |
2 | 2 |
|
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; |
5 | 10 |
|
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 | + } |
7 | 126 |
|
8 | 127 | 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); |
26 | 135 | } |
27 | 136 | } |
0 commit comments