nodes: 0, module; 1, function_definition; 2, function_name:_treeify; 3, parameters; 4, block; 5, identifier:values; 6, expression_statement; 7, if_statement; 8, expression_statement; 9, comment:# Tree indices work as follows:; 10, comment:# 0 is the root; 11, comment:# 2n+1 is the left child of n; 12, comment:# 2n+2 is the right child of n; 13, comment:# So we now rearrange `values` into that format...; 14, comment:# The first step is to remove the bottom row of leaves, which might not be exactly full; 15, expression_statement; 16, expression_statement; 17, if_statement; 18, comment:# Now `values` is length 2**n - 1, so can be packed efficiently into a tree; 19, comment:# Last row of nodes is indices 0, 2, ..., 2**n - 2; 20, comment:# Second-last row is indices 1, 5, ..., 2**n - 3; 21, comment:# nth-last row is indices (2**n - 1)::(2**(n+1)); 22, expression_statement; 23, expression_statement; 24, expression_statement; 25, while_statement; 26, return_statement; 27, comment:"""Convert the np.ndarray `values` into a complete balanced tree.
  
          Assumes `values` is sorted ascending. Returns a list `t` of the same length in which t[i] >
          t[2i+1] and t[i] < t[2i+2] for all i."""; 28, comparison_operator:len(values) == 1; 29, comment:# this case causes problems later; 30, block; 31, assignment; 32, assignment; 33, assignment; 34, comparison_operator:len_ragged_row > 0; 35, block; 36, assignment; 37, assignment; 38, assignment; 39, comparison_operator:values_start < len(values); 40, block; 41, identifier:tree; 42, call; 43, integer:1; 44, return_statement; 45, identifier:tree; 46, call; 47, identifier:last_full_row; 48, call; 49, identifier:len_ragged_row; 50, binary_operator:len(values) - (2 ** (last_full_row + 1) - 1); 51, identifier:len_ragged_row; 52, integer:0; 53, expression_statement; 54, expression_statement; 55, expression_statement; 56, identifier:values_start; 57, integer:0; 58, identifier:values_space; 59, integer:2; 60, identifier:values_len; 61, binary_operator:2 ** last_full_row; 62, identifier:values_start; 63, call; 64, expression_statement; 65, expression_statement; 66, expression_statement; 67, expression_statement; 68, identifier:len; 69, argument_list; 70, identifier:values; 71, attribute; 72, argument_list; 73, identifier:int; 74, argument_list; 75, call; 76, parenthesized_expression; 77, assignment; 78, assignment; 79, assignment; 80, integer:2; 81, identifier:last_full_row; 82, identifier:len; 83, argument_list; 84, assignment; 85, augmented_assignment; 86, augmented_assignment; 87, assignment; 88, identifier:values; 89, identifier:np; 90, identifier:empty_like; 91, identifier:values; 92, binary_operator:np.log2(len(values) + 1) - 1; 93, identifier:len; 94, argument_list; 95, binary_operator:2 ** (last_full_row + 1) - 1; 96, identifier:bottom_row_ix; 97, subscript; 98, subscript; 99, subscript; 100, identifier:values; 101, call; 102, identifier:values; 103, subscript; 104, subscript; 105, identifier:values_start; 106, call; 107, identifier:values_space; 108, integer:2; 109, identifier:values_len; 110, call; 111, call; 112, integer:1; 113, identifier:values; 114, binary_operator:2 ** (last_full_row + 1); 115, integer:1; 116, attribute; 117, slice; 118, identifier:tree; 119, slice; 120, identifier:values; 121, identifier:bottom_row_ix; 122, attribute; 123, argument_list; 124, identifier:tree; 125, slice; 126, identifier:values; 127, slice; 128, identifier:int; 129, argument_list; 130, identifier:int; 131, argument_list; 132, attribute; 133, argument_list; 134, integer:2; 135, parenthesized_expression; 136, identifier:np; 137, identifier:s_; 138, binary_operator:2 * len_ragged_row; 139, integer:2; 140, unary_operator; 141, identifier:np; 142, identifier:delete; 143, identifier:values; 144, identifier:bottom_row_ix; 145, binary_operator:values_len - 1; 146, binary_operator:2 * values_len - 1; 147, identifier:values_start; 148, identifier:values_space; 149, binary_operator:values_space / 2; 150, binary_operator:values_len / 2; 151, identifier:np; 152, identifier:log2; 153, binary_operator:len(values) + 1; 154, binary_operator:last_full_row + 1; 155, integer:2; 156, identifier:len_ragged_row; 157, identifier:len_ragged_row; 158, identifier:values_len; 159, integer:1; 160, binary_operator:2 * values_len; 161, integer:1; 162, identifier:values_space; 163, integer:2; 164, identifier:values_len; 165, integer:2; 166, call; 167, integer:1; 168, identifier:last_full_row; 169, integer:1; 170, integer:2; 171, identifier:values_len; 172, identifier:len; 173, argument_list; 174, identifier:values
code: def _treeify(values):
          """Convert the np.ndarray `values` into a complete balanced tree.
  
          Assumes `values` is sorted ascending. Returns a list `t` of the same length in which t[i] >
          t[2i+1] and t[i] < t[2i+2] for all i."""
          if len(values) == 1:  # this case causes problems later
              return values
          tree = np.empty_like(values)
          # Tree indices work as follows:
          # 0 is the root
          # 2n+1 is the left child of n
          # 2n+2 is the right child of n
          # So we now rearrange `values` into that format...
  
          # The first step is to remove the bottom row of leaves, which might not be exactly full
          last_full_row = int(np.log2(len(values) + 1) - 1)
          len_ragged_row = len(values) - (2 ** (last_full_row + 1) - 1)
          if len_ragged_row > 0:
              bottom_row_ix = np.s_[: 2 * len_ragged_row : 2]
              tree[-len_ragged_row:] = values[bottom_row_ix]
              values = np.delete(values, bottom_row_ix)
  
          # Now `values` is length 2**n - 1, so can be packed efficiently into a tree
          # Last row of nodes is indices 0, 2, ..., 2**n - 2
          # Second-last row is indices 1, 5, ..., 2**n - 3
          # nth-last row is indices (2**n - 1)::(2**(n+1))
          values_start = 0
          values_space = 2
          values_len = 2 ** last_full_row
          while values_start < len(values):
              tree[values_len - 1 : 2 * values_len - 1] = values[values_start::values_space]
              values_start += int(values_space / 2)
              values_space *= 2
              values_len = int(values_len / 2)
          return tree