code: def make_weighted_tree(fn, ls, **kwargs):
      """Takes a function/class that takes two positional arguments and a list of
      (weight, argument) tuples and returns a huffman-like weighted tree of
      results/instances.
      """
  
      if not ls:
          raise ValueError("Called make_weighted_tree with empty list")
  
      ls.sort()
      while len(ls) > 1:
          a = ls.pop(0)
          b = ls.pop(0)
          insort(ls, (a[0] + b[0], fn(a[1], b[1])))
      return ls[0][1]
  
file_path: whoosh/src/whoosh/util/__init__.py
imports: from __future__ import with_statement
  import random, sys, time
  from bisect import insort
  from functools import wraps
  from whoosh.compat import xrange
label_desc: Use image node_id 4 for calling a global function with example usage: make_weighted_tree(fn, ls) and returns: ls