def build_expression_tree(Omega, rewrites):
 class Node:
  def ht(self):
   return reduce((lambda x, y: (x + y)), [x.ht() for x in self.before], 1)
 nodes = {}
 for (expr, v) in Omega:
  n = Node()
  n.before = []
  n.var = v
  n.expr = expr
  nodes[v] = n
 for (_, v) in Omega:
  if (v in rewrites):
   n = nodes[v]
   r = rewrites[v]
   for (_, v2) in Omega:
    if r.has(v2):
     n.before.append(nodes[v2])
 return nodes

def build_expression_tree ( Omega , rewrites ) : class Node : def ht ( self ) : return reduce ( ( lambda x , y : ( x + y ) ) , [ x . ht ( ) for x in self . before ] , 1 ) nodes = { } for ( expr , v ) in Omega : n = Node ( ) n . before = [ ] n . var = v n . expr = expr nodes [ v ] = n for ( _ , v ) in Omega : if ( v in rewrites ) : n = nodes [ v ] r = rewrites [ v ] for ( _ , v2 ) in Omega : if r . has ( v2 ) : n . before . append ( nodes [ v2 ] ) return nodes
helper function for rewrite .