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:
self.inorder(node.right)
def evaluate(self, parent, left):
# can simplify this to call helper function
if left:
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):
print("num node")
print("evaluating", parent.left.eval())
newNode = NumNode(parent.left.eval())
else:
print("variable node")
print("evaluating", parent.left.eval())
newNode = VariableNode(str(parent.left.eval()) + "x")
parent.left = newNode
......@@ -113,14 +118,20 @@ class PlusNode:
self.value = "+"
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
else:
return False
def getInverse(self):
return MinusNode(None,None)
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()
......@@ -134,8 +145,11 @@ class MinusNode:
self.value = "-"
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
else:
return False
def getInverse(self):
......@@ -354,5 +368,11 @@ def main():
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__":
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