Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Part I need help with: Huffman (int numASCIIchars) Constructor: Creates the MyHashMap object, huffMap, passing in numASCIIchars to its constructor void assignCode (HuffTree.HuffNode root)

JAVA Part I need help with: Huffman (int numASCIIchars) Constructor: Creates the MyHashMap object, huffMap, passing in numASCIIchars to its constructor void assignCode (HuffTree.HuffNode root) Recursive: if roots left child pointer is not null, do each of the following steps: 1. Add 0 to the code in root and set it as the code in roots left child root node 2. Call assignCode with roots left child pointer. 3. Add 1 to the code in root and set it as the code in roots right child root node 4. Call assignCode with roots right child pointer. String compress (String fileStr) Take the incoming fileStr string and compress it using the Huffman code algorithm and return the compressed string: See algorithm below String decompress (String binaryStr) Take the incoming binaryStr and decompress it using the Huffman code algorithm and return the decompressed string: See algorithm below String compress (String fileStr) Algorithm: Uses methods to perform these tasks: Add an EOF marker to the fileStr Take the incoming fileStr string and build a HashMap for the characters and their frequencies in fileStr Build a Huffman tree based on the character frequency Traverse the tree to generate codes for each character Use the codes to produce a binary string from the fileStr Return the binary string for output String decompress (String binaryStr) Algorithm: Rebuild the Huffman tree from the MyHashMap Get root and another temp node for traversing the tree Loop through each binary character in the string Traverse the tree going left with '0' and right with '1' When you find a leaf node add its char to the output string. Check each leaf for EOF char and break when found Return the output string ******************************************************************************************************************************************************** Rest of Assignment: Table 1: Vigenere API Data & Methods Description int numChars Character from the file string. String key Character frequency in file string char[][] vigSquare; Binary character code for compression Vigenere (int numChs, String akey) Constructor: initializes numChars to numChs, key to akey, vigSquare by calling createVigSquare() void createVigSquare () Creates the vigSquare by simply using a nested loop for the row and column: 0 to numChars-1 with the one line given below: vigSquare[row][col] = (char) ((col + row) % numChars); String encrypt(String msg) Return an encrypted msg String decrypt(String msg) Return a decrypted msg int getColIndex(char ch) Returns the column index of ch in row 0 int getRowIndex(char keyCh) Returns the row index of keyCh in col 0 char getPlainTextChar (int rowIndex, char ch) Returns the plaintext character corresponding to ch. First, find the columnIndex of ch using rowIndex and then return the char from the square at row 0 and columnIndex. Table 2: HuffTree API - Implements Comparable Interface HuffNode API - Implements Comparable Interface Class is internal to HuffTree Data & Methods Description HuffTree API: HuffNode root Character from the file string. HuffTree () Constructor: default Set root to null HuffTree (HuffTree t1, HuffTree t2) Constructor: Initializes root to a new HuffNode with new HuffElement Adds in t1 as root left subtree Adds in t2 as root right subtree Updates roots element to contain a sum of the counts in t1.root element and t2.root element HuffTree (HuffElement element) Constructor: Uses the element to construct a tree with only a root node with the element HuffNode getRoot() Returns root. int compareTo (HuffTree ht) Compares the root node of this tree to the root node of ht. HuffNode API: HuffElement element; The node element public HuffNode left; Left child pointer public HuffNode right; Right child pointer HuffNode (HuffElement elem) Constructor: Initializes element to elem Set left to null Set right to null char getChar() Return the character from the element int getCount() Return the character count from the element void setCount(int count) Set the count in the element String getCode() Return the code from the element void setCode(String code) Set the code in the element int compareTo (HuffNode node) Compare this node to node using element. String toString() Return string version of the node using the element toString() Table 3: Huffman API Data & Methods Description MyMap huffMap The MyHashMap used to store the characters, character counts, and code for the characters in the file string. final char EOF = 0x7F End of file char needed for compression Huffman (int numASCIIchars) Constructor: Creates the MyHashMap object, huffMap, passing in numASCIIchars to its constructor void buildCountMap (String fileStr) Populate the huffMap with the Map entries, same as with Lab09. It should be cleared at the top. HuffTree buildHuffmanTree() Return a Huffman tree from the counts in huffMap: See algorithm below void getHuffmanCodes (HuffTree.HuffNode root) Return if root is null; otherwise call assignCode passing in root void assignCode (HuffTree.HuffNode root) Recursive: if roots left child pointer is not null, do each of the following steps: 1. Add 0 to the code in root and set it as the code in roots left child root node 2. Call assignCode with roots left child pointer. 3. Add 1 to the code in root and set it as the code in roots right child root node 4. Call assignCode with roots right child pointer. String buildBinaryString (String fileStr) Builds a new string of ones and zeros by walking through the fileStr string and replacing each character with the code in the HuffElement in huffMap String compress (String fileStr) Take the incoming fileStr string and compress it using the Huffman code algorithm and return the compressed string: See algorithm below String decompress (String binaryStr) Take the incoming binaryStr and decompress it using the Huffman code algorithm and return the decompressed string: See algorithm below HuffTree buildHuffmanTree() Algorithm: Get a Huffman tree from the counts in huffMap: Build forest of HuffTrees from huffMap: Create the heap Retrieve the set of keys Iterate through the keys (use a for each loop): Use the key to retrieve the HuffElement Create a HuffTree with a single root node from the HuffElement Add the HuffTree to the Heap Loop through Heap, removing two lowest character count trees Create a new HuffTree using these two trees Add new tree to the Heap Repeat until only one tree left Return the final tree: Last tree in heap String compress (String fileStr) Algorithm: Uses methods to perform these tasks: Add an EOF marker to the fileStr Take the incoming fileStr string and build a HashMap for the characters and their frequencies in fileStr Build a Huffman tree based on the character frequency Traverse the tree to generate codes for each character Use the codes to produce a binary string from the fileStr Return the binary string for output String decompress (String binaryStr) Algorithm: Rebuild the Huffman tree from the MyHashMap Get root and another temp node for traversing the tree Loop through each binary character in the string Traverse the tree going left with '0' and right with '1' When you find a leaf node add its char to the output string. Check each leaf for EOF char and break when found Return the output string Sample Output (1) Read original file: .\src\FDREconomics.txt (2) Original file size: 1545 (3) Compressed original file (no encryption): .\src\FDREconomicsCompressed.dat (4) Compressed original file: 44.1% (5) Encrypt original file using key: 'a' (6) Compressed encrypted file: .\src\FDREconomicsCompressed0.dat (7) Compressed encrypted file: 44.1% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'ab' (6) Compressed encrypted file: .\src\FDREconomicsCompressed1.dat (7) Compressed encrypted file: 38.9% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'cab' (6) Compressed encrypted file: .\src\FDREconomicsCompressed2.dat (7) Compressed encrypted file: 36.1% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'bacd' (6) Compressed encrypted file: .\src\FDREconomicsCompressed3.dat (7) Compressed encrypted file: 34.2% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'becad' (6) Compressed encrypted file: .\src\FDREconomicsCompressed4.dat (7) Compressed encrypted file: 33.2% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'fabced' (6) Compressed encrypted file: .\src\FDREconomicsCompressed5.dat (7) Compressed encrypted file: 32.2% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'fedgabc' (6) Compressed encrypted file: .\src\FDREconomicsCompressed6.dat (7) Compressed encrypted file: 31.3% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'badcfegh' (6) Compressed encrypted file: .\src\FDREconomicsCompressed7.dat (7) Compressed encrypted file: 30.2% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'diagbecfh' (6) Compressed encrypted file: .\src\FDREconomicsCompressed8.dat (7) Compressed encrypted file: 29.9% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'jabfigched' (6) Compressed encrypted file: .\src\FDREconomicsCompressed9.dat (7) Compressed encrypted file: 28.9% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'kebcidjafgh' (6) Compressed encrypted file: .\src\FDREconomicsCompressed10.dat (7) Compressed encrypted file: 28.5% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'ladbickjefgh' (6) Compressed encrypted file: .\src\FDREconomicsCompressed11.dat (7) Compressed encrypted file: 27.6% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'mighkcabdlefj' (6) Compressed encrypted file: .\src\FDREconomicsCompressed12.dat (7) Compressed encrypted file: 27.0% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original (5) Encrypt original file using key: 'nmigheabcdfjkl' (6) Compressed encrypted file: .\src\FDREconomicsCompressed13.dat (7) Compressed encrypted file: 26.5% (8) Decrypted file: .\src\FDREconomicsDecrypted.txt is the same as original

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions