def evaluateMPLogicParseTree(t): """ Function to evaluate if the Logic parse tree function works properly using True,False,Maybe,and Probably """ opers = {'AND': operator.and_, 'OR': operator.or_} leftC = t.getLeftChild() rightC = t.getRightChild() ran = random.random() if leftC and rightC: if leftC.getRootVal() == 'T': leftC = BinaryTree(True) elif leftC.getRootVal() == 'F': leftC = BinaryTree(False) if rightC.getRootVal() == 'T': rightC = BinaryTree(True) elif rightC.getRootVal() == 'F': rightC = BinaryTree(False) if leftC.getRootVal() == 'M': h = leftC.getLeftChild() m = str(h.getRootVal()) if 0 <= float(m) and float(m) <= ran: leftC = BinaryTree(True) elif ran < float(m) and float(m) < 1: leftC = BinaryTree(False) elif leftC.getRootVal() == 'P': h = leftC.getLeftChild() m = str(h.getRootVal()) if 0 <= float(m) and float(m) <= ran: leftC = BinaryTree(False) elif ran < float(m) and float(m) < 1: leftC = BinaryTree(True) if rightC.getRootVal() == 'M': h = rightC.getLeftChild() m = str(h.getRootVal()) if 0 <= float(m) and float(m) <= ran: rightC = BinaryTree(True) elif ran < float(m) and float(m) < 1: rightC = BinaryTree(False) elif rightC.getRootVal() == 'P': h = rightC.getLeftChild() m = str(h.getRootVal()) if 0 <= float(m) and float(m) <= ran: rightC = BinaryTree(False) elif ran < float(m) and float(m) < 1: rightC = BinaryTree(True) fn = opers[t.getRootVal()] return fn(evaluateMPLogicParseTree(leftC), evaluateMPLogicParseTree(rightC)) else: return t.getRootVal()
from binarytree import BinaryTree r = BinaryTree('a') print(r.getRootVal()) print(r.getLeftChild()) r.insertLeft('b') #insert a node print(r.getLeftChild()) #get that node print(r.getLeftChild().getRootVal()) #get that node, get that key(name) r.insertRight('c') print(r.getRightChild().getRootVal()) #get the name of right child r.getRightChild().setRootVal('hello') print(r.getRightChild().getRootVal()) #get the changed name of right child r.insertRight('o') print('-----') print(r.getRightChild().getRootVal()) print(r.getRightChild().getRightChild().getRootVal())