Commit 72411f6d by mrk022

### commented and cleaned up code

parent 6d69bce5
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 ''' AICogSci Final Project base tree code from http://knuth.luther.edu/~leekent/CS2Plus/chap6/chap6.html Added: - EquationTree class to generate a specialized tree - Branch and Equals Node classes to make printing more legible - canEval, getInverse and inorder methods to most of the nodes to allow further functionality - VariableNode class was added to account for nodes with variables - add_to_tree and generate_tree methods used to create and change equation tree class''' import queue ... ... @@ -73,7 +80,6 @@ class EquationTree: q.enqueue(currentNode.right) print(q.dequeue().value, end =' ') class Branch: def __init__(self): self.value = "\n" ... ... @@ -93,19 +99,6 @@ class TimesNode: 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) and isinstance(self.right,VariableNode): # return self.left.eval()*self.right.eval() #elif isinstance(self.left,VariableNode) or isinstance(self.right,VariableNode): # print("can't combine variables of differing types") #need some sort of combined node here i think #else: return self.left.eval() * self.right.eval() def inorder(self): ... ... @@ -129,9 +122,6 @@ class PlusNode: 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() ... ... @@ -187,44 +177,11 @@ class VariableNode: return int(self.value.split("x")[0]) def canEval(self): return False #not an operator return False def inorder(self): return str(self.eval()) def check_if_variables(x, y): if 'x' in x and 'x' in y: return "both_vars" elif 'x' in x or 'x' in y: return "one_var" else: return "both_const" def E(q): if q.isEmpty(): raise ValueError("Invalid Prefix Expression") token = q.dequeue() if token == "+": return PlusNode(E(q),E(q)) if token == "*": return TimesNode(E(q),E(q)) if token == "-": return MinusNode(E(q), E(q)) #try: if "x" in token: output = VariableNode(token) # make a different class for variable node else: output = NumNode(int(token)) #except: # print("Not a valid operation, need variables of like terms") # output = NumNode(float(0)) return output OPERATORS = set(['+', '-', '*', '/', '(', ')']) PRIORITY = {'+':1, '-':1, '*':2, '/':2} ... ... @@ -251,8 +208,6 @@ def infix_to_prefix(formula): b = exp_stack.pop() exp_stack.append( op+" "+b+" "+a ) op_stack.append(ch) # leftover while op_stack: op = op_stack.pop() a = exp_stack.pop() ... ... @@ -312,67 +267,4 @@ def generate_tree(equation): add_to_tree(q2, t.root, False) # add expression tree to right side of equation tree t.printTree() return t def eval_tree(expression): '''x = NumNode(5) y = NumNode(4) p = PlusNode(x,y) t = TimesNode(p, NumNode(6)) root = PlusNode(t, NumNode(3)) print(root.eval()) print(root.inorder())''' #x = input("Please enter a prefix expression: ") x = infix_to_prefix(expression) lst = x.split() q = queue.Queue() for token in lst: q.enqueue(token) root = E(q) result = root.eval() print(root.eval()) print(root.inorder()) return result #print(infix_to_prefix("3 + 4 - 6")) def split_equation(equation): expressions = equation.split("=") return expressions def main(): #exp = split_equation("4x * 3x + 2") #x = eval_tree(exp[0]) '''eqTree = EquationTree() x1 = VariableNode("4x") x2 = NumNode(3) x3 = NumNode(2) x4 = NumNode(7) t = PlusNode(x1,x2) t2 = PlusNode(x3,x4) eqTree.root.left = t eqTree.root.right = t2 eqTree.printTree()''' equationTree = generate_tree("4x + 3 = 2 + 7") print(equationTree.root.left.canEval()) print(equationTree.root.right.canEval()) newNode = NumNode(equationTree.root.right.eval()) equationTree.updateNode(equationTree.root, False, newNode) equationTree.printTree() 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 return t \ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!