ellis horowitz. sartaj sahni dinesh mehta advance data structure review of chapter 3 張啟中

47
ELLIS HOROWITZ

Post on 15-Jan-2016

237 views

Category:

Documents


8 download

TRANSCRIPT

ELLIS HOROWITZ

SARTAJ SAHNI

DINESH MEHTA

Advance Data StructureReview of Chapter 3

張啟中

Stack

An ordered list Insertions and deletions are made at one end, cal

led top Last-In-First-Out (LIFO) list

Abstract Data Type for stacktemplate <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

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};

Representation: Stack

Definitionprivate:

int top;

KeyType *stack;

int MaxSize; Implementation of Stack

template <class KeyType>Stack<KeyType>::Stack(int MaxStackSize):MaxSize(MaxStackSize)

{

stack= new KeyType[MaxSize];

top = -1;

}

0

1

2

MaxSize - 1

k

Top Items

k

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

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

Stack Example: Checking for Balanced Braces

abc{defg{ijk}{l{mn}}op}rr abc{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

{a{b}c}

{

{

{ {

1. Push '{'2. Push '{'3. Pop4. PopStack empty ==> balanced

{a{bc}

{

{

{ {

{

1. Push '{'2. Push '{'3. PopStack not empty ==> not balanced

1. Push '{'2. PopStack empty when last '}' ==> not balanced

{ab}c}

Input string Stack as algorithm executes

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 readingforward and backward.

為了簡化問題起見,此處我們假設字串中含有單一個字元 $ ,此字元區分字串為兩半部,如下圖所示:

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 至堆疊中

ba

c d e f $ f e d c b a

top

16

cba

d e f $ f e d c b a

topdcba

e f $ f e d c b a

topedcba

f $ f e d c b a

top

fedcba

$ f e d c b a

top fedcba

f e d c b a

top

Step 2: 碰到 $ 字元,捨棄 $ 字元後,不再 push 其後的字元至 stack 中,而進入比較階段。

17

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

fedcba

f e d c b a

topedcba

e d c b a

topdcba

d c b a

top

cba

c b a

topba

b a

topa

a

top

18

Step 4: 若比較之後,下列三個條件都成立:

(1) 相比的字元一樣; (2) stack 變成空的; (3) 沒有剩餘的字元;

則此字串是一個 palindrome ,否則不是一個 palindrome 。

a

a

top

cba

k b a

top

c b a

top

cba

top

YES NO NO NO

19

Example Stack: A Mazing Problem

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

0 : open1 : close

S

N

EW

NE

SE

NW

SW

20

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE 0

1

2

3

4

5

6

7

8

9

21

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 NE

0

1

2

3

4

5

6

7

8

9

22

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 NE1 3 E

0

1

2

3

4

5

6

7

8

9

23

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 NE1 3 E1 4 E

0

1

2

3

4

5

6

7

8

9

24

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 NE1 3 E1 4 E

0

1

2

3

4

5

6

7

8

9

1 5 SW

25

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 NE1 3 E1 4 E

0

1

2

3

4

5

6

7

8

9

1 5 SW2 4 SE

26

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 NE

0

1

2

3

15

16

174 14 E3 13 SE3 12 E

1 3 E1 4 E

27

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 E

0

1

2

3

3 13 SE3 12 E 15

16

BacktrackingBacktrackingBacktrackingBacktracking

174 14 E

1 3 E1 4 E

28

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 E

0

1

2

3

3 12 E 15

BacktrackingBacktrackingBacktrackingBacktracking

3 13 SE 16

1 3 E1 4 E

29

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 E

0

1

2

3

3 5 E 8

BacktrackingBacktrackingBacktrackingBacktracking

3 6 NE 9

1 3 E1 4 E

30

0 1 0 0 0 1 1 0 0 0 1 1 1 1 11 0 0 0 1 1 0 1 1 1 0 0 1 1 10 1 1 0 0 0 0 1 1 1 1 0 0 1 11 1 0 1 1 1 1 0 1 1 0 1 1 0 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 0 1 1 0 1 1 0 1 1 1 1 1 0 11 1 0 0 0 1 1 0 1 1 0 0 0 0 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 1 0 0 1 1 1 1 1 0 1 1 1 1 0

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

1

23456789

1011

row col dir

1 1 SE2 2 E

0

1

2

3

3 5 E 83 6 E 9

1 3 E1 4 E

31

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 + a b a b +

a b a b a b

a b a b a b

a b a b a b

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

2 3 4 + *

7

14

2 * ( 3 + 4 ) = 14

2 3 4 + * 3 4 + *

2

4 + *

32

+ *

432

*

72 14

34

2 * 3 + 4 = 10

2 3 * 4 +

6

10

2 3 * 4 + 3 * 4 +

2

* 4 +

32

4 +

6

+

46 10

35

Infix Convert to Postfix從前面的例子,我們知道利用堆疊可以很容易地計算

後置算式。因此,計算中置算式可以分為底下的兩個

步驟來執行:

1. 將中置算式轉換為後置算式

2. 用堆疊來計算所得的後置算式

36

【範例】

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

6

10

2 3 4 + *

7

14

2 * ( 3 + 4 ) = 14

37

【範例】

中置算式 後置算式

(A + B ) * C A B + C *

A + B * C A B C * +

A B + C A B C +

A ( B + C ) A B C +

A ( B / C * D ) A B C / D *

A B + C * D ) / E A B C D * + E /

A B + C * D / E A B C D * E / +

38

運算元的次序在中置算式和後置算式是完全相同的。

在中置算式中,如果運算元 X 在運算子 OP 之前, 那麼,在後置算式中,運算元 X 也會在運算子 OP

之前。 所有括號在轉換後均被消除。

運算子在後置算式中的出現順序是依照其在中置算式 的運算順序而定。

Infix 與 Postfix 比較

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

中置算式轉換為後置算式的演算法

1. 當碰到運算元時,直接輸出此運算元。

2. 當碰到左括號時,將其 push 至堆疊中。

3. 當碰到右括號時,將堆疊中的運算子 pop 出來並輸出,

直到碰到左括號為止,然後再將此左括號 pop 掉。

A + B運算元

運算子

41

4. 當碰到運算子時

依序將堆疊中運算優先次序較高或相同的運算子 pop 出來並輸出,直到遇到下列情形之一

(1) 碰到左括號

(2) 碰到優先次序較低的運算子

(3) 堆疊變為空

最後將此運算子 push 至堆疊中。

5. 當輸入字串全部處理完時,將堆疊中所剩餘的運算子逐一地 pop 出來並輸出,直到堆疊變為空為止。

中置算式轉換為後置算式的演算法

42

【範例】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

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 * + E 1

- A B C D * + E / 5

A B C D * + E / - 5

Example Stack: HTML Parser

<html>

<head>

<title>The Title</title>

</head>

<body>

<h1> 網頁內容 </h1>

</body>

</html> <html>

<head>

<title>

<html>

<head>

<title>

The Title

</title>遇到 </title> 到堆疊中 pop 出 <title> ,所以知道 The Title 是隸屬於 <title> 這個標籤。

將 <head> 推入堆疊前,先檢查堆疊中放的標籤為 <html> ,所以知道 <head> 父節點為 <html> 。

Amount of Permutations of Stack Please see book p130, exercises 2 從右邊依序輸入 1…n 的數,問總共有幾種輸

出情形?有那幾種情形不可能出現?

1 …. n

當 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

Amount of Permutations of Stack 一般化的解

列出遞迴方程式 bn = b0bn-1 + b1bn-2 + …… + bn-2b1+ bn-1b0

此遞迴方程式為非齊次式,用迭代法不好解,用生成函數法比較好解,請參閱離散數學的書。

此解我們於計算二元樹的個數還會看到。

nnn C

nb 2

1

1