ELLIS HOROWITZ SARTAJ SAHNI DINESH MEHTA Advance Data

  • Slides: 47
Download presentation
ELLIS HOROWITZ

ELLIS HOROWITZ

SARTAJ SAHNI

SARTAJ SAHNI

DINESH MEHTA

DINESH MEHTA

Advance Data Structure Review of Chapter 3 張啟中

Advance Data Structure Review of Chapter 3 張啟中

Stack n n n An ordered list Insertions and deletions are made at one

Stack n n n 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 Key. Type> class Stack { //objects: a

Abstract Data Type for stack template <class Key. Type> class Stack { //objects: a finite ordered list with zero or more elements. //for all stack in Stack, item in element, Max. Stack. Size in positive integer public: Stack(int Max. Stack. Size=Default. Size); //create an empty stack whose maximum size is Max. Stack. Size Boolean Is. Full(); //if (number of elements in stack == Max. Stack. Size) return TRUE return FALSE void Add(const Key. Type &item); //if (Is. Full(stack)) then Stack. Full() //else insert item into top of stack Boolean Is. Empty(); //if number of elements in stack is 0, return TRUE //else return FALSE Key. Type* Delete(Key. Type &); //if (Is. Empty()), then return 0 //else remove and return the pointer to the top of the stack };

Representation: Stack n Definition private: int top; Key. Type *stack; int Max. Size; n

Representation: Stack n Definition private: int top; Key. Type *stack; int Max. Size; n Implementation of Stack template <class Key. Type> Stack<Key. Type>: : Stack(int Max. Stack. Size): Max. Size(Max. Stack. Size) { stack= new Key. Type[Max. Size]; top = -1; }

Stack Implementation Based on Array Max. Size - 1 k 2 1 k 0

Stack Implementation Based on Array Max. Size - 1 k 2 1 k 0 Top Items

Stack 的操作 n n Implementation of member function Is. Full() Implementation of member function

Stack 的操作 n n Implementation of member function Is. Full() Implementation of member function Is. Empty() Implementation of member function Add() or push() Implementation of member function Delete() or pop() q See textbook p 129

Example n n n System stack Checking for Balanced Braces Recognizing palindromes Evaluation of

Example n n n System stack Checking for Balanced Braces Recognizing palindromes Evaluation of Expressions A Mazing problem HTML Parser

Example: System stack previous frame pointer return address fp a 1 local variables previous

Example: System stack previous frame pointer return address fp a 1 local variables previous frame pointer return address fp main previous frame pointer return address main

Stack Example: Checking for Balanced Braces Requirements for balanced braces: 1. Each time you

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 12

Input string Stack as algorithm executes {a{b}c} { { {a{bc} { { {ab}c} {

Input string Stack as algorithm executes {a{b}c} { { {a{bc} { { {ab}c} { { { 1. Push '{' 2. Push '{' 3. Pop 4. Pop Stack empty ==> balanced 1. Push '{' 2. Push '{' 3. Pop Stack not empty ==> not balanced 1. Push '{' 2. Pop Stack empty when last '}' ==> not balanced 13

Example Stack: Recognizing palindromes A string is called a palindrome if it is the

Example Stack: Recognizing palindromes A string is called a palindrome if it is the same when reading forward and backward. 為了簡化問題起見,此處我們假設字串中含有單一個 字元 $,此字元區分字串為兩半部,如下圖所示: abcdef$fedcba abcdef$fedkba X YES NO 14

作法示範:假定字串為 a b c d e f $ f e d c b a

作法示範:假定字串為 a b c d e f $ f e d c b a Step 1: 將 $ 之前的字元依序 push 至堆疊中 bcdef$fedcba a top cdef$fedcba b a top 15

def$fedcba c b a top $fedcba f e d c b a top f$fedcba

def$fedcba c b a top $fedcba f e d c b a top f$fedcba ef$fedcba d c b a e d c b a top Step 2: 碰到 $ 字元 ,捨棄 $ 字元後, 不再 push 其後的字 元至 stack 中,而進 入比較階段。 top fedcba f e d c b a top 16

Step 3: 在這個階段,我們比較 stack 中由上而下的字元和 $ 之後 剩餘的字元,如下所示: fedcba f e d c b

Step 3: 在這個階段,我們比較 stack 中由上而下的字元和 $ 之後 剩餘的字元,如下所示: fedcba f e d c b a edcba top e d c b a cba c b a dcba top d c b a ba a top b a top 17

Example Stack: A Mazing Problem 0 1 1 0 0 0 1 0 1

Example Stack: A Mazing Problem 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 1 1 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 0 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 14 13 12 E SE E 17 1 4 1 3 2 2 1 1 row col E E NE SE dir 3 4 3 3 16 15 2 1 0 26

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 1 Backtracking 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 14 13 12 E SE E 17 1 4 1 3 2 2 1 1 row col E E E SE dir 3 4 3 3 16 15 2 1 0 27

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 1 Backtracking 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 13 12 SE E 16 1 4 1 3 2 2 1 1 row col E E E SE dir 3 3 3 15 2 1 0 28

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 1 Backtracking 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 3 3 6 5 1 4 1 3 2 2 1 1 row col NE 9 8 E 3 E E E SE dir 2 1 0 29

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 3 3 6 5 1 4 1 3 2 2 1 1 row col E E 9 E E E SE dir 3 8 2 1 0 30

Example Stack: Evaluation of Expressions _ Infix, Postfix, and Prefix Expressions _ Evaluation of

Example Stack: Evaluation of Expressions _ Infix, Postfix, and Prefix Expressions _ Evaluation of Postfix Expressions _ Conversion from Infix to Postfix Expressions infix prefix postfix a+b +ab ab+ a-b -ab ab- a*b *ab ab* a/b /ab ab/ 31

Postfix Expressions 2 * ( 3 + 4 ) = 14 2 * 3

Postfix Expressions 2 * ( 3 + 4 ) = 14 2 * 3 + 4 = 10 A-(B/C*D) 2 3 4 + * 2 3 * 4 + A B C / D * - 7 6 B/C*D 14 10 A-(B/C*D) 32

2 * ( 3 + 4 ) = 14 2 3 4 + *

2 * ( 3 + 4 ) = 14 2 3 4 + * 7 14 2 4 + * 3 2 + * 4 3 2 * 7 2 14 33

2 * 3 + 4 = 10 2 3 * 4 + 6 2

2 * 3 + 4 = 10 2 3 * 4 + 6 2 10 * 4 + 3 2 4 + 6 + 4 6 10 34

【範例】A - ( B + C * D ) / E Ch Stack S

【範例】A - ( B + C * D ) / E Ch Stack S ( bottom to top) A ABCD*+E/PE rule A 1 - - A 4 ( - ( A 2 B - ( AB 1 + - (+ AB 4 C - (+ ABC 1 * - (+* ABC 4 D - (+* ABCD 1 42

) - (+ ABCD* 3 - ( ABCD*+ 3 - ABCD*+ 3 / -

) - (+ ABCD* 3 - ( ABCD*+ 3 - ABCD*+ 3 / - / ABCD*+ 4 E - / ABCD*+E - ABCD*+E/ 5 ABCD*+E/- 5 1 43

Example Stack: HTML Parser <html> <head> <title>The Title</title> </head> <body> <h 1>網頁內容</h 1> </body>

Example Stack: HTML Parser <html> <head> <title>The Title</title> </head> <body> <h 1>網頁內容</h 1> </body> </html> </title> <head> <title> 將 <head> 推入堆疊 前,先檢查堆疊中放 的標籤為 <html>,所 以知道<head> 父節點 為 <html>。 The Title <title> <head> <html> 遇到 </title> 到堆疊中 pop 出 <title>,所以知道 The Title 是 隸屬於 <title> 這個標籤。

Amount of Permutations of Stack n n Please see book p 130, exercises 2

Amount of Permutations of Stack n n Please see book p 130, exercises 2 從右邊依序輸入 1…n 的數,問總共有幾種輸 出情形?有那幾種情形不可能出現? 1 …. n

Amount of Permutations of Stack n 當 n=1 q n 當 n=2 q n

Amount of Permutations of Stack n 當 n=1 q n 當 n=2 q n 有 2 種 12 及 21 當 n=3 q q q n 只有 1 種 1 可能的排列組合有 123, 132, 213, 231, 312, 321 312 不可能出現 (Why? ) 所以只有五種 當 n=4 (自己練習)