-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Build_Expression_Tree.java
132 lines (113 loc) · 3.79 KB
/
Build_Expression_Tree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
This is the solution to build an expression tree from a given postfix expression.
The corresponding infix expression is obtained by inorder traversal of the built expression tree.
Constraints:
1)Operands in the expression should lie in (0,10^5) or (A,Z) or (a,z)
2)Operators in the expression - {+,-,*,/,%,^}
Approach:
1)Traverse through all the elements in the expression and check if it is an operator or operand.
2)If it is an operand, push it into the stack.
3)It it is an operator,pop the top 2 elements and make them the right and left child respectively of the current node.
Push this node on the stack.
4)After the expression is traversed,only one element is left in the stack.
This is the root of the required expression tree.
*/
import java.util.*;
public class Build_Expression_Tree {
//Class which defines the structure of each node in the binary tree
static class Node {
String data;
Node left, right;
Node(String data) {
this.data = data;
this.left = null;
this.right = null;
}
}
//Function to check if the element passed is an operator
static boolean isOperator(String ch) {
if (ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/") || ch.equals("%") || ch.equals("^"))
return true;
else
return false;
}
//Function which builds the expression tree and returns the root node of the tree
static Node buildTree(ArrayList < String > exp) {
Node root;
Node temp1;
Node temp2;
Stack < Node > s = new Stack < > ();
for (int i = 0; i < exp.size(); i++) {
//If the element is an operand,push it on the stack
if (!isOperator(exp.get(i))) {
root = new Node(exp.get(i));
s.push(root);
}
//If the character is an operator,pop the top two nodes and make them the children of the current character node
else {
root = new Node(exp.get(i));
temp1 = s.pop();
temp2 = s.pop();
root.right = temp1;
root.left = temp2;
s.push(root);
}
}
root = s.peek();
s.pop();
return root;
}
//Inorder traversal by recursion
static void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter postfix expression whose corresponding expression tree is required: ");
ArrayList < String > exp = new ArrayList < > ();
String val = " ";
while (!val.equals("-1")) {
System.out.println("Enter operator or operand or -1 to end");
val = sc.next();
if (!val.equals("-1"))
exp.add(val);
}
Node root = buildTree(exp);
System.out.println("The infix expression by traversing the expression tree is");
inorder(root);
sc.close();
}
}
/*
Sample Input:
Enter postfix expression whose corresponding expression tree is required:
Enter operator or operand or -1 to end
250
Enter operator or operand or -1 to end
300
Enter operator or operand or -1 to end
+
Enter operator or operand or -1 to end
3
Enter operator or operand or -1 to end
/
Enter operator or operand or -1 to end
5
Enter operator or operand or -1 to end
*
Enter operator or operand or -1 to end
10
Enter operator or operand or -1 to end
+
Enter operator or operand or -1 to end
-1
Sample Output:
The infix expression by traversing the expression tree is
250 + 300 / 3 * 5 + 10
Time Complexity : O(n)
Space Complexity : O(n)
*/