MCPcopy Index your code
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / postordereval

Function postordereval

ParseTree.py:58–68  ·  view source on GitHub ↗
(tree)

Source from the content-addressed store, hash-verified

56
57# 利用后序遍历实现两个叶结点的运算
58def postordereval(tree):
59 opers = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv}
60 res1 = None
61 res2 = None
62 if tree:
63 res1 = postordereval(tree.getLeftChild())
64 res2 = postordereval(tree.getRightChild())
65 if res1 and res2:
66 return opers[tree.getRootVal()](res1, res2)
67 else:
68 return tree.getRootVal()
69
70# 递归实现中序遍历
71def inorder(tree):

Callers

nothing calls this directly

Calls 3

getLeftChildMethod · 0.80
getRightChildMethod · 0.80
getRootValMethod · 0.80

Tested by

no test coverage detected