function build_tree(s, desc)
	//    deflate_state *s;
	//    tree_desc *desc; /* the tree descriptor */
	{
	  var tree     = desc.dyn_tree;
	  var stree    = desc.stat_desc.static_tree;
	  var has_stree = desc.stat_desc.has_stree;
	  var elems    = desc.stat_desc.elems;
	  var n, m;          /* iterate over heap elements */
	  var max_code = -1; /* largest code with non zero frequency */
	  var node;          /* new node being created */
	
	  /* Construct the initial heap, with least frequent element in
	   * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
	   * heap[0] is not used.
	   */
	  s.heap_len = 0;
	  s.heap_max = HEAP_SIZE;
	
	  for (n = 0; n < elems; n++) {
	    if (tree[n * 2]/*.Freq*/ !== 0) {
	      s.heap[++s.heap_len] = max_code = n;
	      s.depth[n] = 0;
	
	    } else {
	      tree[n*2 + 1]/*.Len*/ = 0;
	    }
	  }
	
	  /* The pkzip format requires that at least one distance code exists,
	   * and that at least one bit should be sent even if there is only one
	   * possible code. So to avoid special checks later on we force at least
	   * two codes of non zero frequency.
	   */
	  while (s.heap_len < 2) {
	    node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
	    tree[node * 2]/*.Freq*/ = 1;
	    s.depth[node] = 0;
	    s.opt_len--;
	
	    if (has_stree) {
	      s.static_len -= stree[node*2 + 1]/*.Len*/;
	    }
	    /* node is 0 or 1 so it does not have extra bits */
	  }
	  desc.max_code = max_code;
	
	  /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
	   * establish sub-heaps of increasing lengths:
	   */
	  for (n = (s.heap_len >> 1/*int /2*/); n >= 1; n--) { pqdownheap(s, tree, n); }
	
	  /* Construct the Huffman tree by repeatedly combining the least two
	   * frequent nodes.
	   */
	  node = elems;              /* next internal node of the tree */
	  do {
	    //pqremove(s, tree, n);  /* n = node of least frequency */
	    /*** pqremove ***/
	    n = s.heap[1/*SMALLEST*/];
	    s.heap[1/*SMALLEST*/] = s.heap[s.heap_len--];
	    pqdownheap(s, tree, 1/*SMALLEST*/);
	    /***/
	
	    m = s.heap[1/*SMALLEST*/]; /* m = node of next least frequency */
	
	    s.heap[--s.heap_max] = n; /* keep the nodes sorted by frequency */
	    s.heap[--s.heap_max] = m;
	
	    /* Create a new node father of n and m */
	    tree[node * 2]/*.Freq*/ = tree[n * 2]/*.Freq*/ + tree[m * 2]/*.Freq*/;
	    s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
	    tree[n*2 + 1]/*.Dad*/ = tree[m*2 + 1]/*.Dad*/ = node;
	
	    /* and insert the new node in the heap */
	    s.heap[1/*SMALLEST*/] = node++;
	    pqdownheap(s, tree, 1/*SMALLEST*/);
	
	  } while (s.heap_len >= 2);
	
	  s.heap[--s.heap_max] = s.heap[1/*SMALLEST*/];
	
	  /* At this point, the fields freq and dad are set. We can now
	   * generate the bit lengths.
	   */
	  gen_bitlen(s, desc);
	
	  /* The field len is now set, we can generate the bit codes */
	  gen_codes(tree, max_code, s.bl_count);
	}
/* ===========================================================================
	 * Construct one Huffman tree and assigns the code bit strings and lengths.
	 * Update the total bit length for the current block.
	 * IN assertion: the field freq is set for all tree elements.
	 * OUT assertions: the fields len and code are set to the optimal bit length
	 *     and corresponding code. The length opt_len is updated; static_len is
	 *     also updated if stree is not null. The field max_code is set.
	 */
