Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2011 14:33
Show Gist options
  • Save anonymous/915599 to your computer and use it in GitHub Desktop.
Save anonymous/915599 to your computer and use it in GitHub Desktop.
def buildParseTree(fpexp):
fplist = fpexp.split()
pStack = Stack()
eTree = BinaryTree('')
pStack.push(eTree)
currentTree = eTree
for i in fplist:
if i == '(':
currentTree.insertLeft('')
pStack.push(currentTree)
currentTree = currentTree.getLeftChild()
elif i not in '+-*/)':
currentTree.setRootVal(eval(i))
parent = pStack.pop()
currentTree = parent
elif i in '+-*/':
currentTree.setRootVal(i)
currentTree.insertRight('')
pStack.push(currentTree)
currentTree = currentTree.getRightChild()
elif i == ')':
currentTree = pStack.pop()
return eTree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment