Skip to content

Commit ee6dc5f

Browse files
committed
Night commit. Added rich text feature to Huffman tree encoding and decoding. Fixed minor bugs.
1 parent 30897d6 commit ee6dc5f

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

Lab_3/HuffmanTree.java

+43-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ class HuffmanTree
55
private HuffmanNode treeRoot;
66
private HuffmanCode[] codes;
77
private HuffmanCode[] binaryCodeTree;
8+
private boolean richText;
89

910
/////////////////////////
1011
// Constructors
1112
/////////////////////////
1213

13-
HuffmanTree(PriorityQueue queue){
14+
HuffmanTree(PriorityQueue queue, boolean richText){
1415
/**
1516
** Constructor for HuffmanTree. The HuffmanTree must be initialized with a PriorityQueue
1617
** that is seeded with the characters and frequencies to be used in the Huffman
@@ -23,6 +24,7 @@ class HuffmanTree
2324
buildTree(queue);
2425
this.codes = generateCodes();
2526
this.binaryCodeTree = buildSearchTree();
27+
this.richText = richText;
2628

2729
}
2830

@@ -36,6 +38,20 @@ public String encodeString(StringStack data){
3638
String character = data.pop();
3739
String encodedCharacter = encode(character);
3840
encodedString += encodedCharacter;
41+
42+
// Add richText enhancements
43+
if(!encodedCharacter.equals("") && richText){
44+
// Add the capiatlization flag
45+
encodedString += character.toUpperCase().equals(character) ? "1" : "0";
46+
// Check if the next character is a space; if so, add a flag and delete the space
47+
if(!data.isEmpty() && data.peek().equals(" ")){
48+
encodedString += "1";
49+
data.pop();
50+
} else{
51+
encodedString += "0";
52+
}
53+
}
54+
3955
}
4056
return encodedString;
4157
}
@@ -51,18 +67,38 @@ public String decode(StringStack codeString){
5167
**/
5268
String decodedString = "";
5369
while(!codeString.isEmpty()){
70+
String decodedCharacter;
5471
HuffmanNode currentNode = treeRoot;
72+
boolean capitalizeCharacter = false;
73+
boolean addSpace = false;
74+
5575
while(!currentNode.isLeaf()){
5676
try{
5777
currentNode = codeString.pop().equals("0") ? currentNode.getLeftChild() : currentNode.getRightChild();
5878
} catch (EmptyStackException except) {
59-
EncodingException exception = new EncodingException("The encoded string is invalid--does not match code tree");
60-
throw exception;
79+
throw new EncodingException("The encoded string is invalid--does not match code tree");
6180
}
6281
}
63-
decodedString += currentNode.getChars();
64-
65-
// TODO ADD HANDLING FOR SPACES AND/OR CAPITALIZATION
82+
decodedCharacter = currentNode.getChars();
83+
84+
// Make richText adjustments if enabled
85+
if(richText){
86+
try{
87+
capitalizeCharacter = codeString.pop().equals("1") ? true : false;
88+
addSpace = codeString.pop().equals("1") ? true : false;
89+
} catch (EmptyStackException except){
90+
throw new EncodingException("The encoded string is invalid--does not match code tree");
91+
}
92+
if(!capitalizeCharacter){
93+
decodedCharacter = decodedCharacter.toLowerCase();
94+
}
95+
}
96+
97+
// Append new character to decodedString
98+
decodedString += decodedCharacter;
99+
if(addSpace){
100+
decodedString += " ";
101+
}
66102
}
67103

68104
return decodedString;
@@ -93,7 +129,7 @@ private HuffmanCode[] buildSearchTree(){
93129
**
94130
** @return HuffmanCode[] A binary tree of HuffmanCode objects contained in an array.
95131
**/
96-
HuffmanCode[] tree = new HuffmanCode[(int) Math.pow(2, leafNodes.length) + 1];
132+
HuffmanCode[] tree = new HuffmanCode[(int) Math.pow(2, leafNodes.length /2 ) + 1];
97133
int midNode = codes.length / 2;
98134
tree[1] = codes[midNode];
99135
for(int i=0; i<codes.length; i++){

Lab_3/JPowers_Lab3_Analysis.odt

-8.96 KB
Binary file not shown.

Lab_3/Lab3Main.java

+31-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public static void main( String[] args ){
1212
File encodingFile;
1313
File inputFile;
1414
String outputFile;
15-
HuffmanTree tree;
15+
HuffmanTree plainTree;
16+
HuffmanTree richTree;
17+
HuffmanTree currentTree;
1618
StringStackDataPack[] inputData;
1719

1820

@@ -29,9 +31,31 @@ public static void main( String[] args ){
2931
inputFile = getInputFile(args);
3032
outputFile = args[3];
3133
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));
3256

3357
// Build Huffman tree
34-
tree = new HuffmanTree(FreqTableHandler.getFrequencies(encodingFile));
58+
richTree = new HuffmanTree(queue, true);
3559
// Get data from input file
3660
inputData = InputFileHandler.getLinesFromFileAsStacks(inputFile);
3761

@@ -47,6 +71,7 @@ public static void main( String[] args ){
4771
richText = data.turnRichTextOn() ? true : richText;
4872
richText = data.turnRichTextOff() ? false : richText;
4973

74+
currentTree = richText ? richTree : plainTree;
5075

5176
// Setup variables and output boilerplate
5277
StringStack inputDataStack = data.stack;
@@ -61,14 +86,14 @@ public static void main( String[] args ){
6186
// Encode the input file
6287
if(mode.equals("encode")){
6388
try{
64-
String encodedString = tree.encodeString(inputDataStack);
89+
String encodedString = currentTree.encodeString(inputDataStack);
6590
int rawSize = inputString.getBytes("UTF-8").length;
6691
double compressedSize = encodedString.length() / 8.0;
6792

6893
outputWriter.write("Original size: " + inputString.length() + " characters (" + rawSize + " bytes using UTF-8)\n");
6994
outputWriter.write("Encoded size: " + encodedString.length() + " bits (" + compressedSize + " bytes)\n");
7095
outputWriter.write("Compression: " + (compressedSize / rawSize * 100) + "%\n");
71-
outputWriter.write("\nPreorder traversal: " + tree.getPreorderTraversal() + "\n");
96+
outputWriter.write("\nPreorder traversal: " + currentTree.getPreorderTraversal() + "\n");
7297
outputWriter.write("\nEncoded string: " + encodedString + "\n\n\n");
7398
} catch (EncodingException except){
7499
outputWriter.write("Error during encoding: " + except.getMessage() + "\n");
@@ -77,13 +102,13 @@ public static void main( String[] args ){
77102
// Or decode the input file
78103
else{
79104
try{
80-
String decodedString = tree.decode(inputDataStack);
105+
String decodedString = currentTree.decode(inputDataStack);
81106
int rawSize = decodedString.getBytes("UTF-8").length;
82107
double compressedSize = inputString.length() / 8.0;
83108
outputWriter.write("Encoded size: " + inputString.length() + " bits (" + compressedSize + " bytes)\n");
84109
outputWriter.write("Decoded size: " + decodedString.length() + " characters (" + rawSize + " bytes)\n");
85110
outputWriter.write("Compression: " + (compressedSize / rawSize * 100) + "%\n");
86-
outputWriter.write("\nPreorder traversal: " + tree.getPreorderTraversal() + "\n");
111+
outputWriter.write("\nPreorder traversal: " + currentTree.getPreorderTraversal() + "\n");
87112
outputWriter.write("\nDecoded string: " + decodedString +"\n\n\n");
88113
} catch (EncodingException except){
89114
outputWriter.write("Error during decoding: " + except.getMessage() + "\n");

Lab_3/PriorityQueue.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ class PriorityQueue
2222
** the arithmetic methods of calculating parents and children do not work
2323
** with an input index of 0.
2424
**
25+
** In additio, I've added 20 extra spaces into the heap for any enhancement
26+
** characters added that need to be added to the PriorityQueue
27+
**
2528
** @param maxQueueSize Integer specifying the maximum number of items in the priority queue.
2629
**/
27-
heap = new HuffmanNode[maxQueueSize + 1];
30+
heap = new HuffmanNode[maxQueueSize + 1 + 20];
2831
heapSize = 0;
2932
}
3033

0 commit comments

Comments
 (0)