-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffman.java
More file actions
177 lines (156 loc) · 5.74 KB
/
Huffman.java
File metadata and controls
177 lines (156 loc) · 5.74 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
import java.util.ArrayList;
import java.util.HashMap;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* This class implements Huffman encoding and compression algorithm.
*/
public class Huffman {
private String path;
private HashMap<Character, Integer> frequencies;
/**
* Constructs a Huffman object with the specified file path.
*
* @param path The path to the input file.
*/
public Huffman(String path){
this.path = path;
this.frequencies = new HashMap<Character, Integer>();
}
/**
* Retrieves the character frequencies.
*
* @return A HashMap containing character frequencies.
*/
public HashMap<Character, Integer> getFrequencies(){
return this.frequencies;
}
/**
* Reads the input file and calculates character frequencies.
*/
public void read_file(){
try {
File myObj = new File(this.path);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i);
if (!this.frequencies.containsKey(c))
this.frequencies.put(c,1);
else
this.frequencies.put(c,this.frequencies.get(c)+1);
}
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred, can't open the file ");
e.printStackTrace();
}
}
/**
* Writes the character frequencies to a file.
*
* @param path The path to the output file.
*/
public void writeFrequencies(String path) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
ArrayList<HashMap.Entry<Character, Integer>> entryList = new ArrayList<>(this.frequencies.entrySet());
entryList.sort(HashMap.Entry.comparingByValue());
for (HashMap.Entry<Character, Integer> entry : entryList) {
writer.write(entry.getKey() + " " + entry.getValue() + "\n");
}
System.out.println("Data written to file: " + path);
} catch (IOException e) {
System.err.println("An error occurred while writing data to file: " + e.getMessage());
e.printStackTrace();
}
}
/**
* Constructs the Huffman encoding tree.
*
* @return The root node of the Huffman encoding tree.
*/
public EncodingTree treeConstruction(){
ArrayList<Node> nodes = new ArrayList<>();
for(char c : this.frequencies.keySet()){
nodes.add(new Node(c,frequencies.get(c)));
}
while(nodes.size()>1){
Node t1 = nodes.get(0);
Node t2 = nodes.get(1);
for (int i = 2; i < nodes.size(); i++) {
Node current = nodes.get(i);
if(current.getFrequency() < t1.getFrequency()){
t2= t1;
t1=current;
}
else if (current.getFrequency() < t2.getFrequency()) {
t2 = current;
}
}
t1.setValue(0);
t2.setValue(1);
Node t = new Node(t1.getFrequency()+t2.getFrequency(),t1,t2);
nodes.add(t);
nodes.remove(t1);
nodes.remove(t2);
}
Node root = nodes.get(0);
return (new EncodingTree(root));
}
/**
* Compresses the input file using the Huffman encoding tree.
*
* @param tree The Huffman encoding tree.
* @param name The name of the compressed file.
*/
public void compression(EncodingTree tree, String name) {
try {
File read = new File(this.path);
Scanner myReader = new Scanner(read);
try (FileOutputStream fos = new FileOutputStream(name)) {
BitOutputStream bos = new BitOutputStream(fos);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i);
int codeValue = tree.getRoot().indepthCourse(c, tree.getRoot());
if (codeValue != -1) {
String code = Integer.toBinaryString(codeValue);
for (char bit : code.toCharArray()) {
bos.write(bit == '1' ? 1 : 0);
}
}
}
}
bos.close();
System.out.println("Data written to file: " + name);
}
myReader.close();
} catch (IOException e) {
System.err.println("An error occurred while compressing the file: " + e.getMessage());
e.printStackTrace();
}
}
/**
* Calculates the compression ratio of the compressed file.
*
* @param pathOriginal The path to the original file.
* @param pathResult The path to the compressed file.
* @return The compression ratio.
*/
public double getCompressionRatio(String pathOriginal, String pathResult){
File original = new File(pathOriginal);
File result = new File(pathResult);
double volume1 = original.length();
double volume2 = result.length();
double compressionRatio = 1 - (volume1/volume2);
return compressionRatio;
}
}