I need help creating the methods for using a linked binary tree to build,evaluat
ID: 3689197 • Letter: I
Question
I need help creating the methods for using a linked binary tree to build,evaluate, and paranthesize the expression. We have to use linked stack to pop and push the operands into the expression.
Theres two classes:
Expression: Constructor, set/gets, methods.
ExpressionTest: Test driver for defining exp, and calling methods
I need to put the mathematical exp (from the test class) into a binary tree, evaluate the exp in the binary tree, paranthesize it and print it out.
So i need to create 3 methods for building the tree, evaulating it, and paranthesizing it.
package lab5;
import net.datastructures.*;
public class Expression<T> {
/** Contain Linked Tree and Linked Stack instance variables **/
LinkedBinaryTree<T> tree;
LinkedStack<LinkedBinaryTree<T>> stack;
public Expression () {
tree = new LinkedBinaryTree<T> ();
stack = new LinkedStack<LinkedBinaryTree<T>>();
} // end constructor
public LinkedBinaryTree<T> buildExpression (String expression) {// LinkedBinaryTree<T> is a type of LinkedBinaryTree
// major TODO to implement the algorithm]
LinkedBinaryTree<T> operand, op1, op2;
LinkedStack<LinkedBinaryTree<T>> newStack = new LinkedStack<LinkedBinaryTree<T>>();
String symbol;
int i = 0;
int len = expression.length();
for (i = 0; i < len; i++) {
symbol = expression.substring(i, i+1);
if ((!symbol.equals ("(")) && (!symbol.equals (")"))) {
operand = new LinkedBinaryTree<T> ();
operand.addRoot((T)symbol);
newStack.push(operand);
} else if (symbol.equals ("(")){
continue;
} else {
op2 = newStack.pop();
operand = newStack.pop();
op1 = newStack.pop();
tree.attach(operand.root(), op1, op2);
newStack.push(operand);
}
}
tree = newStack.pop();
return tree;
} // end method buildExpression
public int evaluateExpression (LinkedBinaryTree<T> tree, Position<T> v) {
int result = 0;
LinkedStack<String> opStack = new LinkedStack<String>();
LinkedStack<String> valStack = new LinkedStack<String>();
if (tree.isInternal(v)) {
for (Position<T> c : tree.children(v)){
String x = (String)c.getElement();
if (!x.equals("+") && !x.equals("-") && !x.equals("x") && !x.equals("/")) {
valStack.push(x);
valStack.push(x);
String opPop = opStack.pop();
switch (opPop) {
case "+":
String a = valStack.pop();
String b = valStack.pop();
System.out.print(a);
break;
}
} else {
opStack.push(x);
}
//System.out.print(x);
evaluateExpression(tree, c);
}
}
/*if (tree.isInternal(v)) {
for (Position<T> c : tree.children(v)) {
System.out.print(tree.right(c));
System.out.print(v.getElement());
}
}*/
return result;
}
public void parenthesize (Tree<T>tree, Position<T> p) {
if (tree.isInternal(p)) {
boolean firstTime = true;
for (Position<T> c : tree.children(p)) {
System.out.print((firstTime ? "(" : ","));
System.out.print(c.getElement());
firstTime = false;
parenthesize(tree, c);
}
System.out.print(")");
}
}
}
Ouput:
The Tree: net.datastructures.LinkedBinaryTree@12a3a380
The Parenthesize Tree:(/(x(+(3,1),3),+(-(9,5),2)),+(x(3,-(7,4)),6))
Exception in thread "main" java.lang.NullPointerException
at lab5.Expression.evaluateExpression(Expression.java:60)
it won't evaluate the expression inside the binary tree. i just keep getting nullpointerexception.
Explanation / Answer
#include #include #include #include public class Expression { public: char data; TreeNode *left, *right; TreeNode(char data) { this->data = data; this->left = NULL; this->right = NULL; } }; public Expression () { public: TreeNode *treeNode; StackNode *next; StackNode(TreeNode *treeNode) { this->treeNode = treeNode; next = NULL; } }; class ExpressionTree { private: StackNode *top; public: ExpressionTree() { top = NULL; } void clear() { top = NULL; } void push(TreeNode *ptr) { if (top == NULL) top = new StackNode(ptr); else { StackNode *nptr = new StackNode(ptr); nptr->next = top; top = nptr; } } TreeNode *pop() { if (top == NULL) { System.out.priintln("Underflow"); } else { TreeNode *ptr = top->treeNode; top = top->next; return ptr; } } TreeNode *peek() { return top->treeNode; } void insert(char val) { if (isDigit(val)) { TreeNode *nptr = new TreeNode(val); push(nptr); } else if (isOperator(val)) { TreeNode *nptr = new TreeNode(val); nptr->left = pop(); nptr->right = pop(); push(nptr); } else { System.out.println("Invalid Expression"); return; } bool isDigit(char ch) { return ch >= '0' && ch = 0; i--) insert(eqn[i]); } double evaluate() { return evaluate(peek()); } double evaluate(TreeNode *ptr) { if (ptr->left == NULL && ptr->right == NULL) return toDigit(ptr->data); else { double result = 0.0; double left = evaluate(ptr->left); double right = evaluate(ptr->right); char op = ptr->data; switch (op) { case '+': result = left + right; break; case '-': result = left - right; break; case '*': result = left * right; break; case '/': result = left / right; break; default: result = left + right; break; } return result; } } void postfix() { postOrder(peek()); } void postOrder(TreeNode *ptr) { if (ptr != NULL) { postOrder(ptr->left); postOrder(ptr->right); System.out.println(ptr->data); } } void infix() { inOrder(peek()); } void inOrder(TreeNode *ptr) { if (ptr != NULL) { inOrder(ptr->left); coutright); } } void prefix() { preOrder(peek()); } void preOrder(TreeNode *ptr) { if (ptr != NULL) { coutleft); preOrder(ptr->right); } } }; int main() { string s; System.out.println("Expression Tree Test"); ExpressionTree et; System.out.println(" Enter equation in Prefix form: "); IputBufferedStream( String s); s=new main.InputBufferedStream(); et.buildTree(s); System.out.println(" Prefix : "); et.prefix(); System.out.println(" Infix : "); et.infix(); System.out.println(" Postfix : "); et.postfix(); System.out.println(" Evaluated Result : "Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.