-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepresentExpressionUsingBinaryTree.java
More file actions
70 lines (53 loc) · 1.69 KB
/
Copy pathRepresentExpressionUsingBinaryTree.java
File metadata and controls
70 lines (53 loc) · 1.69 KB
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
public class RepresentExpressionUsingBinaryTree {
Node root;
public RepresentExpressionUsingBinaryTree(Node root) {
this.root = root;
}
public RepresentExpressionUsingBinaryTree() {
root = null;
}
static class Node {
String data;
Node left, right;
public Node(String data) {
this.data = data;
left = right = null;
}
}
public void printInOrder(Node root) {
if (root == null) {
return;
}
printInOrder(root.left);
System.out.print(root.data + " ");
printInOrder(root.right);
}
public static void main(String[] args) {
RepresentExpressionUsingBinaryTree expressionTree = new RepresentExpressionUsingBinaryTree();
Node divide = new Node("/");
Node multiply = new Node("*");
Node plus1 = new Node("+");
Node minus = new Node("-");
Node plus2 = new Node("+");
Node five = new Node("5");
Node two1 = new Node("2");
Node two2 = new Node("2");
Node one = new Node("1");
Node two3 = new Node("2");
Node nine = new Node("9");
expressionTree.root = divide;
divide.left = multiply;
divide.right = plus1;
multiply.left = plus2;
multiply.right = minus;
plus2.left = five;
plus2.right = two1;
minus.left = two2;
minus.right = one;
plus1.left = two3;
plus1.right = nine;
System.out.println("InOrder Traversal: ");
expressionTree.printInOrder(expressionTree.root);
System.out.println();
}
}