Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.text.*;
- import java.math.*;
- import java.util.regex.*;
- public class Solution {
- public static void main(String args[] ) throws Exception {
- Scanner in = new Scanner(System.in);
- //System.out.print("Enter your sentence: ");
- String sentence = in.nextLine();
- String binaryString=""; //this stores the string of binary code
- for(int i=0; i < sentence.length(); i++){ //go through the sentence
- int decimalValue = (int)sentence.charAt(i); //convert to decimal
- String binaryValue = Integer.toBinaryString(decimalValue); //convert to binary
- for(int j=7;j>binaryValue.length();j--){
- binaryString+="0"; //this loop adds in those pesky leading zeroes
- }
- binaryString += binaryValue+" "; //add to the string of binary
- }
- //System.out.println(binaryString); //print out the binary
- int[] array = new int[256]; //an array to store all the frequencies
- for(int i=0; i < sentence.length(); i++){ //go through the sentence
- array[(int)sentence.charAt(i)]++; //increment the appropriate frequencies
- }
- PriorityQueue < Tree > PQ = new PriorityQueue < Tree >() ; //make a priority queue to hold the forest of trees
- for(int i=0; i<array.length; i++){ //go through frequency array
- if(array[i]>0){ //print out non-zero frequencies - cast to a char
- //System.out.println("'"+(char)i+"' appeared "+array[i]+((array[i] == 1) ? " time" : " times"));
- //FILL THIS IN:
- Tree Tr = new Tree();
- Tr.root = new Node();
- Tr.root.letter = (char)i;
- Tr.frequency = array[i];
- Tr.alphabet=i;
- PQ.add(Tr);
- }
- }
- //MAKE THE FOREST OF TREES AND ADD THEM TO THE PQ
- //create a new Tree
- //set the cumulative frequency of that Tree
- //insert the letter as the root node
- //add the Tree into the PQ
- while(PQ.size()>1){ //while there are two or more Trees left in the forest
- //FILL THIS IN:
- Tree tr1=PQ.poll();
- Tree tr2=PQ.poll();
- Tree combo= new Tree();
- combo.frequency=tr1.frequency+tr2.frequency;
- combo.root=new Node();
- combo.root.leftChild=tr1.root;
- combo.root.rightChild=tr2.root;
- combo.alphabet=Math.min(tr1.alphabet,tr2.alphabet);
- PQ.add(combo);
- //IMPLEMENT THE HUFFMAN ALGORITHM
- //when you're making the new combined tree, don't forget to assign a default root node (or else you'll get a null pointer exception)
- //if you like, to check if everything is working so far, try printing out the letter of the roots of the two trees you're combining
- }
- Tree HuffmanTree = PQ.poll(); //now there's only one tree left - get its codes
- //FILL THIS IN:
- for(int j=0; j<sentence.length(); j++)
- {
- System.out.print(HuffmanTree.getCode(sentence.charAt(j)));
- }
- }
- //get all the codes for the letters and print them out
- //call the getCode() method on the HuffmanTree Tree object for each letter in the sentence
- //print out all the info
- }
- class Node
- {
- public char letter='@'; //stores letter
- public Node leftChild; // this node's left child
- public Node rightChild; // this node's right child
- } // end class Node
- ////////////////////////////////////////////////////////////////
- class Tree implements Comparable<Tree>
- {
- public Node root; // first node of tree
- public int frequency=0;
- public int alphabet=0;
- // -------------------------------------------------------------
- public Tree() // constructor
- { root = null; } // no nodes in tree yet
- // -------------------------------------------------------------
- //the PriorityQueue needs to be able to somehow rank the objects in it
- //thus, the objects in the PriorityQueue must implement an interface called Comparable
- //the interface requires you to write a compareTo() method so here it is:
- public int compareTo(Tree object){ //
- if(frequency-object.frequency>0){ //compare the cumulative frequencies of the tree
- return 1;
- }else if(frequency-object.frequency<0){
- return -1; //return 1 or -1 depending on whether these frequencies are bigger or smaller
- }else if(alphabet-object.alphabet<0)
- {
- return -1;
- }
- else if(alphabet-object.alphabet>0)
- {
- return 1;
- }
- else{
- return 0;
- }
- }
- // -------------------------------------------------------------
- String path="error"; //this variable will track the path to the letter we're looking for
- public String getCode(char letter){ //we want the code for this letter
- inOrder(root, letter, ""); //call an inOrder traversal, starting at the root, looking for this letter
- return path; //return the path that results
- }
- // -------------------------------------------------------------
- private void inOrder(Node localRoot, char letter, String path){ //the path variable tracks the current search path
- if(localRoot != null){ //if root is null we've gone off the edge of the tree - back up
- if(localRoot.letter==letter){
- this.path=path; //if we've found the letter, note the path - final path = current search path
- }else{
- inOrder(localRoot.leftChild, letter, path+"0"); //go left and add "0" to the current search path
- inOrder(localRoot.rightChild, letter, path+"1"); //go right and add "1" to the current search path
- }
- }
- return; //quit searching this branch of the tree
- }
- // -------------------------------------------------------------
- } // end class Tree
- ////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment