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 - 5 + 3 -3 - 1 + 2 + -3.4 / 5
solution:
class StackNode
{
public char data;
public StackNode leftChild;
public StackNode rightChild;
public StackNode(char x)
{
data = x;
}
public void displayNode()
{
System.out.print(data);
}
}
class Stack1
{
private StackNode[] a;
private int top, m;
public Stack1(int max)
{
m = max;
a = new StackNode[m];
top = -1;
}
public void push(StackNode key)
{
a[++top] = key;
}
public StackNode pop()
{
return (a[top--]);
}
public boolean isEmpty()
{
return (top == -1);
}
}
class Stack2
{
private char[] a;
private int top, m;
public Stack2(int max)
{
m = max;
a = new char[m];
top = -1;
}
public void push(char key)
{
a[++top] = key;
}
public char pop()
{
return (a[top--]);
}
public boolean isEmpty()
{
return (top == -1);
}
}
class Conversion
{
private Stack2 s;
private String input;
private String output = "";
public Conversion(String str)
{
input = str;
s = new Stack2(str.length());
}
public String inToPost()
{
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
switch (ch)
{
case '+':
case '-':
getOperator(ch, 1);
break;
case '*':
case '/':
getOperator(ch, 2);
break;
case '(':
s.push(ch);
break;
case ')':
gotParenthesis();
break;
default:
output = output + ch;
}
}
while (!s.isEmpty())
output = output + s.pop();
return output;
}
private void getOperator(char opThis, int prec1)
{
while (!s.isEmpty())
{
char opTop = s.pop();
if (opTop == '(')
{
s.push(opTop);
break;
} else
{
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1)
{
s.push(opTop);
break;
} else
output = output + opTop;
}
}
s.push(opThis);
}
private void gotParenthesis()
{
while (!s.isEmpty())
{
char ch = s.pop();
if (ch == '(')
break;
else
output = output + ch;
}
}
}
class Tree
{
private StackNode root;
public Tree()
{
root = null;
}
public StackNode getRoot() {
return root;
}
public void insert(String s)
{
Conversion c = new Conversion(s);
s = c.inToPost();
Stack1 stk = new Stack1(s.length());
s = s + "#";
int i = 0;
char symbol = s.charAt(i);
StackNode newNode;
while (symbol != '#')
{
if (symbol >= '0' && symbol <= '9' || symbol >= 'A'
&& symbol <= 'Z' || symbol >= 'a' && symbol <= 'z')
{
newNode = new StackNode(symbol);
stk.push(newNode);
} else if (symbol == '+' || symbol == '-' || symbol == '/'
|| symbol == '*')
{
StackNode ptr1 = stk.pop();
StackNode ptr2 = stk.pop();
newNode = new StackNode(symbol);
newNode.leftChild = ptr2;
newNode.rightChild = ptr1;
stk.push(newNode);
}
symbol = s.charAt(++i);
}
root = stk.pop();
}
public void preOrder(StackNode localRoot)
{
if (localRoot != null)
{
localRoot.displayNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
}
public class Main
{
public static void main(String args[])
{
Tree tree = new Tree();
String exp1 = "7*(3-6)+5";
tree.insert(exp1);
tree.preOrder(tree.getRoot());
}
}
please give me thumb up
Tree and Preorder For each expression, construct an expression tree, then print the preorder of the...
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 +...
[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...
7) Find the preorder and postorder traversal for each tree. 4 points) a) 2. (6 points) b) レ 7) Find the preorder and postorder traversal for each tree. 4 points) a) 2. (6 points) b) レ
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...
Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the value of the expression by doing an in-order traversal of the expression tree. For example: Your output should print the following: o The input is: "Expression" o The inorder traversal is: "inorder traversal" o The output of the expression is: "output" Please print the output in the same format. (example shown below) The input is: 1 68+ The inorder traversal is: 168 The output...
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...
in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...
1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only). 2- Apply your proposed algorithm in the previous point to construct the binary tree with the following traversals (java code only): In order traversal: 9 8 6 1 2 5 4 Postorder traversal: 9 6 1 8 5 4 2