-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy path79.TernaryExpressionToTree.cs
More file actions
130 lines (115 loc) · 3.14 KB
/
Copy path79.TernaryExpressionToTree.cs
File metadata and controls
130 lines (115 loc) · 3.14 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
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
/**given a string that represents a ternary expression,
parse it into a tree comprised of Node objects and
return an Expression object that contains the pointer to the root node of the tree.
example: given string "a?b?c:d:e"
output: tree as follows
a
bc
de
Solution:
Scan the string in reverse order, use two int to count how many qustions mark and colon
have been scanned.
for each char in the string, if it's a question mark, increment the count;
if it's a colon, increment the count;
if it's not question mark and colon, it must be a variable, so construct a node using
current variable.then we check if the number of question mark is 1
&& if the number of colon is greater than 1.
if it is, then we pop the stack twice, construct two previous node.
Use current node as root, linke the root to the two poped node.
If it is not, we just push current node to the stack.
After the loop, we pop the stack and construct an expression using the poped node.
*/
using System;
using System.Collections.Generic;
using System.Collections;
namespace TernaryExpressionToTree
{
class Node{
public char variableName{ get; set;}
public Node left{ get; set;}
public Node right{ get; set;}
public Node(char c){
this.variableName = c;
this.left = null;
this.right = null;
}
}
class Expression{
public Node root{ get; set;}
public Expression(Node n){
this.root = n;
}
}
class Finder{
public static Expression Convert(string s){ //Convert Method
Stack<Node> st = new Stack<Node> ();
int CountQ = 0;
int CountC = 0;
for (int i = s.Length - 1; i >= 0; i--) {
if (s [i] == ':')
CountC++;
else if (s [i] == '?')
CountQ++;
else {
Node curr = new Node (s [i]);
if (CountQ == 1 && CountC >= 1) {
Node pre1 = st.Pop ();
Node pre2 = st.Pop ();
curr.left = pre1;
curr.right = pre2;
st.Push (curr);
CountQ--;
CountC--;
} else
st.Push (curr);
}
}
Node root = st.Pop ();
Expression e = new Expression (root);
return e;
}
}
//helper class to print and test the result
class TreePrint{
public static void Swap<T> (ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
public static void PrintLevel(Node root) //use two queues
{
if (root == null)
return;
Queue<Node> currentLevel = new Queue<Node> ();
Queue<Node> nextLevel = new Queue<Node> ();
currentLevel.Enqueue (root);
while (currentLevel.Count != 0) {
Node currentNode = currentLevel.Peek ();
currentLevel.Dequeue ();
if (currentNode != null) {
Console.Write (currentNode.variableName + " ");
nextLevel.Enqueue (currentNode.left);
nextLevel.Enqueue (currentNode.right);
}
if (currentLevel.Count == 0) {
Console.WriteLine ("");
Swap (ref currentLevel, ref nextLevel);
}
}
}
}
class MainClass
{
public static void Main (string[] args)
{
Expression e = Finder.Convert ("a?b?c:d:e");
TreePrint.PrintLevel (e.root);
Expression e1 = Finder.Convert ("a?b:c?d:e");
TreePrint.PrintLevel (e1.root);
Expression e2 = Finder.Convert ("a?b?c:d?e:f:g");
TreePrint.PrintLevel (e2.root);
}
}
}