480 likes | 795 Views
ELLIS HOROWITZ. SARTAJ SAHNI. DINESH MEHTA. Advance Data Structure Review of Chapter 3. 張啟中. Stack. An ordered list Insertions and deletions are made at one end, called top Last-In-First-Out (LIFO) list. Abstract Data Type for stack. template <class KeyType> class Stack {
E N D
Stack • An ordered list • Insertions and deletions are made at one end, called top • Last-In-First-Out (LIFO) list
Abstract Data Type for stack template <class KeyType> 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 voidAdd(constKeyType &item); //if (IsFull(stack)) then StackFull() //else insert item into top of stack Boolean IsEmpty(); //if number of elements in stack is 0,returnTRUE //else return FALSE KeyType* Delete(KeyType &); //if (IsEmpty()), then return 0 //else remove and return the pointer to the top of the stack };
Representation: Stack • Definition private: inttop; KeyType *stack; intMaxSize; • Implementation of Stack template <class KeyType> Stack<KeyType>::Stack(int MaxStackSize):MaxSize(MaxStackSize) { stack= new KeyType[MaxSize]; top = -1; }
MaxSize - 1 k 2 1 k 0 Top Items Stack Implementation Based on Array
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
Example • System stack • Checking for Balanced Braces • Recognizing palindromes • Evaluation of Expressions • A Mazing problem • HTML Parser
previous frame pointer fp return address a1 local variables fp previous frame pointer previous frame pointer return address main return address main Example: System stack
Stack Example: Checking for Balanced Braces 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 "{". abc{defg{ijk}{l{mn}}op}rr abc{def}}{ghij{kl}m
Input string Stack as algorithm executes {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 { { { { {ab}c} 1. Push '{' 2. Pop Stack empty when last '}' ==> not balanced {
X Example Stack: Recognizing palindromes A string is called a palindrome if it is the same when reading forward and backward. 為了簡化問題起見,此處我們假設字串中含有單一個 字元 $,此字元區分字串為兩半部,如下圖所示: a b c d e f $ f e d k b a a b c d e f $ f e d c b a YES NO
b c d e f $ f e d c b a c d e f $ f e d c b a top b a a top 作法示範:假定字串為 a b c d e f $ f e d c b a Step 1:將 $ 之前的字元依序 push 至堆疊中
d e f $ f e d c b a e f $ f e d c b a f $ f e d c b a e d c b a top top d c b a top c b a $ f e d c b a f e d c b a Step 2:碰到 $ 字元 ,捨棄 $ 字元後, 不再 push 其後的字 元至 stack 中,而進 入比較階段。 top top f e d c b a f e d c b a
f e d c b a e d c b a d c b a top f e d c b a top e d c b a top d c b a c b a b a a top c b a top b a top a Step 3:在這個階段,我們比較 stack 中由上而下的字元和 $ 之後 剩餘的字元,如下所示:
a k b a c b a top top c b a c b a top a top Step 4:若比較之後,下列三個條件都成立: (1) 相比的字元一樣; (2) stack 變成空的; (3) 沒有剩餘的字元; 則此字串是一個 palindrome,否則不是一個 palindrome。 YES NO NO NO
N NW NE W E SE SW 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 9 4 8 5 7 6 7 6 8 5 9 4 10 3 11 2 1 0 0 1 0 0 0 1 1 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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 9 3 6 NE 8 3 5 E 3 1 4 E 2 1 3 E 1 2 2 E 1 1 SE 0 Backtracking row col dir
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 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 9 3 6 E 8 3 5 E 3 1 4 E 2 1 3 E 1 2 2 E 1 1 SE 0 row col dir
Example Stack: Evaluation of Expressions • Infix, Postfix, and Prefix Expressions • Evaluation of Postfix Expressions • Conversion from Infix to Postfix Expressions i n f i x p r e f i x p o s t f i x a + b + a b a b + - - - a b a b a b * * * a b a b a b / / / a b a b a b
6 B / C 7 B / C * D 14 10 A - ( B / C * D ) Postfix Expressions 2 * ( 3 + 4 ) = 14 2 * 3 + 4 = 10 A - ( B / C * D ) 2 3 4 + * 2 3 * 4 + A B C / D * -
2 3 4 + * 4 + * * 7 2 3 2 3 4 + * + * 4 3 2 2 14 2 * ( 3 + 4 ) = 14 2 3 4 + * 7 14
2 * 3 + 4 = 10 2 3 * 4 + 2 3 * 4 + * 4 + + 6 4 6 3 2 10 3 * 4 + 4 + 2 6 10
Infix Convert to Postfix 從前面的例子,我們知道利用堆疊可以很容易地計算 後置算式。因此,計算中置算式可以分為底下的兩個 步驟來執行: 1. 將中置算式轉換為後置算式 2. 用堆疊來計算所得的後置算式
6 10 7 14 【範例】 2 * 3 + 4 = 10 2 3 * 4 + 2 * ( 3 + 4 ) = 14 2 3 4 + *
Infix 與 Postfix 比較 • 運算元的次序在中置算式和後置算式是完全相同的。 • 在中置算式中,如果運算元X在運算子OP 之前, 那麼,在後置算式中,運算元X 也會在運算子OP之前。 • 所有括號在轉換後均被消除。 • 運算子在後置算式中的出現順序是依照其在中置算式 的運算順序而定。
括號內算式先做 • 先乘除後加減 • 由左至右運算 中置算式的運算法則 Example: Infix convert to Postfix 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 / +
運算元 A + B 運算子 中置算式轉換為後置算式的演算法 1. 當碰到運算元時,直接輸出此運算元。 2. 當碰到左括號時,將其 push 至堆疊中。 3. 當碰到右括號時,將堆疊中的運算子 pop 出來並輸出,直到碰到左括號為止,然後再將此左括號 pop 掉。
中置算式轉換為後置算式的演算法 4. 當碰到運算子時 依序將堆疊中運算優先次序較高或相同的運算子 pop 出來並輸出,直到遇到下列情形之一 (1) 碰到左括號 (2) 碰到優先次序較低的運算子 (3) 堆疊變為空 最後將此運算子 push 至堆疊中。 5. 當輸入字串全部處理完時,將堆疊中所剩餘的運算子逐一地 pop 出來並輸出,直到堆疊變為空為止。
【範例】 A - ( B + C * D ) / E A B C D * + E / - Ch Stack S ( bottom to top) PE rule A A 1 - - A 4 ( - ( A 2 B - ( A B 1 + - ( + A B 4 C - ( + A B C 1 * - ( + * A B C 4 D - ( + * A B C D 1
) - ( + 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 * + E 1 - A B C D * + E / 5 A B C D * + E / - 5
<html> <head> <title>The Title</title> </head> <body> <h1>網頁內容</h1> </body> </html> <title> <head> <html> Example Stack: HTML Parser <html> 將 <head> 推入堆疊前,先檢查堆疊中放的標籤為 <html>,所以知道<head> 父節點為 <html>。 <head> <title> The Title 遇到 </title> 到堆疊中 pop 出 <title>,所以知道 The Title 是隸屬於 <title> 這個標籤。 </title>
1 …. n Amount of Permutations of Stack • Please see book p130, exercises 2 • 從右邊依序輸入 1…n 的數,問總共有幾種輸出情形?有那幾種情形不可能出現?
Amount of Permutations of Stack • 當 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 • 一般化的解 • 列出遞迴方程式 bn = b0bn-1 + b1bn-2 + …… + bn-2b1+ bn-1b0 • 此遞迴方程式為非齊次式,用迭代法不好解,用生成函數法比較好解,請參閱離散數學的書。 • 此解我們於計算二元樹的個數還會看到。