import operator
from nodesrefs import * 

def evaluate(parseTree):
    """ evaluate a parse tree that's represented using the BinaryTree object """
    opers = {'+':operator.add, '-':operator.sub, 
             '*':operator.mul, '/':operator.div}
    
    leftC = parseTree.getLeftChild()
    rightC = parseTree.getRightChild()
    
    if leftC and rightC:
        fn = opers[parseTree.getRootValue()]
        return fn(evaluate(leftC),evaluate(rightC))
    else: 
        return parseTree.getRootValue()

    
# create parse tree representing (5+3)*(7-5)
    
t = BinaryTree('*')
t.insertLeftChild('+')
t.insertRightChild('-')

lchild = t.getLeftChild()
rchild = t.getRightChild()

lchild.insertLeftChild(5)
lchild.insertRightChild(3)

rchild.insertLeftChild(7)
rchild.insertRightChild(5)

print evaluate(t)

