Java
construct an expression tree, then print the preorder of the tree for each expression
Test following cases:
7 * ( 3 - 6 ) + 5 => + * 7 - 3 6 5 13.0 +
18 / -4 => + 13.0 / 18 -4
0.11 / 5 * ( 0.2 * 7 ) => * / 0.11 5 * 0.2 7
-0.27 * 2.21 + 3 - -0.7 / -2
2.05 - 5 + 3 -3 - 1 + 2 + -3.4 / 5
( 855 / 11 ) * ( 12.2 / 321 - ( 1 + 2 + ( 1 + 1.1 + ( 12.51 - 0.51 ) ) ) ) - 0.11
-0.12 + 1.22 - -12.1 / 2.1 => - + -0.12 1.22 / -12.1 2.1
0.5 * 8 / ( 12 + 5 * ( 2 + 3 / 2 ) ) - ( 5 + 1 ) * 3 / 2.5 ==> - / * 0.5 8 + 12 * 5 + 2 / 3 2 / * + 5 1 3 2.5
( -5 + 2 ) / -3 * 2 - -1 / 3 * ( ( 5 / -2) + 1.5 ) => - * / + -5 2 -3 2 * / -1 3 + / 5 -2 1.5
Program
import java.util.ArrayList;
import java.util.*;
//Stack class
class Stack<T>
{
//ArratList
ArrayList<T> stackTop;
//constructor
public Stack()
{
stackTop = new ArrayList<>();
}
//method to determine whether the stack is empty.
public boolean isEmpty()
{
if(stackTop.isEmpty())
return true;
return false;
}
//method to add newItem to the stack.
public void add(T value)
{
stackTop.add(value);
}
//method to remove the top element of the stack.
public T pop()
{
if(!isEmpty())
{
T value =
stackTop.get(stackTop.size()-1);
stackTop.remove(stackTop.size() - 1);
return value;
}
else
return null;
}
//method to return the top element of the stack.
public T top()
{
if(!isEmpty())
return
stackTop.get(stackTop.size()-1);
else
return null;
}
}
//InfixToPostfix
class InfixToPostfix
{
//method to return priority of a operator
public int getPriority(String c)
{
switch(c)
{
case "^": return
5;
case "/": return
4;
case "*": return
3;
case "+": return
2;
case "-": return
1;
case "(": return
0;
default: return
999;
}
}
//check if operator
boolean isOperator(String s)
{
switch(s)
{
case "+": case
"-": case "/": case "*": case "^":
return true;
}
return false;
}
//main method
public String infixToPostfix(String e)
{
Stack<String> stk = new
Stack();
String output = "";
stk.add("(");
String exp = e + " )";
String arr[] = exp.split(" ",
50);
//convert from infix to
postfix
for(int i=0; i<arr.length;
i++)
{
String ch =
arr[i];
//check for left
parenthesis
if(ch.equals("("))
{
stk.add(ch);
}
//check for
righr parenthesis
else
if(ch.equals(")"))
{
while(!stk.top().equals("("))
{
output = output + stk.pop() +
" ";
}
stk.pop();
}
//operator
else
if(isOperator(ch))
{
int p1 = getPriority(ch);
int p2 = getPriority(stk.top());
while(p1<=p2)
{
output = output + stk.pop() +
" ";
p2 =
getPriority(stk.top());
}
stk.add(ch);
}
//operand
else
{
output = output + ch + " ";
}
// System.out.println
(output);
}
if(!stk.isEmpty())
return
"None";
else
return
output;
}
}
//Node class
class Node
{
String info;
Node lchild;
Node rchild;
//constructor
Node(String val)
{
info = val;
lchild = null;
rchild = null;
}
}
//ExpressionTree class
class ExpressionTree
{
//check for operator
boolean isOperator(String s)
{
switch(s)
{
case "+": case
"-": case "/": case "*": case "^":
return true;
}
return false;
}
//preorder traversal
void preorder(Node root)
{
if(root!=null)
{
System.out.print(root.info + " ");
preorder(root.lchild);
preorder(root.rchild);
}
}
//expression tree construction
Node expressionTreeConstruction(String postfix)
{
Stack<Node> stk = new
Stack();
String arr[] = postfix.split("
",20);
Node t = null;
for(int i=0; i<arr.length-1;
i++)
{
if(arr[i]==" ")
continue;
t = new
Node(arr[i]);
if(!isOperator(arr[i]))
{
stk.add(t);
}
else
{
Node r = stk.pop();
Node l = stk.pop();
t.lchild = l;
t.rchild = r;
stk.add(t);
}
}
return stk.pop();
}
//main method
public static void main (String[] args)
{
Scanner sc = new
Scanner(System.in);
System.out.println("Enter the
expression: ");
String infix = sc.nextLine();
InfixToPostfix ip = new
InfixToPostfix();
String postfix =
ip.infixToPostfix(infix);
//System.out.println("Postfix
Expression: " + postfix);
if(postfix.equals("None"))
{
System.out.println("Invalid expession");
return;
}
ExpressionTree etree = new
ExpressionTree();
Node root =
etree.expressionTreeConstruction(postfix);
System.out.println("Preorder
traversal of the Expression Tree: ");
etree.preorder(root);
}
}
Output:
Enter the expression:
7 * ( 3 - 6 ) + 5
Preorder traversal of the Expression Tree:
+ * 7 - 3 6 5
Enter the expression:
13.0 + 18 / -4
Preorder traversal of the Expression Tree:
+ 13.0 / 18 -4
Enter the expression:
0.11 / 5 * ( 0.2 * 7 )
Preorder traversal of the Expression Tree:
* / 0.11 5 * 0.2 7
Enter the expression:
( -5 + 2 ) / -3 * 2 - -1 / 3 * ( ( 5 / -2 ) + 1.5 )
Preorder traversal of the Expression Tree:
- * / + -5 2 -3 2 * / -1 3 + / 5 -2 1.5
Java construct an expression tree, then print the preorder of the tree for each expression Test...
construct an expression tree, then print the preorder of the tree for each expression Write this in Java, test all cases below 7 * ( 3 - 6 ) + 5 => + * 7 - 3 6 5 13.0 + 18 / -4 => + 13.0 / 18 -4 0.11 / 5 * ( 0.2 * 7 ) => * / 0.11 5 * 0.2 7 -0.27 * 2.21 + 3 - -0.7 / -2 2.05 - 5 +...
Tree and Preorder For each expression, construct an expression tree, then print the preorder of the tree. Write this in Java, test all cases below 7 * ( 3 - 6 ) + 5 => + * 7 - 3 6 5 13.0 + 18 / -4 => + 13.0 / 18 -4 0.11 / 5 * ( 0.2 * 7 ) => * / 0.11 5 * 0.2 7 -0.27 * 2.21 + 3 - -0.7 / -2 2.05...
[Python]
Construct Tree Using Inorder and Preorder
Given Preorder and Inorder traversal of a binary tree, create
the binary tree associated with the traversals.You just need to
construct the tree and return the root.
Note: Assume binary tree contains only unique elements.
Input format :
Line 1 : n (Total number of nodes in binary tree)
Line 2 : Pre order traversal
Line 3 : Inorder Traversal
Output Format :
Elements are printed level wise, each level in new line...
Test Data would be as follows:
Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...
Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...
Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...
Please help calculate average CFU/ml
Treatment 1: BHI, 370c, +02 Time (min)oD (average) CFU/ml # of colonies 65, 118, 119, 13 8, 16, 13, o Dilution 0.11 E-6 E-7 15 30 0.12 0.19 E-6 E-7 97, 155, 213, 122 18, 8, 26, 10 45 60 0.31 0.51 E-6 E-7 224, 273, 156, 258 64, 44, 16, 66 75 90 0.88 52, 67, 96, TNTC 14, 5, 9, 93 0.98 E-7 E-8 105 120 1.07 E-7 E-8 400, 116, TNTC, 0...
Given the morphological characteristics in the table below,
construct a parsimonious tree.
Given the distance matrix in the table below, construct a parsimonious tree. Species 1 Species 2 Species 3 Species 4 Species 5 Species 6 Species 7 Species 1 11 18 2 19 17 3 Species 2 11 17 9 18 19 10 Species 3 18 17 18 2 4 17 Species 4 2 9 18 20 5 4 Species 5 19 18 2 20 7 17 Species 6...
Given the distance matrix in the table below, construct a
parsimonious tree.
Given the distance matrix in the table below, construct a parsimonious tree. Species 1 Species 2 Species 3 Species 4 Species 5 Species 6 Species 7 Species 1 11 18 2 19 17 3 Species 2 11 17 9 18 19 10 Species 3 18 17 18 2 4 17 Species 4 2 9 18 20 5 4 Species 5 19 18 2 20 7 17 Species 6...
HW Score: 77.78 %, 14 of 18 p Score: 0 of 1 pt 15 of 18 (15 complete) X 3.3.22 Question Help Use the following cell phone airport data speeds (Mbps) from a particular network. Find Q3. 0.5 0.4 0.4 0.4 0.5 0.5 0.5 0.2 0.3 0.4 0.9 1.2 1.2 1.3 1.5 0.8 0.8 0.9 0.7 0.7 2.8 3.6 3.6 4.9 2.4 2.4 2.4 1.8 2.1 2.2 7.6 9.5 12.2 8.6 12.1 5.6 6.5 7,4 4.9 5.5 25.3 13.8 14.2...