-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHuffManDisplay.java
More file actions
102 lines (88 loc) · 2.96 KB
/
Copy pathHuffManDisplay.java
File metadata and controls
102 lines (88 loc) · 2.96 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
import java.util.Map;
import java.io.FileWriter;
import java.io.IOException;
public class HuffManDisplay {
public static Huffman huffmanAccessor;
public static gDescriptionWriter descriptionVariable;
public static Coding Coding;
public static String dot;
public static String orginalString;
static String encodedString;
static String DecodedString;
static String[][] DataArray;
static double sizeForGivenString;
static double sizeAfterCoding = 0;
static double sizeOfTable = 0;
static double sumOfCodeSize = 0;
static double reductionPercentage;
static int lengthOfTable = 0;
public HuffManDisplay(String givenString, String dotfilename) {
dot = dotfilename;
orginalString = givenString;
huffmanAccessor = new Huffman(givenString, dotfilename);
descriptionVariable = new gDescriptionWriter(givenString, dotfilename, huffmanAccessor);
Coding = new Coding(givenString, huffmanAccessor.rCodeToCharacter(), huffmanAccessor.rCharacterToCode());
}
public void WriteToDictionary() {
FileWriter fw;
try {
fw = new FileWriter("HuffmanCodesAssigned.txt");
fw.write("Letter, " + "Code: " + "\n");
for (Map.Entry<Character, String> entry : huffmanAccessor.characterToCode.entrySet()) {
String key = entry.getKey().toString();
String val = entry.getValue();
if (key.equals("\n"))
key = "\\n";
fw.write(key + ": " + val + "\n");
}
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void DisplayHuffman() {
DataArray = null;
DataArray = new String[orginalString.length()][3];
encodedString = Coding.encode();
DecodedString = Coding.decode(encodedString);
descriptionVariable.generateDescriptionFile(dot);
int i = 0;
for (Map.Entry<Character, Integer> entry : huffmanAccessor.characterToFrequency.entrySet()) {
String key = entry.getKey().toString();
int val = entry.getValue();
if (key.equals("\n"))
key = "\\n";
DataArray[i][0] = key;
DataArray[i][1] = Integer.toString(val);
i++;
}
int j = 0;
for (Map.Entry<Character, String> entry : huffmanAccessor.characterToCode.entrySet()) {
String key = entry.getKey().toString();
String val = entry.getValue();
if (key.equals("\n"))
key = "\\n";
DataArray[j][2] = val;
j++;
}
sizeForGivenString = 0;
sizeAfterCoding = 0;
sizeOfTable = 0;
sumOfCodeSize = 0;
reductionPercentage = 0;
lengthOfTable = 0;
sizeForGivenString = orginalString.length() * 8;
sizeAfterCoding = encodedString.length();
sizeOfTable = (DataArray[1].length);
for (int z = 0; z < DataArray.length; z++) {
if (DataArray[z][0] == null) {
break;
} else {
sumOfCodeSize = sumOfCodeSize + DataArray[z][2].length();
lengthOfTable++;
}
}
sizeAfterCoding = sizeAfterCoding + sumOfCodeSize + (lengthOfTable*8);
reductionPercentage = -1 * ((sizeAfterCoding - sizeForGivenString) / sizeForGivenString) * 100;
}
}