I have a binary tree that represents a parsed logical formula. For example, f = a & b & -c | d is represented by a list of lists in prefix notation, where the first element is the operator (unary or binary) and the next elements their arguments:
f = [ |, [&, a, [&, b, [-, c]]], d]
But if you translate (by recursion) to the classical infix notation the result is the same.
f = (((-c & b) & a) | d) = a & b & -c | d
What I'm trying to do is to convert it into an N-ary tree that retains the same information, that is, if you translate it into formula again, the result must be the same. Something like this:
f = {l: [{&: [a,b,{-:[c]}]}, d]}
Which in infixed notation is the following.
f = ((a & b & -c) | d) = a & b & -c | d
I haven't found any library so I tried to do it by myself recursively. However, I have only achieved this code that fails in some cases and it's not very elegant...
def explore_tree(self,tree, last_symbol, new_tree):
if type(tree) != list: # This true means that root is an atom
new_tree[last_symbol].append(tree)
return
root = tree[0]
if is_operator(root):
if root != last_symbol:
branch = {root: []}
new_tree[last_symbol].append(branch)
#This line is to search the index of branch object and expand by them
self.explore_branches(tree, root, new_tree[last_symbol]
[new_tree[last_symbol].index(branch)])
else:
self.explore_branches(tree,root,new_tree)
The functions explore_branches()
call recursively to explore tree from left and right (if exist), and is_operator()
return true if the given string is one logic operator, for example & or |.
Any other idea of how can I do this?
Thanks you in advance.
The only touchy case is the negation. Apart from it, one can simply write your algo or similar such as
from functools import reduce
def op(tree):
return 0 if type(tree)!=list else tree[0]
def bin_to_n(tree):
if op(tree)==0:
return tree
op_tree = tree[0]
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n(node)
if op(node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
Now regarding the negation. The failing case of above algorithm is when flattening -(-(1))
which gives -1
instead of 1
< if op(node) != op_tree
---
> if op(node) != op_tree or op(node)=="-"
meaning that if a "minus" is found, you never "concatenate" it. Thus this lets -(-(1))
as is.
Now we can simplify more but those simplifications could have been done beforehand on the input list. So it "semantically" changes the tree (even though evaluation stays identical).
op_tree = tree[0]
> if op_tree == '-' and op(tree[1]) == '-':
> return bin_to_n2(tree[1][1])
out = [op_tree]
#really invert according to demorgan's law
def bin_to_n3(tree, negate=False):
if op(tree)==0:
return tree
op_tree = tree[0]
if negate:
if op_tree == '-':
#double neg, skip the node
return bin_to_n3(tree[1])
#demorgan
out = [ '+' if op_tree == '*' else '*' ]
for node in tree[1:]:
flat_node = bin_to_n3(node, True)
#notice that since we modify the operators we have
#to take the operator of the resulting tree
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
if op_tree == '-' and op(op_tree)==0:
#do not touch the leaf
return tree
#same code as above, not pun to factorize it
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n3(node)
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
Below some random checks to ensure transformation keeps the tree's value intact
from functools import reduce
def op(tree):
return 0 if type(tree)!=list else tree[0]
def bin_to_n(tree):
if op(tree)==0:
return tree
op_tree = tree[0]
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n(node)
if op(node) != op_tree or op(node)=='-':
out.append(flat_node)
else:
out += flat_node[1:]
return out
def bin_to_n2(tree):
if op(tree)==0:
return tree
op_tree = tree[0]
if op_tree == '-' and op(tree[1]) == '-':
return bin_to_n2(tree[1][1])
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n2(node)
if op(node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
#really invert according to demorgan's law
def bin_to_n3(tree, negate=False):
if op(tree)==0:
return tree
op_tree = tree[0]
if negate:
if op_tree == '-':
#double neg, skip the node
return bin_to_n3(tree[1])
#demorgan
out = [ '+' if op_tree == '*' else '*' ]
for node in tree[1:]:
flat_node = bin_to_n3(node, True)
#notice that since we modify the operators we have
#to take the operator of the resulting tree
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
if op_tree == '-' and op(op_tree)==0:
#do not touch the leaf
return tree
#same code as above, not pun to factorize it
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n3(node)
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
def calc(tree):
if op(tree) == 0:
return tree
s = 0
subtree = tree[1:]
if op(tree)=='+':
s = reduce(lambda x,y: x or calc(y), subtree, False)
elif op(tree) == '-':
s = not calc(subtree[0])
else:
s = reduce(lambda x,y: x and calc(y), subtree, True)
return s
#adaptated from https://stackoverflow.com/questions/6881170/is-there-a-way-to-autogenerate-valid-arithmetic-expressions
def brute_check():
import random
random.seed(3)
def make_L(n=3):
def expr(depth):
if depth==1 or random.random()<1.0/(2**depth-1):
return random.choice([0,1])
if random.random()<0.25:
return ['-', expr(depth-1)]
return [random.choice(['+','*']), expr(depth-1), expr(depth-1)]
return expr(n)
for i in range(100):
L = make_L(n=10)
a = calc(L)
b = calc(bin_to_n(L))
c = calc(bin_to_n2(L))
d = calc(bin_to_n3(L))
if a != b:
print('discrepancy', L,bin_to_n(L), a, b)
if a != c:
print('discrepancy', L,bin_to_n2(L), a, c)
if a != d:
print('discrepancy', L,bin_to_n3(L), a, d)
brute_check()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.