Lecture 9 Recursions Prefix expression evaluation Hanoi Tower

  • Slides: 37
Download presentation
Lecture 9 Recursions Prefix expression evaluation Hanoi Tower Solving Waiting time of Run pattern

Lecture 9 Recursions Prefix expression evaluation Hanoi Tower Solving Waiting time of Run pattern 軟體實作與計算實驗 1

Outlines ØStack ØSimulation ØEvaluation of Prefix Expressions ØMultiple stacks ØSimulation ØHanoi Tower Panel ØAuto-play

Outlines ØStack ØSimulation ØEvaluation of Prefix Expressions ØMultiple stacks ØSimulation ØHanoi Tower Panel ØAuto-play 軟體實作與計算實驗 2

STACK l Data structure l Operations Allocate an empty stack l Check empty l

STACK l Data structure l Operations Allocate an empty stack l Check empty l Push l Pop l 軟體實作與計算實驗 3

Data Structure l Two fields An array l Top l 軟體實作與計算實驗 4

Data Structure l Two fields An array l Top l 軟體實作與計算實驗 4

Simulation of a stack demo_single_stack. m demo_single_stack. fig 軟體實作與計算實驗 5

Simulation of a stack demo_single_stack. m demo_single_stack. fig 軟體實作與計算實驗 5

New l l Function head: stk=new_stack() Function body: stk. s=[]; stk. top=0; 軟體實作與計算實驗 7

New l l Function head: stk=new_stack() Function body: stk. s=[]; stk. top=0; 軟體實作與計算實驗 7

Pop l l Function head: c=pop_stack(stk) Function body: If stk. top > zero j

Pop l l Function head: c=pop_stack(stk) Function body: If stk. top > zero j = stack. top c =stk. s(j) stack. top = stack. top -1 else c = -1 end 軟體實作與計算實驗 8

Push l l Function head: stk=push(c, stk) Function body: stk. s = [stk. s

Push l l Function head: stk=push(c, stk) Function body: stk. s = [stk. s c]; stk. top = stk. top + 1 軟體實作與計算實驗 9

Is_empty l l Function head: flag=is_empty (stk) Function body: If stk. top==0 flag =

Is_empty l l Function head: flag=is_empty (stk) Function body: If stk. top==0 flag = 1 else flag = 0; end 軟體實作與計算實驗 10

Application Evaluation of prefix notations 軟體實作與計算實驗 11

Application Evaluation of prefix notations 軟體實作與計算實驗 11

Prefix notations l The operator is prefixed to two operands in an expression l

Prefix notations l The operator is prefixed to two operands in an expression l *32 * 3 2 軟體實作與計算實驗 13

Prefix notations * V=*3 -*38/42 - 3 / * 3 8 4 2 軟體實作與計算實驗

Prefix notations * V=*3 -*38/42 - 3 / * 3 8 4 2 軟體實作與計算實驗 15

A simulator l Prefix notations simulated here are constrained by Operands in an expression

A simulator l Prefix notations simulated here are constrained by Operands in an expression are positive integers within {1, 2, …, 9} l Operators are within {+, -, *, /} l /ab is realized by floor(a/b) l 軟體實作與計算實驗 17

Evaluation of Prefix notations prefix_one_stack. m prefix_one_stack. fig 軟體實作與計算實驗 18

Evaluation of Prefix notations prefix_one_stack. m prefix_one_stack. fig 軟體實作與計算實驗 18

Problem 7 l Apply an operator to two operands. function v=evaluate(c, s 1, s

Problem 7 l Apply an operator to two operands. function v=evaluate(c, s 1, s 2) if c=='/' str=['v=floor(' s 1 c s 2 ')']; else str=['v=' s 1 c s 2]; end eval(str); 軟體實作與計算實驗 21

Evaluation of a prefix expression Function head: v=prefix(ss) Function body l l Allocate a

Evaluation of a prefix expression Function head: v=prefix(ss) Function body l l Allocate a stack, stk n = length(ss) For j=n: -1: 1 l l l c=ss(j) If c is a digit, push c to stack stk. If c is an operator, l l If stk. top is greater than two l pop an element from stk and set it to d 1 l pop an element from stk and set it to d 2 l apply operand c to d 1 and d 2 and set the result to v l push v to stack stk otherwise display(‘an invalid prefix expression’) 軟體實作與計算實驗 22

Multiple Stacks Hanoi Tower Play Panel 軟體實作與計算實驗 23

Multiple Stacks Hanoi Tower Play Panel 軟體實作與計算實驗 23

Problem statement l Three stacks l The first stack contains n disks, numbered from

Problem statement l Three stacks l The first stack contains n disks, numbered from 1, …, n l Move all disks from the first stack to the third stack. l A disk is never on the top of a smaller disk 軟體實作與計算實驗 24

Initial state of the three stacks 1 2 3 4 5 6 7 軟體實作與計算實驗

Initial state of the three stacks 1 2 3 4 5 6 7 軟體實作與計算實驗 25

Final state 1 2 3 4 5 6 7 軟體實作與計算實驗 26

Final state 1 2 3 4 5 6 7 軟體實作與計算實驗 26

A GUI simulator of multiple stacks demo_stack. m demo_stack. fig 軟體實作與計算實驗 27

A GUI simulator of multiple stacks demo_stack. m demo_stack. fig 軟體實作與計算實驗 27

A Hanoi Tower Panel Play. Hanoi. Tower. m Play. Hanoi. Tower. fig 軟體實作與計算實驗 29

A Hanoi Tower Panel Play. Hanoi. Tower. m Play. Hanoi. Tower. fig 軟體實作與計算實驗 29

Three steps for auto-play Move n-1 objects from stack 1 to stack 2 l

Three steps for auto-play Move n-1 objects from stack 1 to stack 2 l Move 1 object from stack 1 to stack 3 l Move n-1 objects from stack 2 to stack 3 l 軟體實作與計算實驗 31

Initial state of 4 -disk problem 軟體實作與計算實驗 32

Initial state of 4 -disk problem 軟體實作與計算實驗 32

Move 3 disks from stack 1 to 2 軟體實作與計算實驗 33

Move 3 disks from stack 1 to 2 軟體實作與計算實驗 33

Move 3 disks from stack 2 to 3 軟體實作與計算實驗 35

Move 3 disks from stack 2 to 3 軟體實作與計算實驗 35

Hanoi-tower auto-play panel Hanoi. Tower. m Hanoi. Tower. fig 軟體實作與計算實驗 36

Hanoi-tower auto-play panel Hanoi. Tower. m Hanoi. Tower. fig 軟體實作與計算實驗 36