Before running the below code, please create a file stringData.txt with a single line of data to encode in Huffman Encoding.
Java Code:
import java.util.PriorityQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
//Tree Node Structure
class TreeNode {
int data;
char c;
TreeNode left;
TreeNode right;
}
//MyComparator class as an Comparator Operator on Priority Queue
class MyComparator implements Comparator<TreeNode> {
public int compare(TreeNode x, TreeNode y)
{
return x.data - y.data;
}
}
public class HuffmanEncoding {
//load_data to read the line from the file
public static String load_data(String file_name) throws IOException
{
File file=new File(file_name);
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
fr.close();
return line;
}
//Computing the Symbol Frequency Distribution
public static Map<Character, Integer> compute_fd(String content)
{
Map<Character, Integer> symbol_frequency_distribution_map=new HashMap<Character, Integer>();
for(int i=0;i<content.length();i++)
{
char c=content.charAt(i);
if(symbol_frequency_distribution_map.containsKey(c))
{
symbol_frequency_distribution_map.put(c, symbol_frequency_distribution_map.get(c)+1);
}
else
{
symbol_frequency_distribution_map.put(c,1);
}
}
return symbol_frequency_distribution_map;
}
//Forming HuffMan Tree
public static TreeNode huffman_tree(Map<Character,Integer> symbol_frequency_distribution_map)
{
TreeNode root = null;
int size = symbol_frequency_distribution_map.size();
// Min priority queue for min-heap Creation.
PriorityQueue<TreeNode> q = new PriorityQueue<TreeNode>(size, new MyComparator());
//Adding elements to Min priority queue
for (Map.Entry mapElement : symbol_frequency_distribution_map.entrySet())
{
TreeNode tn = new TreeNode();
tn.c = (char)mapElement.getKey();
tn.data = (int)mapElement.getValue();
tn.left = null;
tn.right = null;
q.add(tn);
}
// Forming the Min Heap Tree
while (q.size() > 1) {
TreeNode x = q.peek();
q.poll();
TreeNode y = q.peek();
q.poll();
TreeNode intermediate = new TreeNode();
intermediate.data = x.data + y.data;
intermediate.c = '\0';
intermediate.left = x;
intermediate.right = y;
root = intermediate;
q.add(intermediate);
}
return root;
}
//Getting Huffman Codes
public static Map<Character, String> huffman_code(TreeNode root)
{
Map<Character, String> symbol_huffman_code_map=new HashMap<Character, String>();
return getHuffManCodes(root,"",symbol_huffman_code_map);
}
//Used by Huffman Codes function for recursively getting the codes and updating in symbol_huffman_code_map
public static Map<Character, String> getHuffManCodes(TreeNode root, String s,Map<Character, String> symbol_huffman_code_map)
{
if (root.left == null && root.right == null)
{
symbol_huffman_code_map.put(root.c, s);
return symbol_huffman_code_map;
}
getHuffManCodes(root.left, s + "0",symbol_huffman_code_map);
getHuffManCodes(root.right, s + "1",symbol_huffman_code_map);
return symbol_huffman_code_map;
}
//Encoding the String
public static String encode(Map<Character, String> huffman_code_map, String content)
{
String encoded_string = "";
for(int i=0;i<content.length();i++)
{
encoded_string=encoded_string+huffman_code_map.get(content.charAt(i));
}
return encoded_string;
}
//Printing Output
public static void process_results(Map<Character, Integer> symbol_frequency_distribution_map,String content,String encoded_string)
{
//For Sorting the symbol_frequency_distribution_map in decending Order.
LinkedList<Entry<Character, Integer>> ele_list =new LinkedList<Map.Entry<Character, Integer> >(symbol_frequency_distribution_map.entrySet());
Collections.sort(ele_list, new Comparator<Map.Entry<Character, Integer> >() {
public int compare(Map.Entry<Character, Integer> o1,
Map.Entry<Character, Integer> o2)
{
return (o2.getValue()).compareTo(o1.getValue());
}
});
HashMap<Character, Integer> descending_order_symbol_frequency_distribution_map = new LinkedHashMap<Character, Integer>();
for (Map.Entry<Character, Integer> m : ele_list) {
descending_order_symbol_frequency_distribution_map.put(m.getKey(), m.getValue());
}
System.out.println("Frequency distribution table, in non-ascending order by frequency.");
for (Map.Entry mapElement : descending_order_symbol_frequency_distribution_map.entrySet())
{
System.out.println("\""+(char)mapElement.getKey()+"\" : "+(int)mapElement.getValue());
}
System.out.println(content);
System.out.println(encoded_string);
//Multiply actual string length with 8 as it takes 8 bits for each character
int original_bytes=content.length()*8;
int encoded_bytes=encoded_string.length();
System.out.println("The amount of bytes needed to store the encoded text:"+encoded_bytes);
float saving_percentage=(original_bytes-encoded_bytes)*100/((float)original_bytes);
System.out.println("Saving percentage compared to the amount of bytes needed for the original text:"+saving_percentage+"%");
}
// Driver Function
public static void main(String[] args) throws IOException
{
String content=load_data("stringData.txt");
Map<Character, Integer> symbol_frequency_distribution_map=compute_fd(content);
TreeNode root=huffman_tree(symbol_frequency_distribution_map);
Map<Character, String> huffman_code_map=huffman_code(root);
String encoded_string=encode(huffman_code_map,content);
process_results(symbol_frequency_distribution_map,content,encoded_string);
}
}
Output:
*Java* You will write a program to do the following: 1. Read an input file with...
For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...
. Huffman Encoding (a.) (6 points) Suppose a certain file contains only the following letters with the corresponding frequencies 1 AİB 73 9 30 44 130 28 16 In a fixed-length encoding scheme, cach character is given a binary representation with the same number of bits. What is the minimum number of bits required to represent each letter of this file under fixed-length encoding scheme? Describe how to encode all seven letters in this file using the number of bits...
JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...
(b.) Huffman code is a way to encode information using variable-length binary strings to represent symbols depending on the frequency of each individual letter. Specifically, letters that appear more frequently can be encoded into strings of shorter lengths, while rarer letters can be turned into longer binary strings. On average, Huffman code is a more efficient way to encode a message as the number of bits in the output string will be shorter than if a fixed-length code was used....
Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...
cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...
Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...
4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...
You will construct a Huffman tree based on the given frequencies of 26 English alphabets in upper case plus the space character. An internal tree node class in HuffmanTree with necessary information is required. • You will not randomly switch left and right children when merger two trees. Instead, you will build a right-heavy tree according to the following strategies to select the right child. (1) The tree that is taller will be the right child, i.e., the height is...
Write a C++ Program to do following: 1. Read every character from file char.txt using "get" and write it to a file char.bak using "put" 2. Read every second character from file char.txt using "get" and write it to a file charalternate.txt using "put" and "ignore" 3. Take input from user for offset and number of characters. For example if user enters 5 for offset and 10 for number of characters, then your program should copy 10 bytes from file...