Question: The input begins with an integer t , which gives the number of test cases in the input. Each test case will consist of two

The input begins with an integer t, which gives the number of test cases in the input.

Each test case will consist of two lines. The first line will contain an integer n, which gives the number of nodes in the tree.

The next line will contain n, space-separated symbols representing the values from the pots-order traversal of the tree. These will be chosen from the following set of values: {&, |, ^, !, t, f}.

Sample input 2 6 t f | f ! ^ 8 f t t t ^ | & ! 

Sample Output:

false true 

What I have so far, and is it possible to do it with an single stack?

With the sample test imput

the postordered tree should look like this...

The input begins with an integer t, which gives the number of

What I have got so far.

import java.io.*; import java.util.*; import java.util.Scanner; import java.util.Stack;

public class Expression {

public static boolean Parse(String str) { char[] arr = str.toCharArray(); Stack bool = new Stack(); Stack operator = new Stack(); for (int i = 0; i

else if (arr[i] == '|' || arr[i] == '!' || arr[i] == '^') { operator.push(arr[i]); if (operator.peek() == '!') bool.push(applyOp(operator.pop(), bool.pop(), false)); else bool.push(applyOp(operator.pop(), bool.pop(), bool.pop())); } } return bool.pop(); }

public static boolean applyOp(char op, boolean b, boolean a) { switch (op) { case '|': return a | b; case '^': return a ^ b; case '!': return !a; } return false; } public static void main(String[] args){ Scanner in = new Scanner(System.in); int t = in.nextInt(); //t indicate the run cases int m = in.nextInt(); //contain nodes int cur=0; String a = in.nextLine(); for(int i =0; i

}

t f I f ! A f tz ta t. a | & ! 0 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!