-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTree.java
More file actions
194 lines (168 loc) · 4.67 KB
/
HuffmanTree.java
File metadata and controls
194 lines (168 loc) · 4.67 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
public class HuffmanTree{
int size, ctr = 0, arrCount = 0;
int[] intArray;
static int[] rgbCharacters;
int width, height;
Node first = null;
String code = "", binaryCode = "", imageCode = "";
public HuffmanTree(int size, int[] intArray, int width, int height){
this.size = size;
this.intArray = intArray;
this.width = width;
this.height = height;
rgbCharacters = new int[width*height];
System.out.println(rgbCharacters.length);
createDistribution();
}
public void createDistribution(){
try{
FileReader reader = new FileReader(Compression.file);
BufferedReader bf = new BufferedReader(reader);
String line;
while((line = bf.readLine()) != null){
String[] cut = line.split(" ");
Node node = new Node(Integer.parseInt(cut[0]), Integer.parseInt(cut[1]));
sortedInsert(node);
arrCount++;
}
}catch(Exception e){}
huffman();
}
public void huffman(){
Node node1 = new Node();
while(arrCount != 1){
node1 = createHuffman();
sortedInsert(node1);
}
traverseTree(first, code);
String chunk = "";
File file = new File(HuffmanFrame.huffName +".pards");
try{
FileWriter fw = new FileWriter(file);
for(int i = 0 ; i < intArray.length; i++){
printBinary(first, intArray[i]);
if(binaryCode.length() >= 7){
while(binaryCode.length() >= 7){
chunk = binaryCode.substring(0, 7);
binaryCode = binaryCode.substring(7, binaryCode.length());
fw.write(getChunk(chunk));
}
}
if(i == intArray.length - 1){
if(binaryCode.length() < 7){
chunk = binaryCode;
while(chunk.length() != 7){
chunk += "0";
}
fw.write(getChunk(chunk));
binaryCode = "";
}
}
}
fw.close();
}catch(Exception e){}
String p = "";
try{
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
int num;
while((num = reader.read()) != -1){
p = Integer.toBinaryString(num);
String ex = "";
for(int i = 0; i < 7-p.length(); i++){
ex += "0";
}
String x = ex + p;
imageCode += x;
}
}catch(Exception e){}
Node currently = first;
for(int i = 0; i < imageCode.length(); i++){
char ch = imageCode.charAt(i);
if(currently.left == null && currently.right == null){
rgbCharacters[ctr++] = currently.getChar();
currently = first;
}
if(currently.left != null && currently.right != null){
if(ch == '0'){
currently = currently.left;
}else if(ch == '1'){
currently = currently.right;
}
}
}
}
public Node createHuffman(){
Node temp;
Node node1 = new Node();
for(int i = 1; i <=2; i++){
try{
temp = first;
first = first.next;
if(i == 1){
node1.left = temp;
node1.left.setChar(temp.getChar());
node1.setFrequency(temp.getFrequency());
}
if(i == 2){
node1.right = temp;
node1.right.setChar(temp.getChar());
node1.setFrequency(node1.getFrequency() + temp.getFrequency());
node1.setChar(node1.getFrequency());
}
}catch(NullPointerException e){}
}
try{
arrCount--;
}catch(NullPointerException e){}
return node1;
}
public char getChunk(String str){
int num = Integer.parseInt(str, 2);
char ascii =(char) num;
return ascii;
}
public void sortedInsert(Node new_node){
Node current;
if (first == null || first.getFrequency() >= new_node.getFrequency()){
new_node.next = first;
first = new_node;
}else{
current = first;
while (current.next != null && current.next.getFrequency() < new_node.getFrequency())
current = current.next;
new_node.next = current.next;
current.next = new_node;
}
}
public void traverseTree(Node current, String code){
if(current != null){
if(current.left == null && current.right == null){
current.setBitString(code);
}
if(current.right != null){
traverseTree(current.right, code + "1");
}
if(current.left != null){
traverseTree(current.left, code + "0");
}
}
}
public void printBinary(Node current, int c){
if(current != null){
if(current.left == null && current.right == null && current.getChar() == c){
binaryCode += current.getBitString();
}
if(current.left != null){
printBinary(current.left, c);
}
if(current.right != null){
printBinary(current.right, c);
}
}
}
}