Stack Push Down Stack KMITL 01076249 Data Structures

  • Slides: 39
Download presentation
Stack (Push Down Stack) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data

Stack (Push Down Stack) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 1

Stack ? http: //clipart-library. com/search/ รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249

Stack ? http: //clipart-library. com/search/ รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 2

Stack pop push Top of the stack LIFO Last in First out Pick ONE

Stack pop push Top of the stack LIFO Last in First out Pick ONE ? Which one ? Insert ONE ? Where? http: //www. clipartpanda. com รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 3

Logical Abstract Data Type Logical ADT : 1. Data : ของมลำดบ มปลายบน 2. Methods

Logical Abstract Data Type Logical ADT : 1. Data : ของมลำดบ มปลายบน 2. Methods : pop 1. init() init empty stack 2. push(i) insert i ท 3. i = pop() return + เอาของท 4. i = peek() return ของท top )ไมเอาออก ( 5. b = is. Empty() stack empty ? 6. b = is. Full() push Top top ออก top LIFO Last in First out stack full ? 7. i = size() เครอตราช return รศ. กฤตวน จำนวนของใน ศรบรณ stack. KMITL 01076249 Data Structures & Algorithms : Stack 4 รศ. ดร. บญธร

Stack Implementation Data Implementation ? Logical ADT : 1. Data : ของมลำดบ มปลายบนPython List

Stack Implementation Data Implementation ? Logical ADT : 1. Data : ของมลำดบ มปลายบนPython List 2. Methods : 1. init() init empty stack 2. push(i) insert i ท 3. i = pop() 4. i = peek() S = [] top S. append(i) return + เอาของท ใสทาย top ออก i = S. pop() อนทาย i = S[-1] return ของท top push Top S == [] ? )ไมเอาออก ( 5. b = is. Empty() stack empty ? 6. b = is. Full() pop stack full ? Python List implementation i = len(S) LIFO Last in First out 7. i = size() เครอตราช return รศ. กฤตวน จำนวนของใน ศรบรณ stack. KMITL 01076249 Data Structures & Algorithms : Stack 5 รศ. ดร. บญธร

Stack Data Implementation 1. Data : __init__() : constructor ������� 2 underscores รศ. ดร.

Stack Data Implementation 1. Data : __init__() : constructor ������� 2 underscores รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 2 underscores KMITL 01076249 Data Structures & Algorithms : Stack 6

self ��� object ���� Data Implementation : __init__() method ���������� s = 1. Data

self ��� object ���� Data Implementation : __init__() method ���������� s = 1. Data Implementation : Stack ������� -> Python Stack() List ทำใน constructor self ������� s = Stack(s) __init__() self ����� pass ���� arg. ������������ docstring : �� triple quote print(Stack. __doc__) class Stack: """ class Stack create empty stack """ docstring constructor def __init__(self): function �������� self. items = [] ����� instantiate instance (object) ���� instance ������ pass argument s = Stack() print(s. items) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ [] constructor : ��� define Instance Attributes ��������� �� attribute ���� items : Instance Attributes ����� instance KMITL 01076249 Data Structures & Algorithms : Stack 7

Default Argument class Stack: """ class Stack default : empty stack / Stack([list]) """

Default Argument class Stack: """ class Stack default : empty stack / Stack([list]) """ def __init__(self, list = None): if list == None: self. items = [] else: self. items = list default argument ����� pass arg. �� list = None ��� pass arg. �� list = ������ Object None pass �� ������� obj identity s = Stack() [] print(s. items) s 1 = Stack(['A', 'B', 'C']) ['A', 'B', 'C'] print(s 1. items) ไมเหมอนกบ หลายตวได รศ. ดร. บญธร C++ & Java ใน Python ไมสามารถม constructor เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 8

Default Argument ทำให function สามารถถกเรยกไดโดยใช argument นอยลง ask_ok('Do you really want to quit? ')

Default Argument ทำให function สามารถถกเรยกไดโดยใช argument นอยลง ask_ok('Do you really want to quit? ') ask_ok('OK to overwrite the file? ', 2, 'Come on, only yes or no!') รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 9

Default Value i = 5 def f(a, L = None : ( def f(a,

Default Value i = 5 def f(a, L = None : ( def f(a, L : ([] = def f(arg=i : ( print)arg ( 5 return L L [] = L. append( i = 6 print)f(1 (( [1] f() print)f(2 (( 1], [2 print)f(3(( 1], 2 , [3 Default value ถกประมวณผล ณ function definition ในการ define scope รศ. ดร. บญธร เครอตราช if L is None : L. append(a ( Default value ถกประมวณผล ครงเดยว สำหรบ mutable object จะสงผลไปยงการเรยกฟงก ชนครงถดไป รศ. กฤตวน ศรบรณ a ( return L ไมอยากใช default argument รวมกบการ call ครงถดๆ ไป KMITL 01076249 Data Structures & Algorithms : Stack 10

push() Check Stack Overflow ? -> Python list inside operation insert I ������� list

push() Check Stack Overflow ? -> Python list inside operation insert I ������� list �� class Stack: def push(self, i): self. items. append(i) s = Stack() print(s. items) [] s. push('A') print(s. items) ['A'] s. push('B') print(s. items) 2 C 1 B 0 A ['A', 'B'] s. push('C') print(s. items) รศ. ดร. บญธร เครอตราช ['A', 'B', 'C' ] รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 12

pop() �� class Stack: def pop(self): # remove & return ���� return self. items.

pop() �� class Stack: def pop(self): # remove & return ���� return self. items. pop() ������� return !!! print(s. items) ['A', 'B'] print(s. items. pop()) B print(s. items) ['A'] print(s. items. pop()) A print(s. items) [] s. items. pop() # error Stack Underflow รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1 B 0 A KMITL 01076249 Data Structures & Algorithms : Stack 13

peek() �� class Stack: def peek(self): # return ���� return self. items[ -1] ['A',

peek() �� class Stack: def peek(self): # return ���� return self. items[ -1] ['A', 'B'] print(s. items) print(s. items. peek()) print(s. items) รศ. ดร. บญธร เครอตราช B 1 B 0 A ['A', 'B'] รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 14

is. Empty() �� class Stack: def is. Empty(self): return self. items == [] print(s.

is. Empty() �� class Stack: def is. Empty(self): return self. items == [] print(s. items) ['A', 'B'] print(s. is. Empty()) false รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1 B 0 A KMITL 01076249 Data Structures & Algorithms : Stack 15

size() �� class Stack: def size(self): return len(self. items) print(s. items) ['A', 'B'] print(s.

size() �� class Stack: def size(self): return len(self. items) print(s. items) ['A', 'B'] print(s. size()) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 2 1 B 0 A KMITL 01076249 Data Structures & Algorithms : Stack 16

Stack Implementation class Stack: """ class Stack default : empty stack / Stack([. .

Stack Implementation class Stack: """ class Stack default : empty stack / Stack([. . . ]) """ def __init__(self, list = None): if list == None: self. items = [] else: self. items = list def push(self, i): self. items. append(i) def pop(self): return self. items. pop() def peek(self): return self. items[-1] def __str__(self): s = 'stack of '+ str(self. size())+' items : ' for ele in self. items: s += str(ele)+' ' __str__() ���� return str return s def is. Empty(self): return self. items == [] def size(self): return len(self. items) s 1 = Stack([1, 2, 3]) print(s 1. items) [1, 2, 3] print(s 1) stack of 3 items : 1 2 3 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 17

Stack Applications 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion

Stack Applications 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion (Reverse Polish Notation) 4. Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 18

Parenthesis Matching ( a+b-c *[d+e]/{f*(g+h) } Match ? Algorithm ? รศ. ดร. บญธร เครอตราช

Parenthesis Matching ( a+b-c *[d+e]/{f*(g+h) } Match ? Algorithm ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 19

Parenthesis Matching [ ( a+b-c }*[d+e]/{f*(g+h) } Match ? Algorithm ? รศ. ดร. บญธร

Parenthesis Matching [ ( a+b-c }*[d+e]/{f*(g+h) } Match ? Algorithm ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 20

Parenthesis Matching 45 ( 3 + 2 ) / { 4**5 } Match ?

Parenthesis Matching 45 ( 3 + 2 ) / { 4**5 } Match ? Algorithm ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 21

Parenthesis Matching random / // [ (a+b)*{ (d+e)-3}] / / ↑ 1. 2. 3.

Parenthesis Matching random / // [ (a+b)*{ (d+e)-3}] / / ↑ 1. 2. 3. 4. 5. 6. 7. 8. 1. Simulate ���� • Data Structure : Python List • Expensive 2. Use Stack ↑ Repeat random position (until check all paren || not match). Scan out both ways. if Left side = open paren & Right side = close paren. if match : Check out. goto 2 else end process : Not match. else goto 1 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 22

Writing Code Def paren. Match(s) : # your code ? รศ. ดร. บญธร เครอตราช

Writing Code Def paren. Match(s) : # your code ? รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 24

Design Tools, Warnier-Orr Diagram Code : difficult Pseudocode : easier if open. Paren(c) :

Design Tools, Warnier-Orr Diagram Code : difficult Pseudocode : easier if open. Paren(c) : s. push(c) elif close. Paren(c) : ch = s. pop() match = is. Match(c, ch) Warnier – Orr Diagram : easier if (c is an open parenthesis) push c to stack s else if (c is an close parenthesis) pop ch from stack s if (ch matches c) match = true else match = false c == open paren s. push(c) Warnier – Orr Diagram or + + c == close paren pop&check ch = s. pop() match = is. Match(ch, c) if … then not A รศ. ดร. บญธร A เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 25

Warnier-Orr Diagram Design Tool [ (a+b)*{ (d+e)-3}] Init empty stack s error=false read ch

Warnier-Orr Diagram Design Tool [ (a+b)*{ (d+e)-3}] Init empty stack s error=false read ch ch = non_paren Scan (not EOF ch = open paren + && not error) s. push(ch) s. empty() ch = close paren error =true(no-open-paren ) + open = s. pop() s. empty() Paren matching match(open, ch) match(open, ch ) error (missmatch) no open-paren / missmatch + s. empty() MATCH ( + error s. empty() รศ. ดร. บญธร error = true เครอตราช รศ. กฤตวน ศรบรณ MISSMATCH open paren exceed [ KMITL 01076249 Data Structures & Algorithms : Stack 26

Python : Parenthesis Matching def paren. Matching(str): s = Stack() i = 0 #

Python : Parenthesis Matching def paren. Matching(str): s = Stack() i = 0 # index : str[i] error = 0 while i < len(str) and error == 0 : c = str[i] if c in '{[(': s. push(c) else: if c in '}])': if s. size() > 0: if not match(s. pop(), c): error = 1 # open & close not match else: # empty stack error = 2 # no open paren i += 1 if s. size() > 0: error = 3 return error, c, i, s รศ. ดร. บญธร # stack not empty # open paren(s) excesses เครอตราช รศ. กฤตวน ศรบรณ str = '[{a+b-c}' err, c, i, s = paren. Matching(str) if err == 1: print(str , 'unmatch open-close ') elif err == 2: print(str , 'close paren excess') elif err == 3: print(str , 'open paren(s) excess ', s. size(), ': ', end='' ) for ele in s. item: print(ele, sep=' ', end = '') print() else: print(str, 'MATCH') def match(open, close): return (open == '(' and close == ')') or (open == '{' and close == '}') or (open == '[' and close == ']') def match 2(op, cl): opens = "([{" closes = ")]}" return opens. index(op) == closes. index(cl) KMITL 01076249 Data Structures & Algorithms : Stack 27

Stack Application 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion

Stack Application 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion (Reverse Polish Notation) 4. Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 28

Postfix Notation (Polish Notation) Infix Form Prefix Form Postfix Form a + b +

Postfix Notation (Polish Notation) Infix Form Prefix Form Postfix Form a + b + a b + a + b * c + a * b c a b c * + รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 29

Evaluate Postfix Notation มธรรมชาตเปน ตวกอนหนามน stack: operator เปนของ operands 2 6523+8*-3+* =? input: 6523

Evaluate Postfix Notation มธรรมชาตเปน ตวกอนหนามน stack: operator เปนของ operands 2 6523+8*-3+* =? input: 6523 Push : 6, 5, 2, 3 3 2 5 6 S รศ. ดร. บญธร เครอตราช input: + o 2 <- pop#1 o 1 <- pop#2 + 5 push What ‘s next ? รศ. กฤตวน ศรบรณ 5 5 6 S KMITL 01076249 Data Structures & Algorithms : Stack 30

6523+8*-3+* =? input: 6523 input: + Push : 6 5 2 3 3 pop#1

6523+8*-3+* =? input: 6523 input: + Push : 6 5 2 3 3 pop#1 → 3 2 pop#2 → 2 5 5 push 2+3 5 6 6 s input: 8 s 8 input: * push 5 pop#1 → 8 40 5 pop#2 → 5 5 6 push 5*8 6 8 input: - input: 3 pop#1 → 40 push 3 3 pop#2 → 5 -35 push 5 -40 6 6 input: + input: * pop#1 → 3 pop#1 → -32 pop#2 → -35 push -35+3 รศ. ดร. บญธร เครอตราช -32 6 รศ. กฤตวน ศรบรณ pop#2 → 6 push 6 * -32 - 192 KMITL 01076249 Data Structures & Algorithms : Stack 31

Stack Application 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion

Stack Application 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion (Reverse Polish Notation) 4. Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 32

Infix to Postfix Conversion aa*b+c ===> ab*c+ Notice : output: operands’ order is the

Infix to Postfix Conversion aa*b+c ===> ab*c+ Notice : output: operands’ order is the same as input’s. output stack รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 33

Infix to Postfix Conversion a*b+c input: stack: a*b+c a*b+c รศ. ดร. บญธร ---- >

Infix to Postfix Conversion a*b+c input: stack: a*b+c a*b+c รศ. ดร. บญธร ---- > * * + + เครอตราช รศ. กฤตวน ศรบรณ ab*c+ output: a a ab ab*c+ KMITL 01076249 Data Structures & Algorithms : Stack 34

Infix to Postfix Conversion a+b*c input: stack: a+b*c + + * + a+b*c รศ.

Infix to Postfix Conversion a+b*c input: stack: a+b*c + + * + a+b*c รศ. ดร. บญธร เครอตราช abc*+ ---- > รศ. กฤตวน ศรบรณ output: a a ab ab abc*+ KMITL 01076249 Data Structures & Algorithms : Stack 35

Infix to Postfix Conversion input: a+b*c-d stack: a+b*c-d a+b*c-d รศ. ดร. บญธร เครอตราช output:

Infix to Postfix Conversion input: a+b*c-d stack: a+b*c-d a+b*c-d รศ. ดร. บญธร เครอตราช output: + + * + - รศ. กฤตวน ศรบรณ a+b*c-d => abc*+d- a a ab ab abc*+d- KMITL 01076249 Data Structures & Algorithms : Stack 36

Infix to Postfix Conversion abc*+de/f+g*- a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g a+b*c-(d/e+f)*g รศ. ดร. บญธร เครอตราช +

Infix to Postfix Conversion abc*+de/f+g*- a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g a+b*c-(d/e+f)*g รศ. ดร. บญธร เครอตราช + * + ( / ( - ab abc*+de รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 37

Infix to Postfix Conversion (cont. ) a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g a+b*c-(d/e+f)*g รศ. ดร. บญธร เครอตราช

Infix to Postfix Conversion (cont. ) a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g a+b*c-(d/e+f)*g รศ. ดร. บญธร เครอตราช / ( + ( * - abc*+de/f+g*abc*+de/f+ abc*+de/f+g*- รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 38

Stack Application 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion

Stack Application 1. Parenthesis Matching 2. Evaluate Postfix Expression 3. Infix to Postfix Conversion (Reverse Polish Notation) 4. Function Call (clearly see in recursion) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Stack 39