Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Jul 17th, 2018 60 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1.    import java.io.*;
  2.    import java.util.*;
  3.    public class HuffmanCoding_3_Lay{
  4.       private static HashMap<String, Integer> frequencyMap = new HashMap<String, Integer>();
  5.       private static TreeMap<String, String> huffMap = new TreeMap<String, String>();
  6.       private static PriorityQueue<HuffNode> nodeQueue = new PriorityQueue<HuffNode>();
  7.       private static HuffNode huffRoot = null;
  8.       public static void main(String[] args) throws IOException{
  9.          Scanner sc = new Scanner(System.in);
  10.          System.out.println("Type in something.");
  11.          String message = sc.nextLine();
  12.          File schemeFile = new File("scheme." + message.toLowerCase()+".txt");
  13.          if(!schemeFile.exists())
  14.             schemeFile.createNewFile();
  15.          System.setOut(new PrintStream(new FileOutputStream(schemeFile)));
  16.          huffRoot = makeTree(makeQueue(calcFrequency(message)));
  17.          printScheme(huffRoot, message);
  18.          File messageFile = new File("message."+message.toLowerCase()+".txt");
  19.          if(!messageFile.exists())
  20.             messageFile.createNewFile();
  21.          System.setOut(new PrintStream(new FileOutputStream(messageFile)));
  22.          System.out.println(huffmanCode(message));
  23.       }
  24.       public static void printScheme(HuffNode root, String s){
  25.          HuffNode temp = root;
  26.          printHelper(temp, huffMap, "");
  27.          for(String str : huffMap.keySet())
  28.             System.out.println(str + ":" + huffMap.get(str));
  29.       }
  30.       private static void printHelper(HuffNode temp, TreeMap<String, String> map, String s){
  31.          if(temp == null)
  32.             return;
  33.          else{
  34.             if(!temp.getLetter().equals("*"))
  35.                map.put(temp.getLetter(), s);
  36.             printHelper(temp.getLeft(), map, s+"0");
  37.             printHelper(temp.getRight(), map, s+"1");
  38.          }
  39.       }
  40.       public static HashMap<String, Integer> calcFrequency(String s){
  41.          String letter;
  42.          for(int x = 0; x < s.length(); x++){
  43.             letter = s.substring(x,x+1);
  44.             if(!frequencyMap.containsKey(letter))
  45.                frequencyMap.put(letter, 1);
  46.             else
  47.                frequencyMap.put(letter, frequencyMap.get(letter)+1);
  48.          }
  49.          return frequencyMap;
  50.       }
  51.       public static PriorityQueue<HuffNode> makeQueue(HashMap<String, Integer> map){
  52.          Iterator<String> itr = map.keySet().iterator();
  53.          String current = "";
  54.          while(itr.hasNext()){
  55.             current = itr.next();
  56.             nodeQueue.add(new HuffNode(current, map.get(current), null, null));
  57.          }
  58.          return nodeQueue;
  59.       }
  60.       public static HuffNode makeTree(PriorityQueue<HuffNode> q){
  61.          HuffNode temp1;
  62.          HuffNode temp2;
  63.          int sum;
  64.          while(q.size() > 1){
  65.             temp1 = q.remove();
  66.             temp2 = q.remove();
  67.             int sums = temp1.getFrequency() + temp2.getFrequency();
  68.             q.add(new HuffNode("*", sums, temp1, temp2));
  69.          }
  70.          huffRoot = q.remove();
  71.          return huffRoot;
  72.       }
  73.       public static String huffmanCode(String s){
  74.          String str = "";
  75.          for(int x = 0; x < s.length(); x++)
  76.             str += huffMap.get(s.substring(x, x+1));
  77.          return str;
  78.       }
  79.       static class HuffNode implements Comparable<HuffNode>{
  80.          private int myFrequency;
  81.          private HuffNode myLeft;
  82.          private HuffNode myRight;
  83.          private String myLetter;
  84.          public HuffNode(String letter, int frequency, HuffNode left, HuffNode right){
  85.             myFrequency = frequency;
  86.             myLeft = left;
  87.             myRight = right;
  88.             myLetter = letter;
  89.          }
  90.          public int getFrequency(){
  91.             return myFrequency;
  92.          }
  93.          public HuffNode getLeft(){
  94.             return myLeft;
  95.          }
  96.          public HuffNode getRight(){
  97.             return myRight;
  98.          }
  99.          public String getLetter(){
  100.             return myLetter;
  101.          }
  102.          public void setFrequency(int frequency){
  103.             myFrequency = frequency;
  104.          }
  105.          public void setLeft(HuffNode left){
  106.             myLeft = left;
  107.          }
  108.          public void setRight(HuffNode right){
  109.             myRight = right;
  110.          }
  111.          public void setLetter(String letter){
  112.             myLetter = letter;
  113.          }
  114.          public int compareTo(HuffNode h){
  115.             if(myFrequency > h.getFrequency())
  116.                return 1;
  117.             if(myFrequency < h.getFrequency())
  118.                return -1;
  119.             else
  120.                return 0;
  121.          }
  122.       }
  123.    }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top