Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.44 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package testing.huffman;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import java.util.Arrays;
  14. import java.util.PriorityQueue;
  15. import java.util.Scanner;
  16. import java.util.Comparator;
  17.  
  18. // node class is the basic structure
  19. // of each node present in the Huffman - tree.
  20. class HuffmanNode {
  21.  
  22.     int data;
  23.     char c;
  24.  
  25.     HuffmanNode left;
  26.     HuffmanNode right;
  27. }
  28.  
  29. // comparator class helps to compare the node
  30. // on the basis of one of its attribute.
  31. // Here we will be compared
  32. // on the basis of data values of the nodes.
  33. class MyComparator implements Comparator<HuffmanNode> {
  34.     public int compare(HuffmanNode x, HuffmanNode y)
  35.     {
  36.  
  37.         return x.data - y.data;
  38.     }
  39. }
  40.  
  41. public class TestingHuffman {
  42.    
  43.     public static int[] freq;
  44.  
  45.     // recursive function to print the
  46.     // huffman-code through the tree traversal.
  47.     // Here s is the huffman - code generated.
  48.     public static void printCode(HuffmanNode root, String s)
  49.     {
  50.  
  51.         // base case; if the left and right are null
  52.         // then its a leaf node and we print
  53.         // the code s generated by traversing the tree.
  54.         if (root.left
  55.                 == null
  56.             && root.right
  57.                    == null
  58.             && Character.isLetter(root.c)) {
  59.  
  60.             // c is the character in the node
  61.             System.out.println(root.c + ":" + s);
  62.  
  63.             return;
  64.         }
  65.  
  66.         // if we go to left then add "0" to the code.
  67.         // if we go to the right add"1" to the code.
  68.  
  69.         // recursive calls for left and
  70.         // right sub-tree of the generated tree.
  71.         printCode(root.left, s + "0");
  72.         printCode(root.right, s + "1");
  73.     }
  74.  
  75.     // main function
  76.     public static void main(String[] args) throws FileNotFoundException, IOException
  77.     {
  78.        
  79.         Scanner s = new Scanner(System.in);
  80.  
  81.         Scanner scan = new Scanner(new FileInputStream("C:\\Users\\saint\\Desktop\\FALL 2019\\CSC 3102 (Adv Data Structures) - Rahul Shah\\Projects\\prog2\\HuffmanCodeTest.txt"));
  82.         //PrintWriter outputFile = new PrintWriter("HuffOutput.txt", "UTF-8");
  83.        
  84.         int n = 0;
  85.         for(int i = 0; i < scan.toString().length(); i++){
  86.             n += i;
  87.         }
  88.         char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '∅'};
  89.         //int[] charFreq = freq;
  90.        
  91.        
  92.  
  93.         // creating a priority queue q.
  94.         // makes a min-priority queue(min-heap).
  95.         PriorityQueue<HuffmanNode> q
  96.             = new PriorityQueue<HuffmanNode>(n, new MyComparator());
  97.  
  98.         for (int i = 0; i < n; i++) {
  99.  
  100.             // creating a Huffman node object
  101.             // and add it to the priority queue.
  102.             HuffmanNode hn = new HuffmanNode();
  103.  
  104.             hn.c = charArray[i];
  105.             hn.data = freq[i];
  106.  
  107.             hn.left = null;
  108.             hn.right = null;
  109.  
  110.             // add functions adds
  111.             // the huffman node to the queue.
  112.             q.add(hn);
  113.         }
  114.  
  115.         // create a root node
  116.         HuffmanNode root = null;
  117.  
  118.         // Here we will extract the two minimum value
  119.         // from the heap each time until
  120.         // its size reduces to 1, extract until
  121.         // all the nodes are extracted.
  122.         while (q.size() > 1) {
  123.  
  124.             // first min extract.
  125.             HuffmanNode x = q.peek();
  126.             q.poll();
  127.  
  128.             // second min extarct.
  129.             HuffmanNode y = q.peek();
  130.             q.poll();
  131.  
  132.             // new node f which is equal
  133.             HuffmanNode f = new HuffmanNode();
  134.  
  135.             // to the sum of the frequency of the two nodes
  136.             // assigning values to the f node.
  137.             f.data = x.data + y.data;
  138.             f.c = '-';
  139.  
  140.             // first extracted node as left child.
  141.             f.left = x;
  142.  
  143.             // second extracted node as the right child.
  144.             f.right = y;
  145.  
  146.             // marking the f node as the root node.
  147.             root = f;
  148.  
  149.             // add this node to the priority-queue.
  150.             q.add(f);
  151.         }
  152.  
  153.         // print the codes by traversing the tree
  154.         printCode(root, "");
  155.     }
  156.    
  157.     public static int[] countFrequency(String filename) throws IOException{
  158.         freq = new int[27];
  159.         BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\saint\\Desktop\\FALL 2019\\CSC 3102 (Adv Data Structures) - Rahul Shah\\Projects\\prog2\\HuffmanCodeTest.txt"));
  160.         String line;
  161.         while((line = in.readLine()) != null){
  162.             line = line.toLowerCase();
  163.                         for(char ch:line.toCharArray()){
  164.                 if(Character.isLetter(ch)){
  165.                     freq[ch - 'a']++;
  166.                 }
  167.             }
  168.             for(char sp:line.toCharArray()){
  169.                 if(sp == ' '){
  170.                     freq[26]++;
  171.                 }
  172.             }
  173.         }
  174.         in.close();
  175.         return freq;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement