forked from sublime1809/HuffmanTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanTree.java
165 lines (146 loc) · 4.81 KB
/
HuffmanTree.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
163
164
165
import java.util.*;
import java.io.*;
public class HuffmanTree implements HuffmanInterface{
public String alphabet = "muckd!ap,.re ";
public ArrayList<alphabits> symbols = new ArrayList<alphabits>();
public alphabits[] letters = new alphabits[13];
public BinaryTree<alphabits> root = null;
public int age = 0;
public static void main(String args[]){
HuffmanTree temp = new HuffmanTree();
temp.createTree("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEmud!ap. r mkucd!ap,.reemk e");
System.out.println(temp.encodeMessage("eEeEeE"));
}
public void createTree(String sampleText){
for(int i = 0; i < sampleText.length(); i++){
char inspect = Character.toLowerCase(sampleText.charAt(i));
if(alphabet.indexOf(inspect) != -1){
addToHuff(inspect);
}
}
for(int i=0; i<symbols.size(); i++){
letters[i] = symbols.get(i);
}
buildTree(letters);
}
private int findInSymbols(char letter){
int index = -1;
for(int j=0; j < symbols.size(); j++){
if(symbols.get(j).symbol.equals(letter)){
return j;
}
}
return index;
}
private void addToHuff(char letter){
if(symbols.isEmpty())
symbols.add(new alphabits(1, letter));
else {
if(findInSymbols(letter) != -1)
symbols.get(findInSymbols(letter)).weight++;
else
symbols.add(new alphabits(1, letter));
}
}
private void buildTree(alphabits[] symbols){
Queue<BinaryTree<alphabits>> theQueue = new PriorityQueue<BinaryTree<alphabits>>(symbols.length, new CompareHuffmanTrees());
for(alphabits nextSymbol : symbols){
BinaryTree<alphabits> aBinaryTree = new BinaryTree<alphabits>(nextSymbol, null, null, age++);
theQueue.offer(aBinaryTree);
}
while(theQueue.size() > 1){
BinaryTree<alphabits> left = theQueue.poll();
BinaryTree<alphabits> right = theQueue.poll();
double wl = left.getData().weight;
double wr = right.getData().weight;
alphabits sum = new alphabits(wl + wr, null);
BinaryTree<alphabits> newTree = new BinaryTree<alphabits>(sum, left, right, age++);
theQueue.offer(newTree);
}
root = theQueue.poll();
}
private boolean in(BinaryTree<alphabits> root, char symbol){
if(root.isLeaf()){
if(root.getData().symbol.equals(symbol))
return true;
else
return false;
}
else
return (in(root.getLeftSubtree(),symbol) || in(root.getRightSubtree(), symbol));
}
private String code(char symbol){
BinaryTree<alphabits> currentTree = root;
StringBuilder binary = new StringBuilder();
while(!currentTree.isLeaf()){
if(in(currentTree.getLeftSubtree(), symbol)){
binary.append("0");
currentTree = currentTree.getLeftSubtree();
}
else if(in(currentTree.getRightSubtree(), symbol)){
binary.append("1");
currentTree = currentTree.getRightSubtree();
}
}
return binary.toString();
}
public String encodeMessage(String message){
StringBuilder result = new StringBuilder();
message = message.toLowerCase();
for(int i=0; i < message.length(); i++){
if(findInSymbols(message.charAt(i)) != -1){
result.append(code(message.charAt(i)));
}
}
return result.toString();
}
public String decodeMessage(String encodedMessage){
StringBuilder result = new StringBuilder();
BinaryTree<alphabits> currentTree = root;
for(int i = 0; i < encodedMessage.length(); i++){
if(encodedMessage.charAt(i) == '1')
currentTree = currentTree.getRightSubtree();
else
currentTree = currentTree.getLeftSubtree();
if(currentTree.isLeaf()){
alphabits theData = currentTree.getData();
result.append(theData.symbol);
currentTree = root;
}
}
return result.toString();
}
private static class alphabits implements Serializable {
private double weight;
public Character symbol;
public alphabits(double weight, Character symbol){
this.weight = weight;
this.symbol = symbol;
}
}
private static class CompareHuffmanTrees implements Comparator<BinaryTree<alphabits>> {
public int compare(BinaryTree<alphabits> treeLeft, BinaryTree<alphabits> treeRight) {
double wLeft = treeLeft.getData().weight;
double wRight = treeRight.getData().weight;
if(Double.compare(wLeft, wRight) == 0){
if(treeLeft.isLeaf() && !treeRight.isLeaf())
return -1;
else if(!treeLeft.isLeaf() && treeRight.isLeaf())
return 1;
else if(treeLeft.isLeaf() && treeRight.isLeaf()){
if(treeLeft.getData().symbol < treeRight.getData().symbol)
return -1;
else if(treeLeft.getData().symbol > treeRight.getData().symbol)
return 1;
}
else if(!treeLeft.isLeaf() && !treeRight.isLeaf()){
if(treeLeft.age < treeRight.age)
return -1;
else if (treeLeft.age > treeRight.age)
return 1;
}
}
return Double.compare(wLeft, wRight);
}
}
}