Question

In: Computer Science

Create a Huffman Tree and generate the codes for each character of the following input: create...

  1. Create a Huffman Tree and generate the codes for each character of the following input:

create a huffman tree

For consistency:

  1. If same frequency – put in priority queue alphabetically; put space before other characters of the same frequency

  2. Add subtrees to end of group with same priority

  3. Lower number has higher priority (goes to front)

Solutions

Expert Solution

import java.util.*;
class HN { // node class is the basic structure of each node present in the Huffman - tree.
int d;
char c;
HN l;
HN r;
}
  

class MyComparator implements Comparator<HN> { // comparator helps to compare the node
public int compare(HN x, HN y)
{
  
return x.d - y.d;
}
}
  
public class Main {
public static void pCode(HN r1, String s)
{
if (r1.l == null && r1.r == null && Character.isLetter(r1.c)) { // base case; if the l and r are null then its a leaf node and we print the code s generated by traversing the tree.
System.out.println(r1.c + ":" + s);
return;
}
pCode(r1.l, s + "0"); // if we go to l then add "0" to the code.
pCode(r1.r, s + "1"); // if we go to the r add"1" to the code.
}
  
  
public static void main(String[] args) // main function
{
  
Scanner s = new Scanner(System.in);
  
  
int n = 7; // number of characters.
char[] cArray = { 'm', 'i', 's', 'p','r','v','e'};
int[] cFreq = { 1, 5, 4, 2, 2, 1, 1 };
PriorityQueue<HN> q = new PriorityQueue<HN>(n, new MyComparator()); // creating a priority queue q.
  
for (int i = 0; i < n; i++) {
HN hn = new HN(); // creating a Huffman node object and add it to the priority queue.
hn.c = cArray[i];
hn.d = cFreq[i];
hn.l = null;
hn.r = null;
q.add(hn);// add functions adds the huffman node to the queue.
}
HN r1 = null; // create a r1 node
while (q.size() > 1) {
HN x = q.peek(); // first min extract.
q.poll();
HN y = q.peek(); // second min extarct.
q.poll();
HN f = new HN(); // new node f which is equal
f.d = x.d + y.d; // to the sum of the frequency of the two nodes assigning values to the f node.
f.c = '-';
f.l = x; // first extracted node as l child.
f.r = y; // second extracted node as the r child.
r1 = f; // marking the f node as the r1 node.
q.add(f); // add this node to the priority-queue.
}
pCode(r1, ""); // print the codes by traversing the tree
}
}

If you found this answer helpful please give a thumbs up.


Related Solutions

Must a Huffman tree be a two-tree? Explain
Must a Huffman tree be a two-tree? Explain
Create a SINGLE, binary tree using the following three groups of input. After EACH group of...
Create a SINGLE, binary tree using the following three groups of input. After EACH group of input, say whether the tree is full, balanced, and/or complete. PLEASE NOTE you’re only making a SINGLE binary tree in this problem, and answering THREE TIMES. [50,25,30,24,70] [72,71,26,32,22] [56,60,77,54]
C++, Need to create a code that will calculated the statistics for character input? This is...
C++, Need to create a code that will calculated the statistics for character input? This is the prompt for the code. I truly understand nothing of what it's asking. Ask the user for one character (terminated by a carriage return).Using flow control, classify the character into one of these categories: 1)           vowel 2)           consonant 3)           digit (0-9) 4)           other Output the character input, its numeric decimal value, and the classification. Total up the number of each type of character entered....
Say that the input for an Huffman coding contains 256 letters. Each appearing the the same...
Say that the input for an Huffman coding contains 256 letters. Each appearing the the same number of times. (a) If we choose to code all letters by codes of the same length, what is the number of bits required? (b) Is the Huffman code better than the code of the first item?
C# Assignment: Create a DLL that contains a class used to generate QR codes. From a...
C# Assignment: Create a DLL that contains a class used to generate QR codes. From a console application, reference the project that generates the DLL. Allow the user to enter an address and pass it to the method in the separate DLL that will generate the QR code. You only need to allow the user to generate a single QR code at a time (you may close the application once you have generated it).
Huffman codes T/F Questions: Q1 : Let s be the depth of the leaf with the...
Huffman codes T/F Questions: Q1 : Let s be the depth of the leaf with the smallest depth (i.e. closest to the root) in an optimal prefix-free code tree T. Then the highest frequency character has depth s in T. (Assume frequencies are distinct.) T or F Q2 : Let T be an optimal prefix-free code tree, and let v be any non-leaf node in T. If we exchange the left and right subtrees of v (i.e. swap its left...
A hacker has programmed their computer to generate, uniformly at random, an eight-character password, with each...
A hacker has programmed their computer to generate, uniformly at random, an eight-character password, with each character being either one of 26 lower-case letters (a-z), one of 26 upper-case letters (A-Z) or one of 10 integers (0-9). The hacker wants to infiltrate a website that has 2 million users. Assume, for simplicity, that each user is required to use a unique password. What is the expected number of attempts before the hacker successfully generates a user password? What is the...
Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue)...
In C#, write a method that adds a Huffman tree (implemented via a minimum priority queue) to a dictionary table. Include any necessary fields/properties, but keep the functionality within one single method.
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT