python - Creating formula based on binary tree leaves -
i have list of points length n (in below example n = 6) after have made other points based on these default point example point 7 made "anding" point 5 , point 4 , on question based on data structure have, how can retrieve chain of formula? example point 10 (recursive or non recursive) how can point coming?.
if want know how point 10 made must return this:
(((5 & 4) | 3) | 2) & (5 & 4) 
you need representation of you've got before start. suggest sth this:
points = [ none, # dummy value fill index 0 (we want start @ 1) none, none, none, none, none, none, # first 6 atoms (5, 4, '&'), # point 7 (7, 3, '|'), # point 8 (8, 2, '|'), # point 9 (9, 7, '&') ] # point 10 then creating formula string recursive:
def formula(points, n): if points[n] none: return str(n) a, b, operator = points[n] return '(%s %s %s)' % (formula(points, a), operator, formula(points, b)) print formula(points, 10) this print
((((5 & 4) | 3) | 2) & (5 & 4)) to points used in formula set, use this:
def used(points, n): if points[n] none: return { n } a, b, operator = points[n] return used(points, a) | used(points, b) print used(points, 10) will print:
set([2, 3, 4, 5])
Comments
Post a Comment