Exercise Section
Choice Questions
first question .
A recursive algorithm for solving a problem and its corresponding non-recursive algorithm solution ()
A. recursive algorithms are usually efficient some B. non-recursive algorithms are usually more efficient
C. same as D. can't compare the second question
You need to use queues as secondary storage when performing () operations
A. finding hash (hash) Table B. Breadth First Search chart
C. pre-order (root) traverse binary tree D. Depth First Search graph third question
Known operators include ' + '-' '/' (') '. When converting infix expression a+b-a((c+d)/e-f) +g into an equivalent suffix expression ab+acd+e/f-*-g+ , the stack is used to store operators that are temporarily unable to determine the order of operations. If the stack is initially empty, the maximum number of operators that colleagues save on the stack in the conversion process is ()
A. 5 B. 7
C. 8 D. Fourth question
Assuming that the stack is initially empty, in the process of converting infix expression a/b+ (c*d-e*f)/g into an equivalent dog-chasing expression, when scanning to F, the elements in the stack are sequentially ()
A. + (*- B. + (-* C. /+ (*-* D. /+-* Answer The first question
In general, the recursive algorithm in the actual implementation of the computer contains a lot of repetitive calculation, so the efficiency is lower the second question
The breadth-first search for graphs is similar to the sequence traversal of a tree, which requires the help of the third problem of the queue
The application of the stack in the transformation of the pig expression into the suffix expression is studied.
To convert infix expression a+b-a* (c+d)/e-f) +g to the corresponding suffix expression, we need to change the stack according to the precedence of operator <op>, we use ICP to represent the operator currently scanned. The priority of CH , which is the priority of the operator after the stack is the ISP, the precedence of the operator is shown in the following table:
(ISP) is the number of priority (in stack priority) within the stack, and ICP is the top of the stack (in coming priority).
operator |
# |
( |
*,/ |
+,- |
) |
Isp |
0 |
1 |
5 |
3 |
6 |
Icp |
0 |
6 |
4 |
2 |
1 |
We add ' # ' after the expression, which indicates the end of the expression, and the exact exchange process is as follows:
Steps |
Scan Items |
Item Type |
Action |
content inside the stack |
Output |
Isp |
0 |
1 |
5 |
3 |
6 |
Icp |
0 |
6 |
4 |
2 |
1 |
0 |
|
|
#进栈, read the next symbol |
# |
|
1 |
A |
Number of operands |
Direct output |
# |
A |
2 |
+ |
Operator |
ISP (' # ') < ICP (' + '), into the stack |
#+ |
|
3 |
B |
Number of operands |
Direct output |
#+ |
B |
4 |
- |
Operator |
ISP (' + ') > ICP ('-'), back up the stack and output |
# |
+ |
5 |
|
|
ISP (' # ') < ICP ('-'), into the stack |
#- |
|
6 |
A |
Number of operands |
Direct output |
#- |
A |
7 |
* |
Operator |
ISP ('-') < ICP (' * ') into the stack |
#-* |
|
8 |
( |
Operator |
ISP (' * ') < ICP (' ('), into the stack |
#-*( |
|
9 |
( |
Operator |
ISP (' (') < ICP ('), stack |
#-*(( |
|
10 |
C |
Number of operands |
Direct output |
#-*(( |
C |
11 |
+ |
Operator |
ISP (' (') < ICP (' + '), into the stack |
#-*((+ |
|
12 |
D |
Number of operands |
Direct output |
#-*((+ |
D |
13 |
) |
Operator |
ISP (' + ') > ICP (') '), back up the stack and output |
#-*(( |
+ |
14 |
|
|
ISP (' (') = = ICP (') '), direct back stack |
#-*( |
|
15 |
/ |
Operator |
ISP (' (') < ICP ('/'), into the stack |
#-*(/ |
|
16 |
E |
Number of operands |
Direct output |
#-*(/ |
E |
17 |
- |
Operator |
ISP ('/') > ICP ('-'), back up the stack and output |
#-*( |
/ |
18 |
|
|
ISP (' (') < ICP ('-'), into the stack |
#-*(- |
|
19 |
F |
Number of operands |
Direct output |
#-*(- |
F |
20 |
) |
Operator |
ISP ('-') > ICP (') '), back up the stack and output |
#-*( |
- |
21st |
|
|
ISP (' (') = = ICP (') '), direct back stack |
#-* |
|
22 |
+ |
Operator |
ISP (' * ') > ICP (' + '), exit stack and output |
#- |
* |
23 |
|
|
ISP ('-') > ICP (' + '), back up the stack and output |
# |
- |
24 |
|
|
ISP (' # ') < ICP (' + '), into the stack |
#+ |
|
25 |
G |
Number of operands |
Direct output |
#+ |
G |
26 |
# |
Operator |
ISP (' + ') > ICP (' # '), exit stack and output |
|
# |
27 |
|
|
ISP (' # ') = = ICP (' # '), exit stack, end |
|
|
question Fourth .
The algorithm for converting infix expressions to suffix expressions is as follows:
Start from left to right to scan infix expression;
When the number is encountered, the suffix expression is added;
When an operator is encountered:
A. If it is ' (', into the stack;
B. If ') ', the operator in the stack is added to the suffix expression at a time until the ' (', delete from the stack ' (';
C. In the case of an operator other than parentheses, when its precedence is higher than the exception ' (' unexpected stack-top operator, go directly to the stack. Otherwise, starting at the top of the stack, an operator is ejected at a time that is higher than the current processing operator and has an equal precedence, until one has a lower priority or encounters a left parenthesis position.
When the scanned infix expression ends, all operators in the stack are added to the suffix expression at a time.
Pending Sequence |
Stack |
suffix expression |
Current Scan element |
Action |
a/b+ (c*d-e*f)/g |
|
|
A |
A add suffix expression |
/b+ (c*d-e*f)/g |
|
A |
/ |
/Into the stack |
B + (c*d-e*f)/g |
/ |
A |
B |
b add suffix expression |
+ (c*d-e*f)/g |
/ |
Ab |
+ |
+ priority lower than top of stack/, popup/ |
+ (c*d-e*f)/g |
|
ab/ |
+ |
+ Into Stack |
(c*d-e*f)/g |
+ |
ab/ |
( |
(Into the Stack |
c*d-e*f)/g |
+( |
ab/ |
C |
C-add suffix expression |
*d-e*f)/g |
+( |
Ab/c |
* |
Stack top for (, * into stack |
d-e*f)/g |
+(* |
Ab/c |
D |
d Add suffix expression |
-e*f)/g |
+(* |
Ab/cd |
- |
-priority is lower than the top of the stack, pop-up |
-e*f)/g |
+( |
ab/cd* |
- |
Stack top for (,-into stack |
e*f)/g |
+(- |
ab/cd* |
E |
E add suffix expression |
*f)/g |
+(- |
Ab/cd*e |
* |
* Priority higher than the top of the stack-,* |
f)/g |
+(-* |
Ab/cd*e |
F |
F Add suffix expression |
)/g |
+(-* |
Ab/cd*ef |
) |
Add the previous symbol in the stack to the expression |
/g |
+ |
ab/cd*ef*- |
/ |
/priority higher than the top of the stack +,/ |
G |
+/ |
ab/cd*ef*- |
G |
G add suffix expression |
|
+/ |
Ab/cd*ef*-g |
|
The scan is complete, the operator joins the expression at once |
|
|
ab/cd*ef*-g/+ |
|
Complete |
So, when you scan to F, the elements of the stack are once + (-*, select B).
Again give the infix expression conversion to prefix or suffix expression of manual practice, with the above given infix expression as an example:
Step one: Parentheses all units of operation by precedence of operators
The formula becomes: ((A/b) + (((c*d)-(e*f)/g)
Part II: Converts to a prefix or suffix expression.
Prefix: Move the operation symbol to the corresponding bracket before it becomes: + (/(AB)/(-(* (CD) * (EF)) g)
Remove the parentheses: the +/AB/-*CD*EFG prefix equation appears
Suffix: Moves the operator to the back of the corresponding bracket, then becomes: ((AB)/((CD) * (EF) *-g/) +
Remove the parentheses: the ab/cd*ef*-g/+ suffix appears
This method is much quicker than the previous one when the topic requires a prefix or suffix expression to be asked directly.