Download presentation
Presentation is loading. Please wait.
1
ELLIS HOROWITZ
2
SARTAJ SAHNI
3
DINESH MEHTA
4
Advance Data Structure Review of Chapter 3 張啟中
5
Stack An ordered list Insertions and deletions are made at one end, called top Last-In-First-Out (LIFO) list
6
Abstract Data Type for stack template class Stack { //objects: a finite ordered list with zero or more elements. //for all stack in Stack, item in element, MaxStackSize in positive integer public: Stack(int MaxStackSize=DefaultSize); //create an empty stack whose maximum size is MaxStackSize Boolean IsFull(); //if (number of elements in stack == MaxStackSize) return TRUE return FALSE void Add(const KeyType &item); //if (IsFull(stack)) then StackFull() //else insert item into top of stack Boolean IsEmpty(); //if number of elements in stack is 0, return TRUE //else return FALSE KeyType* Delete(KeyType &); //if (IsEmpty()), then return 0 //else remove and return the pointer to the top of the stack };
7
Representation: Stack Definition private: int top; KeyType *stack; int MaxSize; Implementation of Stack template Stack ::Stack(int MaxStackSize):MaxSize(MaxStackSize) { stack= new KeyType[MaxSize]; top = -1; }
8
0 1 2 MaxSize - 1 k TopItems k Stack Implementation Based on Array
9
Stack 的操作 Implementation of member function IsFull() Implementation of member function IsEmpty() Implementation of member function Add() or push() Implementation of member function Delete() or pop() See textbook p129
10
Example System stack Checking for Balanced Braces Recognizing palindromes Evaluation of Expressions A Mazing problem HTML Parser
11
Example: System stack return address previous frame pointer fp main return address previous frame pointer fp main local variables return address previous frame pointer a1
12
12 Stack Example: Checking for Balanced Braces abc{defg{ijk}{l{mn}}op}rrabc{def}}{ghij{kl}m Requirements for balanced braces: 1. Each time you encounter a "}", it matches an already encountered "{". 2. When you reach the end of the string, you have matched each "{".
13
13 {a{b}c} { {{{{ { 1. Push '{' 2. Push '{' 3. Pop 4. Pop Stack empty ==> balanced {a{bc} { {{{{ { { 1. Push '{' 2. Push '{' 3. Pop Stack not empty ==> not balanced 1. Push '{' 2. Pop Stack empty when last '}' ==> not balanced {ab}c} Input stringStack as algorithm executes
14
14 Example Stack: Recognizing palindromes YES a b c d e f $ f e d c b a NO a b c d e f $ f e d k b a X A string is called a palindrome if it is the same when reading forward and backward. 為了簡化問題起見,此處我們假設字串中含有單一個 字元 $ ,此字元區分字串為兩半部,如下圖所示:
15
15 a b c d e f $ f e d c b a top 作法示範: 假定字串為 a b c d e f $ f e d c b a Step 1: 將 $ 之前的字元依序 push 至堆疊中 baba c d e f $ f e d c b a top
16
16 cbacba d e f $ f e d c b a top dcbadcba e f $ f e d c b a top edcbaedcba f $ f e d c b a top fedcbafedcba $ f e d c b a top fedcbafedcba f e d c b a top Step 2: 碰到 $ 字元 ,捨棄 $ 字元後, 不再 push 其後的字 元至 stack 中,而進 入比較階段。
17
17 Step 3: 在這個階段,我們比較 stack 中由上而下的字元和 $ 之後 剩餘的字元,如下所示: fedcbafedcba f e d c b a top edcbaedcba e d c b a top dcbadcba d c b a top cbacba c b a top baba b a top a a
18
18 Step 4: 若比較之後,下列三個條件都成立: (1) 相比的字元一樣; (2) stack 變成空的; (3) 沒有剩餘的字元; 則此字串是一個 palindrome ,否則不是一個 palindrome 。 a a top cbacba k b a top c b a top cbacba YESNO
19
19 Example Stack: A Mazing Problem 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 0 : open 1 : close S N EW NE SE NW SW
20
20 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1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 6 7 8 9 10 11 rowcoldir 11SE 22E 0 1 2 3 313SE 312E 15 16 BacktrackingBacktracking 17 414E 13E 14E
28
28 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 123456789101112131415 1 2 3 4 5 6 7 8 9 10 11 rowcoldir 11SE 22E 0 1 2 3 312E 15 BacktrackingBacktracking 313SE 16 13E 14E
29
29 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 123456789101112131415 1 2 3 4 5 6 7 8 9 10 11 rowcoldir 11SE 22E 0 1 2 3 35E 8 BacktrackingBacktracking 36NE 9 13E 14E
30
30 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 123456789101112131415 1 2 3 4 5 6 7 8 9 10 11 rowcoldir 11SE 22E 0 1 2 3 35E 8 36E 9 13E 14E
31
31 Example Stack: Evaluation of Expressions _ Infix, Postfix, and Prefix Expressions _ Evaluation of Postfix Expressions _ Conversion from Infix to Postfix Expressions infixprefixpostfix a + b+ a ba b + a b a ba b a b a ba b a b a ba b
32
32 2 3 4 + * 7 14 2 * ( 3 + 4 ) = 14 A ( B / C * D ) 2 3 * 4 + 6 10 A B C / D * B / C B / C * D A ( B / C * D ) 2 * 3 + 4 = 10 Postfix Expressions
33
33 2 3 4 + * 7 14 2 * ( 3 + 4 ) = 14 2 3 4 + * 3 4 + * 2 4 + * 3232 + * 432432 * 7272 14
34
34 2 * 3 + 4 = 10 2 3 * 4 + 6 10 2 3 * 4 + 3 * 4 + 2 * 4 + 3232 4 + 6 + 4646 10
35
35 Infix Convert to Postfix 從前面的例子,我們知道利用堆疊可以很容易地計算 後置算式。因此,計算中置算式可以分為底下的兩個 步驟來執行: 1. 將中置算式轉換為後置算式 2. 用堆疊來計算所得的後置算式
36
36 【範例】 2 * 3 + 4 = 102 3 * 4 + 6 10 2 3 4 + * 7 14 2 * ( 3 + 4 ) = 14
37
37 【範例】
38
38 運算元的次序在中置算式和後置算式是完全相同的。 在中置算式中,如果運算元 X 在運算子 OP 之前, 那麼,在後置算式中,運算元 X 也會在運算子 OP 之前。 所有括號在轉換後均被消除。 運算子在後置算式中的出現順序是依照其在中置算式 的運算順序而定。 Infix 與 Postfix 比較
39
39 中置算式的運算法則 括號內算式先做 先乘除後加減 由左至右運算 A ( B + C * D ) / E A ( ( B + ( C * D ) ) / E ) ) A ( ( B + C D *) / E ) ) A ( B C D * + / E ) ) A B C D * + E / ) A B C D * + E / A B + C * D / E A B ) + ( ( C * D ) / E ) ) A B + ( ( C * D ) / E ) ) A B + ( C D * / E ) ) A B + C D * E / ) A B C D * E / + Example: Infix convert to Postfix
40
40 中置算式轉換為後置算式的演算法 1. 當碰到運算元時,直接輸出此運算元。 2. 當碰到左括號時,將其 push 至堆疊中。 3. 當碰到右括號時,將堆疊中的運算子 pop 出來並輸出, 直到碰到左括號為止,然後再將此左括號 pop 掉。 A + B 運算元 運算子
41
41 4. 當碰到運算子時 依序將堆疊中運算優先次序較高或相同的運算子 pop 出來並輸 出,直到遇到下列情形之一 (1) 碰到左括號 (2) 碰到優先次序較低的運算子 (3) 堆疊變為空 最後將此運算子 push 至堆疊中。 5. 當輸入字串全部處理完時,將堆疊中所剩餘的運算子逐一地 pop 出來並輸出,直到堆疊變為空為止。 中置算式轉換為後置算式的演算法
42
42 【範例】 A ( B + C * D ) / EA B C D * + E / ChStack S ( bottom to top)PErule AA1 --A4 (- (A2 B- (A B1 + - ( +A B4 C - ( +A B C1 * - ( + *A B C4 D - ( + *A B C D1
43
43 ) - ( +A B C D *3 - (A B C D * +3 - A B C D * +3 /- /A B C D * +4 E - /A B C D * + E1 - A B C D * + E /5 A B C D * + E / -5
44
Example Stack: HTML Parser The Title 網頁內容 The Title 遇到 到堆疊中 pop 出 ,所以知道 The Title 是 隸屬於 這個標籤。 將 推入堆疊 前,先檢查堆疊中放 的標籤為 ,所 以知道 父節點 為 。
45
Amount of Permutations of Stack Please see book p130, exercises 2 從右邊依序輸入 1…n 的數,問總共有幾種輸 出情形?有那幾種情形不可能出現? 1 …. n
46
當 n=1 只有 1 種 1 當 n=2 有 2 種 12 及 21 當 n=3 可能的排列組合有 123,132,213,231,312,321 312 不可能出現 (Why?) 所以只有五種 當 n=4 ( 自己練習 ) Amount of Permutations of Stack
47
一般化的解 列出遞迴方程式 b n = b 0 b n-1 + b 1 b n-2 + …… + b n-2 b 1 + b n-1 b 0 此遞迴方程式為非齊次式,用迭代法不好解,用生成函 數法比較好解,請參閱離散數學的書。 此解我們於計算二元樹的個數還會看到。
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.