public class Node public enum NodeType sum minus divison multiplicatio

 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
public class Node
{
public enum NodeType { sum, minus, divison, multiplication, number};
public NodeType ntype;
public Node left;
public Node right;
public int value;
public Node(Node l, Node r, NodeType n, int num)
{
left = l;
right = r;
ntype = n;
value = num;
}
public void print(Node n, int offset)
{
for (int i = 0; i < offset; i++)
Console.Write(" ");
string s = "+-/*";
if (n.ntype == NodeType.number)
Console.WriteLine(n.value);
else
Console.WriteLine(s[(int)(n.ntype)]);
if (n.left != null)
print(n.left, offset + 4);
if (n.right != null)
print(n.right, offset + 4);
}
Node res = new Node(new Node(
new Node(null, null, Node.NodeType.number, 5),
new Node(null, null, Node.NodeType.number, 2),
Node.NodeType.sum,
0),
new Node(null, null, Node.NodeType.number, 3),
Node.NodeType.multiplication,
0);
res.print(res, 0);
Console.Read();