@@ -12,7 +12,9 @@ public static void main( String[] args ){
12
12
File encodingFile ;
13
13
File inputFile ;
14
14
String outputFile ;
15
- HuffmanTree tree ;
15
+ HuffmanTree plainTree ;
16
+ HuffmanTree richTree ;
17
+ HuffmanTree currentTree ;
16
18
StringStackDataPack [] inputData ;
17
19
18
20
@@ -29,9 +31,31 @@ public static void main( String[] args ){
29
31
inputFile = getInputFile (args );
30
32
outputFile = args [3 ];
31
33
richText = getRichText (args );
34
+
35
+ // Build the plainTree
36
+ plainTree = new HuffmanTree (FreqTableHandler .getFrequencies (encodingFile ), false );
37
+
38
+ // Build the richTree--Generate the queue, inject punctuation into it, then build the tree
39
+ PriorityQueue queue = FreqTableHandler .getFrequencies (encodingFile );
40
+ queue .push (new HuffmanNode ("." , 13 ));
41
+ queue .push (new HuffmanNode ("," , 20 ));
42
+ queue .push (new HuffmanNode (";" , 7 ));
43
+ queue .push (new HuffmanNode (":" , 29 ));
44
+ queue .push (new HuffmanNode ("-" , 3 ));
45
+ queue .push (new HuffmanNode ("/" , 11 ));
46
+ queue .push (new HuffmanNode ("\\ " , 7 ));
47
+ queue .push (new HuffmanNode ("(" , 4 ));
48
+ queue .push (new HuffmanNode (")" , 27 ));
49
+ queue .push (new HuffmanNode ("[" , 18 ));
50
+ queue .push (new HuffmanNode ("]" , 21 ));
51
+ queue .push (new HuffmanNode ("{" , 7 ));
52
+ queue .push (new HuffmanNode ("}" , 9 ));
53
+ queue .push (new HuffmanNode ("?" , 13 ));
54
+ queue .push (new HuffmanNode ("!" , 16 ));
55
+ queue .push (new HuffmanNode ("'" , 22 ));
32
56
33
57
// Build Huffman tree
34
- tree = new HuffmanTree (FreqTableHandler . getFrequencies ( encodingFile ) );
58
+ richTree = new HuffmanTree (queue , true );
35
59
// Get data from input file
36
60
inputData = InputFileHandler .getLinesFromFileAsStacks (inputFile );
37
61
@@ -47,6 +71,7 @@ public static void main( String[] args ){
47
71
richText = data .turnRichTextOn () ? true : richText ;
48
72
richText = data .turnRichTextOff () ? false : richText ;
49
73
74
+ currentTree = richText ? richTree : plainTree ;
50
75
51
76
// Setup variables and output boilerplate
52
77
StringStack inputDataStack = data .stack ;
@@ -61,14 +86,14 @@ public static void main( String[] args ){
61
86
// Encode the input file
62
87
if (mode .equals ("encode" )){
63
88
try {
64
- String encodedString = tree .encodeString (inputDataStack );
89
+ String encodedString = currentTree .encodeString (inputDataStack );
65
90
int rawSize = inputString .getBytes ("UTF-8" ).length ;
66
91
double compressedSize = encodedString .length () / 8.0 ;
67
92
68
93
outputWriter .write ("Original size: " + inputString .length () + " characters (" + rawSize + " bytes using UTF-8)\n " );
69
94
outputWriter .write ("Encoded size: " + encodedString .length () + " bits (" + compressedSize + " bytes)\n " );
70
95
outputWriter .write ("Compression: " + (compressedSize / rawSize * 100 ) + "%\n " );
71
- outputWriter .write ("\n Preorder traversal: " + tree .getPreorderTraversal () + "\n " );
96
+ outputWriter .write ("\n Preorder traversal: " + currentTree .getPreorderTraversal () + "\n " );
72
97
outputWriter .write ("\n Encoded string: " + encodedString + "\n \n \n " );
73
98
} catch (EncodingException except ){
74
99
outputWriter .write ("Error during encoding: " + except .getMessage () + "\n " );
@@ -77,13 +102,13 @@ public static void main( String[] args ){
77
102
// Or decode the input file
78
103
else {
79
104
try {
80
- String decodedString = tree .decode (inputDataStack );
105
+ String decodedString = currentTree .decode (inputDataStack );
81
106
int rawSize = decodedString .getBytes ("UTF-8" ).length ;
82
107
double compressedSize = inputString .length () / 8.0 ;
83
108
outputWriter .write ("Encoded size: " + inputString .length () + " bits (" + compressedSize + " bytes)\n " );
84
109
outputWriter .write ("Decoded size: " + decodedString .length () + " characters (" + rawSize + " bytes)\n " );
85
110
outputWriter .write ("Compression: " + (compressedSize / rawSize * 100 ) + "%\n " );
86
- outputWriter .write ("\n Preorder traversal: " + tree .getPreorderTraversal () + "\n " );
111
+ outputWriter .write ("\n Preorder traversal: " + currentTree .getPreorderTraversal () + "\n " );
87
112
outputWriter .write ("\n Decoded string: " + decodedString +"\n \n \n " );
88
113
} catch (EncodingException except ){
89
114
outputWriter .write ("Error during decoding: " + except .getMessage () + "\n " );
0 commit comments