Construct one Huffman tree and assigns the code bit strings and lengths.
Update the total bit length for the current block.
IN assertion: the field freq is set for all tree elements.
OUT assertions: the fields len and code are set to the optimal bit length
and corresponding code. The length opt_len is updated; static_len is
also updated if stree is not null. The field max_code is set.
Construct one Huffman tree and assigns the code bit strings and lengths . Update the total bit length for the current block . IN assertion : the field freq is set for all tree elements . OUT assertions : the fields len and code are set to the optimal bit length and corresponding code . The length opt_len is updated ; static_len is also updated if stree is not null . The field max_code is set .
function build_tree(s, desc)
	{
	  var tree     = desc.dyn_tree;
	  var stree    = desc.stat_desc.static_tree;
	  var has_stree = desc.stat_desc.has_stree;
	  var elems    = desc.stat_desc.elems;
	  var n, m;          
	  var max_code = -1; 
	  var node;          
	  s.heap_len = 0;
	  s.heap_max = HEAP_SIZE;
	  for (n = 0; n < elems; n++) {
	    if (tree[n * 2] !== 0) {
	      s.heap[++s.heap_len] = max_code = n;
	      s.depth[n] = 0;
	    } else {
	      tree[n*2 + 1] = 0;
	    }
	  }
	  while (s.heap_len < 2) {
	    node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
	    tree[node * 2] = 1;
	    s.depth[node] = 0;
	    s.opt_len--;
	    if (has_stree) {
	      s.static_len -= stree[node*2 + 1];
	    }
	  }
	  desc.max_code = max_code;
	  for (n = (s.heap_len >> 1); n >= 1; n--) { pqdownheap(s, tree, n); }
	  node = elems;              
	  do {
	    n = s.heap[1];
	    s.heap[1] = s.heap[s.heap_len--];
	    pqdownheap(s, tree, 1);
	    m = s.heap[1]; 
	    s.heap[--s.heap_max] = n; 
	    s.heap[--s.heap_max] = m;
	    tree[node * 2] = tree[n * 2] + tree[m * 2];
	    s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
	    tree[n*2 + 1] = tree[m*2 + 1] = node;
	    s.heap[1] = node++;
	    pqdownheap(s, tree, 1);
	  } while (s.heap_len >= 2);
	  s.heap[--s.heap_max] = s.heap[1];
	  gen_bitlen(s, desc);
	  gen_codes(tree, max_code, s.bl_count);
	}
function build_tree ( s , desc ) { var tree = desc . dyn_tree ; var stree = desc . stat_desc . static_tree ; var has_stree = desc . stat_desc . has_stree ; var elems = desc . stat_desc . elems ; var n , m ; var max_code = - 1 ; var node ; s . heap_len = 0 ; s . heap_max = HEAP_SIZE ; for ( n = 0 ; n < elems ; n ++ ) { if ( tree [ n * 2 ] !== 0 ) { s . heap [ ++ s . heap_len ] = max_code = n ; s . depth [ n ] = 0 ; } else { tree [ n * 2 + 1 ] = 0 ; } } while ( s . heap_len < 2 ) { node = s . heap [ ++ s . heap_len ] = ( max_code < 2 ? ++ max_code : 0 ) ; tree [ node * 2 ] = 1 ; s . depth [ node ] = 0 ; s . opt_len -- ; if ( has_stree ) { s . static_len -= stree [ node * 2 + 1 ] ; } } desc . max_code = max_code ; for ( n = ( s . heap_len >> 1 ) ; n >= 1 ; n -- ) { pqdownheap ( s , tree , n ) ; } node = elems ; do { n = s . heap [ 1 ] ; s . heap [ 1 ] = s . heap [ s . heap_len -- ] ; pqdownheap ( s , tree , 1 ) ; m = s . heap [ 1 ] ; s . heap [ -- s . heap_max ] = n ; s . heap [ -- s . heap_max ] = m ; tree [ node * 2 ] = tree [ n * 2 ] + tree [ m * 2 ] ; s . depth [ node ] = ( s . depth [ n ] >= s . depth [ m ] ? s . depth [ n ] : s . depth [ m ] ) + 1 ; tree [ n * 2 + 1 ] = tree [ m * 2 + 1 ] = node ; s . heap [ 1 ] = node ++ ; pqdownheap ( s , tree , 1 ) ; } while ( s . heap_len >= 2 ) ; s . heap [ -- s . heap_max ] = s . heap [ 1 ] ; gen_bitlen ( s , desc ) ; gen_codes ( tree , max_code , s . bl_count ) ; }
Construct one Huffman tree and assigns the code bit strings and lengths.
Construct one Huffman tree and assigns the code bit strings and lengths .
//    deflate_state *s; //    tree_desc *desc; /* the tree descriptor */ /* iterate over heap elements */ /* largest code with non zero frequency */ /* new node being created */ /* Construct the initial heap, with least frequent element in
	   * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
	   * heap[0] is not used.
	   */ /*.Freq*/ /*.Len*/ /* The pkzip format requires that at least one distance code exists,
	   * and that at least one bit should be sent even if there is only one
	   * possible code. So to avoid special checks later on we force at least
	   * two codes of non zero frequency.
	   */ /*.Freq*/ /*.Len*/ /* node is 0 or 1 so it does not have extra bits */ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
	   * establish sub-heaps of increasing lengths:
	   */ /*int /2*/ /* Construct the Huffman tree by repeatedly combining the least two
	   * frequent nodes.
	   */ /* next internal node of the tree */ //pqremove(s, tree, n);  /* n = node of least frequency */ /*** pqremove ***/ /*SMALLEST*/ /*SMALLEST*/ /*SMALLEST*/ /***/ /*SMALLEST*/ /* m = node of next least frequency */ /* keep the nodes sorted by frequency */ /* Create a new node father of n and m */ /*.Freq*/ /*.Freq*/ /*.Freq*/ /*.Dad*/ /*.Dad*/ /* and insert the new node in the heap */ /*SMALLEST*/ /*SMALLEST*/ /*SMALLEST*/ /* At this point, the fields freq and dad are set. We can now
	   * generate the bit lengths.
	   */ /* The field len is now set, we can generate the bit codes */