Commit 16a63a41 authored by mrk022's avatar mrk022

can follow user steps up until fraction reduction

parent c37c1241
# Equation Tree class
# AI Final Project
# Takes an equation of the form x + 3 = 5 + x and makes this into an equation tree
# this means = is in root and each expression is a tree on either side
# currently only works for addition and two variables on each side of equation
class EquationTreeNode:
def __init__(self, value, parent=None):
self.value = value
self.left = None
self.right = None
self.parent = parent
def setLeft(self, newNode):
self.left = newNode
def setRight(self, newNode):
self.right = newNode
def addLeftNode(self, newNode):
self.left = newNode
newNode.parent = self
def addRightNode(self, newNode):
self.right = newNode
newNode.parent = self
def splitExpression(self, expression):
expressions = expression.split("+")
leftNode = EquationTreeNode(expressions[0])
rightNode = EquationTreeNode(expressions[1])
self.addLeftNode(leftNode)
self.addRightNode(rightNode)
'''def main():
equation = "3x+5=2+2x"
expressions = equation.split("=")
rootNode = EquationTreeNode("=", None)
leftNode = EquationTreeNode("+")
rootNode.addLeftNode(leftNode)
splitExpression(expressions[0], leftNode)
rightNode = EquationTreeNode("+")
rootNode.addRightNode(rightNode)
splitExpression(expressions[1], rightNode)
print(rootNode.value)
print(rootNode.left.value, rootNode.right.value)
print(rootNode.left.left.value, rootNode.left.right.value, rootNode.right.left.value, rootNode.right.right.value)
main() '''
\ No newline at end of file
...@@ -28,12 +28,17 @@ class EquationTree: ...@@ -28,12 +28,17 @@ class EquationTree:
self.inorder(node.right) self.inorder(node.right)
def evaluate(self, parent, left): def evaluate(self, parent, left):
# can simplify this to call helper function
if left: if left:
if parent.left.canEval(): if parent.left.canEval():
print("parent.left.left is ", parent.left.left.value)
print("parent.left.right is ", parent.left.right)
if isinstance(parent.left.left, NumNode): if isinstance(parent.left.left, NumNode):
print("num node")
print("evaluating", parent.left.eval()) print("evaluating", parent.left.eval())
newNode = NumNode(parent.left.eval()) newNode = NumNode(parent.left.eval())
else: else:
print("variable node")
print("evaluating", parent.left.eval()) print("evaluating", parent.left.eval())
newNode = VariableNode(str(parent.left.eval()) + "x") newNode = VariableNode(str(parent.left.eval()) + "x")
parent.left = newNode parent.left = newNode
...@@ -113,14 +118,20 @@ class PlusNode: ...@@ -113,14 +118,20 @@ class PlusNode:
self.value = "+" self.value = "+"
def canEval(self): def canEval(self):
if isinstance(self.left, VariableNode) == isinstance(self.right,VariableNode): if isinstance(self.left, VariableNode) and isinstance(self.right,VariableNode):
return True return True
return False elif isinstance(self.left, NumNode) and isinstance(self.right, NumNode):
return True
else:
return False
def getInverse(self): def getInverse(self):
return MinusNode(None,None) return MinusNode(None,None)
def eval(self): def eval(self):
#print("left value ", self.left)
#print(isinstance(self.left, NumNode))
#print("right value ", self.right.value())
return self.left.eval() + self.right.eval() return self.left.eval() + self.right.eval()
...@@ -134,9 +145,12 @@ class MinusNode: ...@@ -134,9 +145,12 @@ class MinusNode:
self.value = "-" self.value = "-"
def canEval(self): def canEval(self):
if isinstance(self.left, VariableNode) == isinstance(self.right,VariableNode): if isinstance(self.left, VariableNode) and isinstance(self.right,VariableNode):
return True
elif isinstance(self.left, NumNode) and isinstance(self.right, NumNode):
return True return True
return False else:
return False
def getInverse(self): def getInverse(self):
return PlusNode(None, None) return PlusNode(None, None)
...@@ -353,6 +367,12 @@ def main(): ...@@ -353,6 +367,12 @@ def main():
equationTree.printTree() equationTree.printTree()
equationTree.get_expression() equationTree.get_expression()
'''
print("old node should be 2, is ", oldNum.value)
print("moved node should be 7, is ", moveNode.value)
print("new op is ", newOp.value)
'''
if __name__ == "__main__": if __name__ == "__main__":
main() main()
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment