-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathHuffman.java
92 lines (79 loc) · 1.93 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
package cryptography.encoding.huffman;
import java.util.*;
import cryptography.Mode;
public class Huffman {
private static ArrayList<tree> v = new ArrayList<tree>();
public static String huffman(String input, Mode mode) {
String output = "";
if (mode == Mode.ENCODE) {
createList(input);
prepareTree();
encode(v.size() - 1);
output = code(input);
}
return output;
}
private static void createList(String data) {
for (int a = 0; a < data.length(); a++) {
if (!exist(Character.toString(data.charAt(a)))) {
tree temp = new tree();
temp.ch = Character.toString(data.charAt(a));
temp.freq = 1;
temp.left = -1;
temp.right = -1;
temp.code = "";
v.add(temp);
}
}
}
private static boolean exist(String c) {
for (int a = 0; a < v.size(); a++) {
if (v.get(a).ch.equals(c)) {
v.get(a).freq++;
return true;
}
}
return false;
}
private static void prepareTree() {
int pos = 0;
int n = v.size();
for (int a = 0; a < n - 1; a++) {
Collections.sort(v);
tree t1 = v.get(pos + 0);
tree t2 = v.get(pos + 1);
tree temp = new tree();
temp.ch = t1.ch + t2.ch;
temp.freq = t1.freq + t2.freq;
temp.left = pos + 0;
temp.right = pos + 1;
temp.code = "";
v.add(temp);
pos += 2;
}
}
private static void encode(int pos) {
if (pos != -1) {
tree parent = v.get(pos);
if (parent.left != -1) {
v.get(parent.left).code = parent.code + "0";
}
if (parent.left != -1) {
v.get(parent.right).code = parent.code + "1";
}
encode(parent.left);
encode(parent.right);
}
}
private static String code(String data) {
String str = "";
for (int a = 0; a < data.length(); a++) {
for (int b = 0; b < v.size(); b++) {
if (Character.toString(data.charAt(a)).contentEquals(v.get(b).ch)) {
str += v.get(b).code + "";
}
}
}
return str;
}
}