4 Exmple exm questions Omvnl uttryket ( ) e / (f g / h ) från infix till postfix me hjälp v en stk oh vis vrje steg i proessen. (5p) Vis sen me hjälp v en stk hur mn skulle eräkn et postfix uttrykets väre om = 4, = 3, = 5, = 8, e=3, f = 5, g = 7 oh h = 9. (5p) Vis hur mn skulle kunn omvnl uttryket i el (1) från infix till postfix me hjälp v ett trä. (5p) Använ smm exempel för tt förklr hur mn skulle kunn få tillk uttryket i infix nottion från postfix. (5p) 20/11/2016 DFR Dt Strutures & Algorithms 1
First Priniples & Rules of Thum Infix => postfix operns pper in the sme orer (in => post) in the orresponing expression tree, operns re lwys LEAF NODES if you forget the preeene rules, work from first priniples (see next slie) ALWAYS oule hek your work e.g. Infix => postfix => tree => infix (inorer) 20/11/2016 DFR Dt Strutures & Algorithms 2
First Priniples exmples x y => x y x y => x y hene y sustitution x =, y = => ( ) x =, y = => In pitures x y x y 20/11/2016 DFR Dt Strutures & Algorithms 3
Algorithm: infix => postfix Input = opern => output opern (=> opern orer lwys sme) Input = opertor => hek preeene preeene (input opertor) > preeene (tos opertor) stk input opertor preeene (input opertor) <= preeene (tos opertor) (pop n output tos opertor) then stk input opertor Input = ( stk ( Input = ) pop & output tos opertor until ( pop ( Input = # (empty) => pop & output tos opertor until stk = 20/11/2016 DFR Dt Strutures & Algorithms 4
Question 1 Omvnl uttryket ( ) e / (f g / h ) från infix till postfix me hjälp v en stk oh vis vrje steg i proessen. (5p) pproh eie formt e.g. input string / output string / stk (tos on rhs) / rule go through the exmple stepwise strt setup input : ( ) e / (f g / h ) # output: stk: rule: <text> 20/11/2016 DFR Dt Strutures & Algorithms 5
Answer 1 (input ; output ; stk ; rule) ( ) e / (f g / h ) # ; ; ; strt setup ( ) e / (f g / h ) # ; ; ; output ( ) e / (f g / h ) # ; ; ; stk ( ) e / (f g / h ) # ; ; ; output ( ) e / (f g / h ) # ; ; ; stk pre > ) e / (f g / h ) # ; ; ( ; stk ( lwys ) e / (f g / h ) # ; ; ( ; output ) e / (f g / h ) # ; ; ( ; stk pre > ( ) e / (f g / h ) # ; ; ( ; output e / (f g / h ) # ; ; ; output pop to ( e / (f g / h ) # ; ; ; output pre > 20/11/2016 DFR Dt Strutures & Algorithms 6
Answer 1 (input ; output ; stk ; rule) (ontinue) e / (f g / h ) # ; ; ; output pre > e / (f g / h ) # ; ; ; output pre = e / (f g / h ) # ; ; ; stk stk / (f g / h ) # ; e ; ; output e (f g / h ) # ; e ; / ; stk / pre / > f g / h ) # ; e ; /( ; stk ( lwys g / h ) # ; e f ; /( ; output f g / h ) # ; e f ; /( ; stk pre > ( / h ) # ; e f g ; /( ; output g h ) # ; e f g ; /( /; stk / pre / > ) # ; e f g h ; /( /; output h 20/11/2016 DFR Dt Strutures & Algorithms 7
Answer 1 (input ; output ; stk ; rule) (ontinue) ) # ; e f g h ; / ( / ; output h # ; e f g h / ; / ; output pop to ( # ; e f g h / / ; ; output eos # pop result: e f g h / / ; eos # / stk input: ( ) e / (f g / h ) ross hek: ( ) e / ( f g / h ) 20/11/2016 DFR Dt Strutures & Algorithms 8
Question 2 Vis sen me hjälp v en stk hur mn skulle eräkn et postfix uttrykets väre om = 4, = 3, = 5, = 8, e=3, f = 5, g = 7 oh h = 9. (5p) infix: ( ) e / ( f g / h ) 4 3 ( 5 8 ) 3 / ( 5 7 / 9 ) 3 0.77 9 5.77 5 ~ 0.5 ~ 5.5 20/11/2016 DFR Dt Strutures & Algorithms 9
Answer 2 Approh postfix: e f g h / / sustitute: 4 3 5 8 3 5 7 9 / / strt setup input: 4 3 5 8 3 5 7 9 / / stk: (tos on rhs) rule: (see elow) lgorithm if input = opern, stk opern if input = opertor, pply opertor to tos & tos 1 : tos 1 op tos 20/11/2016 DFR Dt Strutures & Algorithms 10
Answer 2 (input ; stk ; rule) (ontinue) 4 3 5 8 3 5 7 9 / / ; ; strt setup 3 5 8 3 5 7 9 / / ; 4 ; stk 4 5 8 3 5 7 9 / / ; 4 3 ; stk 3 8 3 5 7 9 / / ; 4 3 5 ; stk 5 3 5 7 9 / / ; 4 3 5 8 ; stk 8 3 5 7 9 / / ; 4 3 3 ; pply tos 1 op tos 3 5 7 9 / / ; 4 9 ; pply tos 1 op tos 3 5 7 9 / / ; 5 ; pply tos 1 op tos 20/11/2016 DFR Dt Strutures & Algorithms 11
Answer 2 (input ; stk ; rule) (ontinue) 3 5 7 9 / / ; 5 ; pply tos 1 op tos 5 7 9 / / ; 5 3 ; stk 3 7 9 / / ; 5 3 5 ; stk 5 9 / / ; 5 3 5 7 ; stk 7 / / ; 5 3 5 7 9 ; stk 9 / ; 5 3 5 0.77 ; pply / tos 1 op tos / ; 5 3 5.77 ; pply tos 1 op tos ; 5 ~0.5 ; pply / tos 1 op tos ; ~ 5.5 ; pply tos 1 op tos 20/11/2016 DFR Dt Strutures & Algorithms 12
Question 3 Vis hur mn skulle kunn omvnl uttryket i el (1) från infix till postfix me hjälp v ett trä. (5p) pproh infix: ( ) e / (f g / h ) postfix: e f g h / / from previous nswer (nswer 1 to question 1) ( ) e / ( f g / h ) 20/11/2016 DFR Dt Strutures & Algorithms 13
From first priniples ( ) e / ( f g / h ) / e f / g h 20/11/2016 DFR Dt Strutures & Algorithms 14
Postorer trversl of tree / e f / g h Answer to question 3: e f g h / / 20/11/2016 DFR Dt Strutures & Algorithms 15
Question 4 Använ smm exempel för tt förklr hur mn skulle kunn få tillk uttryket i infix nottion från postfix. (5p) Approh reonstrut the tree from the postfix expression perform n inorer trversl of the tree to otin the infix form of the expression Comment: think out how you restore the ( n ) HINT: think out the preeene of the opertors 20/11/2016 DFR Dt Strutures & Algorithms 16
Postfix => tree Postfix: e f g h / / Metho: pply sme rules s in question 2 Algorithm: if input = opern, stk opern if input = opertor, pply opertor to tos & tos 1 : tos 1 op tos to mke tree Comment: this is generlistion of the lultion in nswer 2 inste of prtiulr vlue, the result is (su)tree n the finl result is tree for the whole expression 20/11/2016 DFR Dt Strutures & Algorithms 17
Postfix => tree: exmple Postfix: e f g h / / pply pply 20/11/2016 DFR Dt Strutures & Algorithms 18
Postfix => tree: exmple (ontinue) Postfix: e f g h / / pply pply 20/11/2016 DFR Dt Strutures & Algorithms 19
Postfix => tree: exmple (ontinue) Postfix: e f g h / / e f g h e f / g h pply / 20/11/2016 DFR Dt Strutures & Algorithms 20
20/11/2016 DFR Dt Strutures & Algorithms 21 Postfix => tree: exmple (ontinue) Postfix: e f g h / / / h g pply f / h g e e f
Postfix => tree: exmple (ontinue) Postfix: e f g h / / e f / g h pply / / e f / g h 20/11/2016 DFR Dt Strutures & Algorithms 22
Postfix => tree: exmple (ontinue) Postfix: e f g h / / pply / / e e f / f / g h g h 20/11/2016 DFR Dt Strutures & Algorithms 23
Postfix => tree: exmple (ontinue) Postfix: e f g h / / / e f / g h now ross hek inorer trversl: ( ) e / (f g / h ) postorer trversl: e f g h / / hene nswer to question 4 20/11/2016 DFR Dt Strutures & Algorithms 24