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.
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
I need help creating the methods for using a linked binary tree to build,evaluate, and paranthesize...
The code below accepts and evaluates an integer expression with the following operators: +, _, *, and /. Your task is to modify it to include the % remainder operator that has the same precedence as * and /. No need to rewrite the entire program, just insert the needed statements. import java.util.Stack; public class EvaluateExpression { public static void main(String[] args) { // Check number of arguments passed if (args.length != 1) { System.out.println( "Usage:...
Return a method as an expression tree
Hi guys.
I need to return a method as an expression tree, it's currently
returning null.
public static ExpressionTree getExpressionTree(String
expression)
throws Exception {
char[] charArray = expression.toCharArray();
Node root = et.constructTree(charArray);
System.out.println("infix expression is");
et.inorder(root);
return et;
}
In the above method, et needs to have a value.
-- Take a look at the complete class below.
Kindly assist.
; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...
I am trying to understand the following code I found in a website so that I can use it for a project that evaluates boolean expressions. The code is below but I think it is in java because I do not understand it. Can you help me describe what it is doing in c++ so that I can understand it better? The link to the code is here: https://stackoverflow.com/questions/16762057/algorithm-to-evaluate-value-of-boolean-expression The code is below: public static boolean evaluateBool(String s) { Stack<Object>...
Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...
I'm getting errors that i can't figure out. I need help fixing them. particularly focus on the errors they are highlighted in bold on the list code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <ctype.h> #include "stack.h" #include "booleanEvaluation.h" #include "booleanWithError.h" /* evaluatePostfix * input: a postfix expression * output: T, F, or E * * Uses a stack to evaluates the postfix expression and returns the result as a string where "T" denotes true and "F" denotes...
Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix) { LinkedStack stack2 = new LinkedStack(); int val1; int val2; int result; for(int i = 0; i < postFix.length(); i++) { char m = postFix.charAt(i); if(Character.isDigit(m)) { stack2.push(m); } else { ...
I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...
Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...