I need a help in making a method that will Input a text that will be converted to Huffman code and Input a Huffman code that will be converted to its text equivalent.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need a help in making a method that will Input a text that will be converted to Huffman code and Input a Huffman code that will be converted to its text equivalent.


You can try this sample Character input: HAAHAABAAACACAAAABABABEBEBBAABEEBBCCCCCAAAAAAACCCDDDDDDAAADDDDDEEEEEEDDADDDADDEEEEEEEECCCEEEEEEEEEEDDEEEEEEEEEEBBEEEEEFFGGHAHHEEHHA

 

Huffman.java

import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Comparator;
import java.util.ArrayList;

class HuffmanNode {
   int data;
   char c;
   HuffmanNode left;
   HuffmanNode right;
}

class MyComparator implements Comparator<HuffmanNode> {
   public int compare(HuffmanNode x, HuffmanNode y) {
       return x.data - y.data;
   }
}

public class Huffman {
   public static void printCode(HuffmanNode root, String s) {
       if (root.left == null && root.right == null && Character.isLetter(root.c)) {
           System.out.println(s + "tttt"+ s.length());
           return;
       }

       printCode(root.left, s + "0");
       printCode(root.right, s + "1");

   }

   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Input a series of characters to be converted into Huffman code");
       System.out.print("Input text: ");
       String str = keyboard.nextLine();

       char[] ch = str.toCharArray();
       ArrayList<Character> characters = new ArrayList<Character>();

       for (int i = 0; i < ch.length; i++) {
           if (!(characters.contains(ch[i]))) {
               characters.add(ch[i]);
           }
       }

       int[] countOfChar = new int[characters.size()];
       for (int x = 0; x < countOfChar.length; x++) {
           countOfChar[x] = 0;
       }

       for (int i = 0; i < characters.size(); i++) {
           char checker = characters.get(i);
           for (int x = 0; x < ch.length; x++) {
               if (checker == ch[x]) {
                   countOfChar[i]++;
               }
           }
       }

       int n = countOfChar.length;
       System.out.println("Characters  Number of occurrence of the character in the text");
       for (int x = 0; x < countOfChar.length; x++) {
           System.out.println(characters.get(x) + "ttt" + countOfChar[x]);
       }
       System.out.println("nHuffman Node  Number of Bits");

       PriorityQueue<HuffmanNode> q = new PriorityQueue<HuffmanNode>(n, new MyComparator());

       for (int i = 0; i < n; i++) {

           HuffmanNode hn = new HuffmanNode();
           hn.c =characters.get(i);
           hn.data=countOfChar[i];
           hn.left = null;
           hn.right = null;

           q.add(hn);
       }

       HuffmanNode root = null;

       while (q.size() > 1) {
           HuffmanNode x = q.peek();

           q.poll();

           HuffmanNode y = q.peek();

           q.poll();

           HuffmanNode f = new HuffmanNode();

           f.data = x.data + y.data;
           f.c = '-';
           f.left = x;
           f.right = y;
           root = f;
           q.add(f);
       }
       printCode(root, "");
   }
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY