表达式树是树结构的一个经典应用,常用于编译器的设计。
在表达式树中,叶子通常是常数值或者变量名,统称为操作数(operands)。而其他非叶子结点则包含各种操作符(operators)。
在不同的词法解析中,表达式树的分支设计也不同。对于简单的诸如表达式求解而言,表达式树往往采用二叉树。这是因为每一个操作符正好对应两个操作数。
而在包含一元操作符(e.g自增自减操作符)或三元操作符的复杂情况下,每个操作符结点拥有的不再是两个孩子结点,此时二叉树便显得不太适用。
在此处的简单实现,我们只考虑适合二叉树的表达式求解,其余情况请自行翻阅编译原理相关书籍。当然,无论是哪种情况,本质的算法思想是相同的。
0.构造表达式树
要构造一个表达式树,首先要将一般的中缀表达式(infix)转换成逆波兰式(即后缀表达式,postfix)。
PS:关于逆波兰式以及infix和postfix的转换,请参考我之前的一篇文章http://blog.kingsamchen.com/archives/637
对于中缀表达式(a+b)*(c*(d+e)),其逆波兰式为ab+cde+**,我们现在结合这个表达式,阐述利用逆波兰式构造表达式树的一般步骤。
在构建表达式树时,我们需要一个堆栈,用于保存树或者结点的指针,记这个堆栈为S。
我们对逆波兰式进行遍历,如果碰到的是
(1)操作数,那么则建立一个叶子结点,数据为操作数,并将结点指针保存到堆栈中
(2)操作符,那么从堆栈中弹出两个指针,分别为p2和p1.建立一个结点,内容为操作符,然后分别将结点的左右孩子设置为p1和p2,最后将结点指针压入栈
一直重复以上步骤即可。最后堆栈中保存的即是表达式树的根结点指针。
对于中缀表达式(a+b)*(c*(d+e)),其逆波兰式为ab+cde+**,我们现在结合这个表达式,利用图示展示构建过程
1.表达式树的求值
表达式树的求值非常简单,只需要在后序遍历的基础上进行改进即可。
我们要对一个结点进行求值,那么我们需要得到左子树的值和右子树的值,这是递归思想的基准。
而当我们遍历到叶子结点,即操作数结点时,我们返回结点的自身的值。
2.代码实现
为了简化实现,实现中结点的数据类型设置为int型,且假设每个操作数只有一位数。
由于操作数只出现在叶子结点,所以非叶子结点的数据都被解释为操作符,实现中为字符类型
PS:如下代码只是为了说明表达式树建立和求值的算法本质,要设计完善的表达式树,还需要做大量的细节工作。
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
struct
node
{
int
data
;
node*
pLeftChild
;
node*
pRightChild
;
}
;
#include "stdafx.h"
#include <vector>
#include <cassert>
#include "exptree.h"
#define pop(x) x.back();\
x
.
pop_back
(
)
;
node*
ConstructExpTree
(
const
std
::
wstring
&
postfixExp
)
{
std
::
vector
<
node*
>
nodeStack
;
for
(
std
::
wstring
::
const_iterator
it
=
postfixExp
.
begin
(
)
;
it
!=
postfixExp
.
end
(
)
;
++
it
)
{
// operands
if
(
L
'0'
<=
*
it
&&
*
it
<=
L
'9'
)
{
node*
pNode
=
CreateTree
(
*
it
-
L
'0'
,
NULL
,
NULL
)
;
nodeStack
.
push_back
(
pNode
)
;
}
else
{
// forms a new tree
node*
pRight
=
pop
(
nodeStack
)
;
node*
pLeft
=
pop
(
nodeStack
)
;
node*
pNewTree
=
CreateTree
(
*
it
,
pLeft
,
pRight
)
;
nodeStack
.
push_back
(
pNewTree
)
;
}
}
node*
pRoot
=
pop
(
nodeStack
)
;
return
pRoot
;
}
int
Eval
(
const
node*
pRoot
)
{
if
(
NULL
==
pRoot
->
pLeftChild
&&
NULL
==
pRoot
->
pRightChild
)
{
return
pRoot
->
data
;
}
// shoulde be refactored if wanna support more operations
// cos the Calculate will not only evaluate binary operators
// but also unary or ternary operators
int
num1
=
Eval
(
pRoot
->
pLeftChild
)
;
int
num2
=
Eval
(
pRoot
->
pRightChild
)
;
return
Calculate
(
num1
,
num2
,
pRoot
->
data
)
;
}
// only for dump
void
Visit
(
const
node*
pRoot
)
{
if
(
NULL
==
pRoot
)
{
return
;
}
Visit
(
pRoot
->
pLeftChild
)
;
// will invalid if number is euqal to or great than 10
if
(
pRoot
->
data
<
10
)
{
wprintf
(
L
"%d"
,
pRoot
->
data
)
;
}
else
{
wprintf
(
L
"%c"
,
pRoot
->
data
)
;
}
Visit
(
pRoot
->
pRightChild
)
;
}
node*
CreateTree
(
int
data
,
const
node*
pLeftChild
,
const
node*
pRightChild
)
{
node*
p
=
new
node
;
assert
(
p
!=
NULL
)
;
p
->
data
=
data
;
p
->
pLeftChild
=
const_cast
<
node*
>
(
pLeftChild
)
;
p
->
pRightChild
=
const_cast
<
node*
>
(
pRightChild
)
;
return
p
;
}
void
DestroyExpTree
(
node*
&
pRoot
)
{
if
(
NULL
==
pRoot
)
{
return
;
}
node*
pLeft
=
pRoot
->
pLeftChild
;
node*
pRight
=
pRoot
->
pRightChild
;
delete
pRoot
;
pRoot
=
NULL
;
DestroyExpTree
(
pLeft
)
;
DestroyExpTree
(
pRight
)
;
}
std
::
wstring
InfixToPostfix
(
const
wchar_t*
pszInfixExp
)
{
std
::
wstring
postfixExp
;
std
::
vector
<
wchar_t
>
stackOp
;
for
(
const
wchar_t*
p
=
pszInfixExp
;
*
p
!=
NULL
;
++
p
)
{
if
(
L
'0'
<=
*
p
&&
*
p
<=
L
'9'
)
{
postfixExp
.
push_back
(
*
p
)
;
}
else
// operators
{
if
(
L
'('
==
*
p
)
{
stackOp
.
push_back
(
*
p
)
;
}
else
if
(
L
')'
==
*
p
)
{
do
{
wchar_t
op
=
pop
(
stackOp
)
;
// parentheses own the most higher priority in RPN
// right parenthesis must match the left one
if
(
L
'('
==
op
)
{
break
;
}
else
{
postfixExp
.
push_back
(
op
)
;
}
}
while
(
true
)
;
}
else
// operators except ( & )
{
do
{
if
(
stackOp
.
empty
(
)
||
L
'('
==
stackOp
.
back
(
)
||
GetPriority
(
*
p
)
>
GetPriority
(
stackOp
.
back
(
)
)
)
{
stackOp
.
push_back
(
*
p
)
;
break
;
}
else
{
wchar_t
op
=
pop
(
stackOp
)
;
postfixExp
.
push_back
(
op
)
;
}
}
while
(
true
)
;
}
}
}
// output remaining operators in stack
while
(
!
stackOp
.
empty
(
)
)
{
wchar_t
op
=
pop
(
stackOp
)
;
postfixExp
.
push_back
(
op
)
;
}
return
postfixExp
;
}
int
GetPriority
(
wchar_t
op
)
{
int
priority
=
-
1
;
switch
(
op
)
{
case
L
'+'
:
case
L
'-'
:
priority
=
1
;
break
;
case
L
'*'
:
case
L
'/'
:
priority
=
2
;
break
;
case
L
'('
:
case
L
')'
:
priority
=
3
;
break
;
default
:
break
;
}
assert
(
priority
!=
-
1
)
;
return
priority
;
}
int
Calculate
(
int
num
,
int
num2
,
wchar_t
op
)
{
int
ret
=
INT_MIN
;
switch
(
op
)
{
case
L
'+'
:
ret
=
(
num
+
num2
)
;
break
;
case
L
'-'
:
ret
=
(
num
-
num2
)
;
break
;
case
L
'*'
:
ret
=
(
num *
num2
)
;
break
;
case
L
'/'
:
ret
=
(
num
/
num2
)
;
break
;
}
assert
(
ret
!=
INT_MIN
)
;
return
ret
;
}
|