import java.io.*;

public class HTProject {
  // implement all static methods required by main
  public static void main (String[] args) throws FileNotFoundException,
                           EOFException, IOException {
          HTableElement[] ht = new HTableElement[256];
          for(int i = 0; i < 256; i++) ht[i] = new HTableElement();

          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          if(args.length == 0) System.out.println("input a sequence of lines; 
                                                   end with an empty line.");
     
          System.out.print("building the character-count table.....");
          StringBuffer text = buildTable(in, ht); /* read input text, build
                                                     table of frequency counts
                                                     indexed by character */
          System.out.println("done.");

          int text_length = text.length();
          System.out.print("initializing the priority queue....");
          PQ pq = startUpHT(ht);  /* populate the priority queue with
                                     character-frequency pairs */
          System.out.println("done.");
 
          System.out.print("building the Huffman Tree......");
          HT huff = buildHT(pq);   // build the HT from the PQ
          System.out.println("done.");

          System.out.print("writing HT codes to table....");
          huff.writeCodes(ht);           
          /* traverse the tree writing HTcodes for each character (leaf) 
             to the frequency-count table */

          System.out.println("done.");

          StringBuffer codetext = new StringBuffer(1000);
          // convert text to codetext, using the HTcodes in the table
 
          System.out.println("\ntext file is : " + text + "\n");
          System.out.println("codetext file is : " + codetext + "\n");

          System.out.println("decoding coded text, writing to output:\n");
          decode(codetext, huff);  // decode handles the output

          double compress = ((float) codetext.length()/((float)text.length())/
                             8.0);
          System.out.println("\ncompression ratio = " + compress);

     }
}