A. 栈ADT及实现
一、栈
- 栈(stack)是受限的序列:只能在栈顶(top)插入和删除;栈底(bottom)为盲端
- 基本接口:size()/empty(),入栈push(),出栈pop(),查顶top()
- LIFO:last in, first out先进后出
二、实现
三、作业
- The stack S is initially empty. After the following operations, the elements from the top of the stack to the bottom of the stack are:栈S初始为空,进行以下操作后从栈顶到栈底的元素依次为:S.push(5);S.push(4);S.pop();S.push(2);S.pop();S.pop();S.push(1) 1
B. 调用栈*
栈的典型实例
C. 进制转换
一、应用
- 短除法:整商+余数
- 难点+算法:自低而高得到的位数,如何自高而低输出?
· 向量:扩容策略?
· 列表:多数接口闲置?
· 栈:即满足需求,亦有效控制计算成本
二、实现
三、作业
- The binary number of hexadecimal number AF is:
16进制数AF的二进制为:10101111
D. 括号匹配
一、构思
- 尝试(由外而内):只考虑了充分性,必要性!!!
- 构思(由内而外):顺序扫描表达式,用栈记录已扫描部分,凡遇"(“则进栈,遇”)"则出栈
二、实现
扩展:可实现多类符号
三、作业
- When an open parenthesis is scanned:当扫描到一个左括号时:Enter the stack 进栈
E. 栈混洗
一、混洗
- 栈混洗StackPermutation:将A的顶元素弹出并压入S,或将S的顶元素弹出并压入B,若一系列操作后,A中元素全部转入B中,则称为A的一个栈混洗
- 混洗总数:SP(n) ≤ n!
- 计数:catalan(n)
二、甄别
- 检测禁形:对于1 ≤ i < j < k ≤ n,其中[…, k, …, i, …, j, …>必非栈混洗([为栈底,>为栈顶)
- 甄别算法
· O(n3):不存在禁形的栈混洗序列,一定是栈混洗
· O(n2):[ p1, p2, p3, …, pn>的栈混洗,当且仅当对于任意 i < j,不含模式[ …, j+1, …, i, …, j, …>
· O(n):直接借助栈A、B和S,模拟混洗过程。每次s.pop()之前,检测s是否已空,或需弹出的元素在s中,却非顶元素
三、作业
- Is {3,1,2,4} a stack shuffle of {1,2,3,4}?{3,1,2,4}是否是{1,2,3,4}的栈混洗?No 不是
F. 中缀表达式求值
一、问题与构思
- 表达式求值应用
- 减而治之:优先级高的局部执行计算,并被代以其数值,运算符减少,直至得到最终结果
- 延迟缓冲:为处理某一前缀,必须提前预读并分析更长的前缀
二、算法
- 主算法:
- 制表定义运算顺序
· 栈顶运算符优先级更低:计算推迟,当前运算符进栈
· ……………………更高:执行相应的计算
· ……………………相等:(右括号或尾部哨兵元素)脱括号并接收下一个字符
三、作业
- When does the actual calculation happen?什么时候进行实际的运算?The current operator has a lower priority than the operator on the top of the stack当前的操作符比栈顶的操作符优先级低
G. 逆波兰RPN表达式
一、定义与求值
- 逆波兰表达式(ReversePolishNotation):在由运算符(operator)和操作数(operand)组成的表达式中,不使用括号(parenthesis-free),即可表示带优先级的运算关系
- 较中缀式(infix),RPN亦称后缀式(postfix)
- 栈式求值:
二、转换
- 手动转换
- 添加括号
- 以运算符替换右括号,清除左括号(运算符右移;清除括号)
- 自动转换
三、作业
- When evaluating an inverse Polish expression, when does an actual calculation occur?逆波兰表达式的求值算法中,什么时候进行一次实际的运算?Each encounters a new operator每遇到一个新的操作符
- Reverse Polish Expression of 1 5 +
逆波兰表达式 1 5 + 6 - 在本节中,我们学过了如何将中缀表达式转化为逆波兰表达式,那么反过来,思考一下如何将逆波兰表达式还原为中缀表达式呢?试将下列逆波兰表达式还原为中缀表达式:1 2 + 3 4 ^ * (1+2)*3^4
H. 队列ADT及实现
一、队列接口
- 队列(queue):也是受限的线性序列,先进先出(FIFO),后进后出(LILO)
- 只能在队尾插入(查询): enqueue() / rear()
- 只能在对头删除(查询): dequeue() / front()
二、实现
应用*
- 队列应用:资源循环分配
- 双栈当队
- steap + queap
1. steap = stack + Heap = push + pop + getMax
2. queap = queue + heap = enqueue + dequeue + getMax - 直方图内最大矩形
测验
- The stack is initially empty and goes through the following operations in sequence:栈初始为空,依次经过以下操作:push(5);push(8);pop();push(5);top();push(1);push(3);pop();pop();push(2);At this point from the top of the stack to the bottom of the stack:此时从栈顶到栈底依次为:2,5,5
- The following is a vector-based implementation of the queue (for the interface and implementation of vectors refer to Chapter 2):下面是队列的一种基于向量的实现(向量的接口和实现可参考第2章):For this queue of size n, the worst-case complexity of enqueue() and dequeue() is:对于规模为n的该队列,enqueue()和dequeue()的最坏时间复杂度分别为:O(n),O(1) 【The queue enqueue() thus implemented needs to move n elements in successors.如此实现的队列enqueue()需要移动后继中的n个元素。】
- The following is some kind of data structure, which is implemented with two stacks and supports adding and deleting elements:下面是某种数据结构,它用2个栈实现,支持添加、删除元素的操作:When T is an integer, the following operations are performed sequentially on the initially empty data structure:当T为整数时,对初始为空的以上数据结构依次执行以下操作:The only remaining elements in XXX at this time are:此时XXX中唯一剩下的元素是:11 【The data structure is a queue. The complexity of enqueue() is O(1), and the worst time complexity of a single dequeue() is Ω(n) but the apportionment complexity is O(1)该数据结构即为队列。enqueue()的复杂度为O(1),单次dequeue()的最坏时间复杂度为Ω(n) 但分摊复杂度为O(1)】
- The following is some kind of data structure, which is implemented with two queues and supports adding and deleting elements:下面是某种数据结构,它用2个队列实现,支持添加、删除元素的操作:When T is an integer, the following operations are performed sequentially on the initially empty data structure:当T为整数时,对初始为空的以上数据结构依次执行以下操作:The only remaining elements in XXX at this time are:此时XXX中唯一剩下的元素是:2 【The data structure is a stack, but when the size is n, the time complexity of pop() is as high as Ω(n) 该数据结构即为栈,只不过规模为n时,其pop()的时间复杂度高达Ω(n) 】
- Read the function below (where 1≤x, y≤16) and indicate its function:阅读下面函数(其中1≤x, y≤16),指出其功能:Prints the y-ary representation of a decimal integer x. 打印十进制整数x的y进制表示 【This function is a recursive version of the conversion of number systems该函数是进制转换的递归版】
- The operations on transfer stack S when sequence {2, 3, 5, 7, 11} is shuffled to get {3, 5, 2, 11, 7}对序列{2, 3, 5, 7, 11}进行栈混洗得到{3, 5, 2, 11, 7}的过程中用于中转的栈S进行的操作是: push, push, pop, push, pop, pop, push, push pop, pop
- Which of the following sequences must not be a stack shuffle for {1,2,3… i…j…k…n}:下列哪一个序列一定不是{1,2,3… i…j…k…n}的栈混洗:{… k…i…j…} 【To generate {… k…i…j…} from stack shuffling, when k enters the target stack, i,j must be in the middle stack and j is relative to i above the stack. From “first in last out”, j must enter the target stack before i, so it is impossible to generate {… k…i…j…}. (It is also possible to consider the special case of i,j,k adjacent)若要通过栈混洗产生{… k…i…j…},则k进入目标栈时i,j必然在中间栈且j相对于i在栈的上方。由“先进后出”,j必定先于i进入目标栈,故不可能产生{… k…i…j…}。(也可以考虑i,j,k相邻的特殊情形来解答)】
- which of the following are equal以下几个量中相等的是: ②③ 【The push and pop in the stack shuffle correspond to the “(” and “)” in the parentheses match, so they are equal in number. (1 is 2^n, 2 is Catalan(n), 3 is Catalan(n), 4 ≤n)栈混洗中的push和pop分别对应于括号匹配中的”( ”和”) ”,故它们数量相等.(①为2^n,②为 Catalan(n),③ 为Catalan(n),④≤n)】
① Numbers of different n-bit binary 不同的n位二进制数个数
② The number of legal parentheses matched by n pairs of parenthesesn对小括号所能构成的合法括号匹配个数
③ Numbers of different stack shuffle for {1, 2 … n}{1, 2 … n}的不同栈混洗个数
④ Number of operator stack push operations during infix expression evaluation with n operators含n个运算符的中缀表达式求值过程中运算符栈push操作的次数 - In the evaluation of infix expressions, the stack of operands from the top of the stack to the bottom of the stack is:在中缀表达式求值中,某时刻运算数栈从栈顶到栈底依次为: 6,2,1
The stack of operators from the top of the stack to the bottom of the stack is:运算符栈从栈顶到栈底依次为:*,+,(
The remaining pending expression is:剩下的待处理表达式为 : ) / ( 4 * 5 - 7 )
In the following process, the stacking order of operators and the final calculation result are在接下来的过程中运算符的入栈顺序以及最终的计算结果分别为: / ( * The final result is 1最终结果为1 - (1 + 2 * 3! ) / (4 * 5 - 7 )The inverse Polish expression is (The integers in the expression are all one-digit numbers)(1 + 2 * 3! ) / (4 * 5 - 7 )的逆波兰表达式为(表达式中的整数都是一位数): 1 2 3! * + 4 5 + 7 - / 【The inverse Polish expression for A+B is AB+A+B的逆波兰表达式为AB+】