Exemplo n.º 1
0
  /**
   * Create a new TreeMaker object.
   *
   * @param stream input stream
   * @throws IOException sth wrong with IO
   * @throws Exception sth wrong otherwise
   */
  public TreeMaker(BitInputStream stream) throws IOException, Exception {

    // Using CharCounter to get necessary values
    cc = new CharCounter();
    streamSize = cc.countAll(stream);
    size = cc.getNonZeroCharCount();

    // Creating HuffTree and HuffBaseNode arrays to store things
    HuffArr = new HuffTree[size + 1];
    newHuffArr = new HuffBaseNode[size + 1];
    int j = 0;

    // Creating individual HuffTree elements
    for (int i = 0; i < cc.getSize(); i++) {
      if (cc.getCount(i) != 0) {
        HuffArr[j] = new HuffTree(i, cc.getCount(i));
        // System.out.println((char)i + ":" + cc.getCount(i));
        j++;
      }
    }

    //  Last HuffArr element is the Pseudo_eof
    HuffArr[size] = new HuffTree(IHuffConstants.PSEUDO_EOF, 1);

    // Creating a minheap out of the HuffArr array
    Heap = new MinHeap(HuffArr, size + 1, 2 * (size + 1));

    // Get root from helper method
    root = buildTree();

    // Initializing index as 0
    index = 0;

    // Go from root to get all of the leaf nodes
    // Put the leaf nodes to newHuffArr
    realTree(root.root());

    // Set codings for nodes
    codings(root.root());
  }