def create_merge_tree(func, keys, token):
 level = 0
 prev_width = len(keys)
 prev_keys = iter(keys)
 rv = {}
 while (prev_width > 1):
  width = tree_width(prev_width)
  groups = tree_groups(prev_width, width)
  keys = [(token, level, i) for i in range(width)]
  rv.update(((key, (func, list(take(num, prev_keys)))) for (num, key) in zip(groups, keys)))
  prev_width = width
  prev_keys = iter(keys)
  level += 1
 return rv

def create_merge_tree ( func , keys , token ) : level = 0 prev_width = len ( keys ) prev_keys = iter ( keys ) rv = { } while ( prev_width > 1 ) : width = tree_width ( prev_width ) groups = tree_groups ( prev_width , width ) keys = [ ( token , level , i ) for i in range ( width ) ] rv . update ( ( ( key , ( func , list ( take ( num , prev_keys ) ) ) ) for ( num , key ) in zip ( groups , keys ) ) ) prev_width = width prev_keys = iter ( keys ) level += 1 return rv
create a task tree that merges all the keys with a reduction function .