Commit 8ecfff01 authored by mrk022's avatar mrk022

updated tree.py to handle variable nodes

parent 1e7de1fe
......@@ -20,8 +20,8 @@ class Queue:
# When queue is half full, compress it. This
# achieves an amortized complexity of O(1) while
# not letting the list continue to grow unchecked.
if self.frontIdx * 2 > len(self.items):
self.__compress()
#if self.frontIdx * 2 > len(self.items):
# self.__compress()
item = self.items[self.frontIdx]
self.frontIdx += 1
......
No preview for this file type
# base tree code from http://knuth.luther.edu/~leekent/CS2Plus/chap6/chap6.html
# changed this code to allow for variable functions, and change infix expressions to postfix
# added variable node class to consider when both nodes have variables in them
import queue
......@@ -9,6 +10,16 @@ class TimesNode:
self.right = right
def eval(self):
'''if check_if_variables(str(self.left.eval()), str(self.right.eval())) == "both_vars":
v1 = str(self.left.eval()).split("x")[0]
v2 = str(self.right.eval()).split("x")[0]
evalStr = v1 + "*" + v2
return eval(evalStr)
else:
return self.left.eval() * self.right.eval()'''
#if isinstance(self.left, VariableNode):
# return self.left.getMultiplier() * self.left.getMultiplier()
#else:
return self.left.eval() * self.right.eval()
def inorder(self):
......@@ -47,6 +58,17 @@ class NumNode:
def inorder(self):
return str(self.num)
class VariableNode:
def __init__(self, value):
self.value = value
def eval(self):
# get value before x
return float(self.value.split("x")[0])
def inorder(self):
return str(self.eval())
def check_if_variables(x, y):
if 'x' in x and 'x' in y:
return "both_vars"
......@@ -60,7 +82,6 @@ def E(q):
raise ValueError("Invalid Prefix Expression")
token = q.dequeue()
if token == "+":
return PlusNode(E(q),E(q))
......@@ -70,11 +91,15 @@ def E(q):
if token == "-":
return MinusNode(E(q), E(q))
try:
#try:
if "x" in token:
output = VariableNode(token)
# make a different class for variable node
else:
output = NumNode(float(token))
except:
print("Not a valid operation, need variables of like terms")
output = NumNode(float(0))
#except:
# print("Not a valid operation, need variables of like terms")
# output = NumNode(float(0))
return output
OPERATORS = set(['+', '-', '*', '/', '(', ')'])
......@@ -144,7 +169,8 @@ def split_equation(equation):
return expressions
def main():
exp = split_equation("3 * 4 - 6=0")
exp = split_equation("4x * 3x")
eval_tree(exp[0])
......
......@@ -101,7 +101,6 @@ class IntelligentTutor(ACTR):
goal = Buffer()
user = UserState("4x+7=5x+2")
def init():
user.get_input()
goal.set(user.state)
......
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