lambda - Getting list of all paths which lead to leaf in Python -


i have binary tree, each node stores piece of data (self.pred). want write function each leaf in tree, returns list of data (which lambda function) in nodes visited reach leaf.

for example, want function called on tree:

        / \  b    c / \  / \ 1  2 3  4 

to return:

returned_list = [ [a, b, 1] [a, b, 2] [a, c, 3] [a, c, 4] ] 

to make thinks more complicated, following right branch reach piece of data must return inverse of lambda function stored node's data.

for example, a' not(a), following list must returned:

returned_list = [ [a, b, 1] [a, b', 2] [a', c, 3] [a', c', 4] ] 

here have far:

class node:     def __init__(self, pred):         self.parent = none         self.left = none         self.right = none         self.pred = pred  def build_tree(pred_choices, k):     if k == 0:         return node(get_pred())     else:         node = node(get_pred())         node.left = build_tree(pred_choices, k-1)         node.right = build_tree(pred_choices, k-1)         return node  def get_paths(root, cur_path, all_paths):     if (root.left == none , root.right == none): # if leaf, append current path         all_paths.append(cur_path)     else:         get_paths(root.left, cur_path.append(root.pred), all_paths)         get_paths(root.right, cur_path.append(lambda x: not(root.pred(x)), all_paths)     return all_paths  def auto_tree_build(pred_choices, k):      root = build_tree(pred_choices, k)     all_paths = get_paths(root, [], [])     return all_paths 

but above code doesn't work, , not understand output. can me make above code execute described behavior?

i use recursion.

i changed node class bit, can design long each node stores left , right children.

from copy import copy collections import namedtuple   # nodepoint, distinguish between , a' np = namedtuple('np', ['node', 'right'])   class node(object):     def __init__(self, name, left=none, right=none):         self.name = name         self.left = left         self.right = right   d = node('1') e = node('2') f = node('3') g = node('4') b = node('b', d, e) c = node('c', f, g) = node('a', b, c)   def get_routes(node, route=none):     route = route or []     # if node has children, clone route, can process     # both right , left child separately.     if node.right:         right_route = copy(route)      # process main (left) route.  pass route on left      # child, may return multiple routes.     route.append(np(node, false))     routes = get_routes(node.left, route) if node.left else [route]      # if there right child, process well.  add route     # results.  note np.right set true, indicate right path.     if node.right:         right_route.append(np(node, true))         right_routes = get_routes(node.right, right_route)         routes.extend(right_routes)      # pass results     return routes   routes = get_routes(a)  # print out results. route in routes:     names = []     np in route:         name = '{0}{1}'.format(np.node.name, "'" if np.right else '')         names.append(name)     print names  # ['a', 'b', '1'] # ['a', "b'", '2'] # ["a'", 'c', '3'] # ["a'", "c'", '4'] 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -