Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Huffman{
  4.  
  5. public static void main(String[] args){
  6. Scanner in = new Scanner(System.in);
  7.  
  8. System.out.print("Enter your sentence: ");
  9. String sentence = in.nextLine();
  10. String binaryString=""; //this stores the string of binary code
  11.  
  12.  
  13. for(int i=0; i < sentence.length(); i++){ //go through the sentence
  14. int decimalValue = (int)sentence.charAt(i); //convert to decimal
  15. String binaryValue = Integer.toBinaryString(decimalValue); //convert to binary
  16. for(int j=7;j>binaryValue.length();j--){
  17. binaryString+="0"; //this loop adds in those pesky leading zeroes
  18. }
  19. binaryString += binaryValue+" "; //add to the string of binary
  20. }
  21. System.out.println(binaryString); //print out the binary
  22.  
  23.  
  24. int[] array = new int[256]; //an array to store all the frequencies
  25.  
  26. for(int i=0; i < sentence.length(); i++){ //go through the sentence
  27. array[(int)sentence.charAt(i)]++; //increment the appropriate frequencies
  28.  
  29. }
  30.  
  31. PriorityQueue < Tree > PQ = new PriorityQueue < Tree >() ; //make a priority queue to hold the forest of trees
  32.  
  33.  
  34. for(int i=0; i<array.length; i++){ //go through frequency array
  35. if(array[i]>0){ //print out non-zero frequencies - cast to a char
  36. System.out.println("'"+(char)i+"' appeared "+array[i]+((array[i] == 1) ? " time" : " times"));
  37.  
  38. Tree myTree = new Tree(); //create a new Tree
  39. myTree.frequency = array[i]; //set the cumulative frequency of that Tree
  40. myTree.root=new Node(); //insert the letter as the root node
  41. myTree.root.letter = (char)i;
  42. PQ.add(myTree); //add the Tree into the PQ
  43.  
  44. }
  45. }
  46.  
  47.  
  48. while(PQ.size()>1){ //while there are two or more Trees left in the forest
  49.  
  50. Tree firstTree = PQ.poll(); //get the two trees
  51. Tree secondTree = PQ.poll();
  52. Tree comboTree = new Tree(); //combine them into a new tree
  53. comboTree.frequency=firstTree.frequency+secondTree.frequency; //add the cumulative frequency of both trees
  54. comboTree.root=new Node(); //insert a default root node (or else you get a null pointer exception)
  55. comboTree.root.leftChild=firstTree.root; //the two trees are the left and right children of the combo tree
  56. comboTree.root.rightChild=secondTree.root;
  57. PQ.add(comboTree);
  58. }
  59.  
  60. Tree HuffmanTree = PQ.poll(); //now there's only one tree left - get its codes
  61.  
  62.  
  63. int totalLength=0; //keeps track of the length of the new compressed version
  64. String theCode;
  65. for(int i=0; i<sentence.length(); i++){
  66. theCode=HuffmanTree.getCode(sentence.charAt(i));
  67. System.out.print(theCode+" "); //get the code for the letter
  68. totalLength+=theCode.length(); //track the length of the solution
  69. }
  70. //print out all the info
  71. System.out.println("\nCompressed size is "+totalLength+" bits / "+sentence.length()*7+" bits = "+(int)((totalLength*100)/(sentence.length()*7))+" %\n");
  72.  
  73. in.close();
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement