def generate_tree_function(function, path=[]): path_len = len(path) if path_len<self.n: v = Vertex(index=path_len+1) v.low = generate_tree_function(function, path+[False]) v.high = generate_tree_function(function, path+[True]) return v elif path_len==self.n: # reached leafes return Vertex(value=function(*path))
def generate_tree_values(values, level=0): if level<self.n: v = Vertex(index=level+1) v.low = generate_tree_values(values, level+1) v.high = generate_tree_values(values, level+1) return v elif level==self.n: # reached leafes v = Vertex(value=values[self._vcount]) self._vcount = self._vcount + 1 return v
def _apply(v1, v2, f): # Check if v1 and v2 have already been calculated key = str(v1.id) + ' ' + str(v2.id) if key in cache: return cache[key] # Result vertex u = Vertex() # If the vertices are both leafs, # apply the boolean function to them if v1.value!=None and v2.value!=None: u = leafs[f(v1.value, v2.value)] #u = Vertex(value=f(v1.value, v2.value)) cache[key] = u return u # v1.index < v2.index if v1.value==None and (v2.value!=None or v1.index<v2.index): u.index = v1.index u.low = _apply(v1.low, v2, f) u.high = _apply(v1.high, v2, f) # v1.index > v2.index elif v1.value!=None or v1.index>v2.index: u.index = v2.index u.low = _apply(v1, v2.low, f) u.high = _apply(v1, v2.high, f) # v1.index == v2.index else: u.index = v1.index u.low = _apply(v1.low, v2.low, f) u.high = _apply(v1.high, v2.high, f) #u.erase_children_redundancy() cache[key] = u return u