private final static void gen_codes(
                        short[] tree, // the tree to decorate
                        int max_code, // largest code with non zero frequency
                        short[] bl_count, // number of codes at each bit length
                        short[] next_code){
    short code = 0;            // running code value
    int bits;                  // bit index
    int n;                     // code index

    // The distribution counts are first used to generate the code values
    // without bit reversal.
    next_code[0]=0;
    for (bits = 1; bits <= MAX_BITS; bits++) {
      next_code[bits] = code = (short)((code + bl_count[bits-1]) << 1);
    }

    // Check that the bit counts in bl_count are consistent. The last code
    // must be all ones.
    //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
    //        "inconsistent bit counts");
    //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));

    for (n = 0;  n <= max_code; n++) {
      int len = tree[n*2+1];
      if (len == 0) continue;
      // Now reverse the bits
      tree[n*2] = (short)(bi_reverse(next_code[len]++, len));
    }
  }