class TreeNode: def __init__(self, val=None, left=None, right=None): self.val = val self.left = left self.right = right def infix_to_postfix(infix): operators = {'(': 0, ')': 0, 'NOT': 1, 'AND': 2, 'OR': 3} stack = [] postfix = [] for token in infix: if token in operators: if token == '(': stack.append(token) elif token == ')': while stack[-1] != '(': postfix.append(stack.pop()) stack.pop() else: while stack and operators[stack[-1]] >= operators[token]: postfix.append(stack.pop()) stack.append(token) else: postfix.append(token) while stack: postfix.append(stack.pop()) return postfix def postfix_to_tree(postfix): stack = [] for token in postfix: if token in {'NOT', 'AND', 'OR'}: right = stack.pop() if token == 'NOT': stack.append(TreeNode('NOT', None, right)) else: left = stack.pop() stack.append(TreeNode(token, left, right)) else: stack.append(TreeNode(token)) return stack.pop() def evaluate(root, values): if root.val in values: return values[root.val] elif root.val == 'NOT': return not evaluate(root.right, values) elif root.val == 'AND': return evaluate(root.left, values) and evaluate(root.right, values) elif root.val == 'OR': return evaluate(root.left, values) or evaluate(root.right, values) def print_tree(root, level=0): if root: print_tree(root.right, level + 1) print(' ' * 4 * level + '->', root.val) print_tree(root.left, level + 1) infix = input('请输入命题演算公式:').split() postfix = infix_to_postfix(infix) root = postfix_to_tree(postfix) print('后缀表达式:', postfix) print('二叉树构造过程:') print_tree(root) print('真值表:') variables = list(set(filter(lambda x: x not in {'NOT', 'AND', 'OR'}, infix))) for values in itertools.product([True, False], repeat=len(variables)): values = dict(zip(variables, values)) result = evaluate(root, values) print(values, '->', result)其中有错误NameError: name 'itertools' is not defined。请修改
时间: 2024-01-10 18:04:31 浏览: 111
在代码的开头处添加`import itertools`即可解决该错误:
```
import itertools
class TreeNode:
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right
def infix_to_postfix(infix):
operators = {'(': 0, ')': 0, 'NOT': 1, 'AND': 2, 'OR': 3}
stack = []
postfix = []
for token in infix:
if token in operators:
if token == '(':
stack.append(token)
elif token == ')':
while stack[-1] != '(':
postfix.append(stack.pop())
stack.pop()
else:
while stack and operators[stack[-1]] >= operators[token]:
postfix.append(stack.pop())
stack.append(token)
else:
postfix.append(token)
while stack:
postfix.append(stack.pop())
return postfix
def postfix_to_tree(postfix):
stack = []
for token in postfix:
if token in {'NOT', 'AND', 'OR'}:
right = stack.pop()
if token == 'NOT':
stack.append(TreeNode('NOT', None, right))
else:
left = stack.pop()
stack.append(TreeNode(token, left, right))
else:
stack.append(TreeNode(token))
return stack.pop()
def evaluate(root, values):
if root.val in values:
return values[root.val]
elif root.val == 'NOT':
return not evaluate(root.right, values)
elif root.val == 'AND':
return evaluate(root.left, values) and evaluate(root.right, values)
elif root.val == 'OR':
return evaluate(root.left, values) or evaluate(root.right, values)
def print_tree(root, level=0):
if root:
print_tree(root.right, level + 1)
print(' ' * 4 * level + '->', root.val)
print_tree(root.left, level + 1)
infix = input('请输入命题演算公式:').split()
postfix = infix_to_postfix(infix)
root = postfix_to_tree(postfix)
print('后缀表达式:', postfix)
print('二叉树构造过程:')
print_tree(root)
print('真值表:')
variables = list(set(filter(lambda x: x not in {'NOT', 'AND', 'OR'}, infix)))
for values in itertools.product([True, False], repeat=len(variables)):
values = dict(zip(variables, values))
result = evaluate(root, values)
print(values, '->', result)
```
阅读全文