SlideShare a Scribd company logo
COMP1603
Stacks
RPN (Postfix) Evaluation
Infix to Postfix Algorithm and examples
1
Covered so far
• ADTs – shows data structures and the
operations that can be performed on them,
but no implementation. E.g. Stack - Push, Pop
• Stack and Queue introduction
• Stack implementations – arrays and linked list
• Postfix evaluation – but this will be reviewed
2
Extra: USAGE OF STACKS
• Evaluation of postfix expressions : this idea is
used in the writing of calculator apps
• Checking if parentheses match in an
expression ((5 + 2) –don’t match
((7-8) + (9-5)) - match
how is stack used?
• Converting from infix to postfix
• Reverse a word and checking palindrome
• Tracking function calls in compilers
3
Using Stacks to Evaluate expressions
• Expressions must be in postfix form. Need to
convert from infix to postfix
• INFIX 5 + 2
• PREFIX + 5 2
• POSTFIX 5 2 + (operators come after relevant
operands)
• OTHER EXAMPLES (next slide)
4
INFIX ->POSTFIX EXAMPLE
• INFIX (8-4) * (5-2) + 6/2
= 4 * 3 + 3 = 15
• POSTFIX 8 4 - 5 2 - * 6 2 / +
A stack can be used to evaluate. (Later)
• There is another way to evaluate: Scan from
left to right and match operands with
operators and simplify. (next slide)
8 4 - 5 2 - * 6 2 / +
5
Evaluating a Postfix Expression
(Scanning from left to right)
INFIX (8-4) * (5-2) +
6/2
= 4 * 3 + 3 = 15
8 4 - 5 2 - * 6 2 / +
4 5 2 - * 6 2 / +
4 3 * 6 2 / +
12 6 2 / +
12 3 +
15 ANSWER
6
Evaluating a Postfix Expression
(Using a Stack)
• We can use a stack to track the operands for
the operators. Intermediate values are stored
on the stack
• See Algorithm on the following page
(Available on My Elearning)
7
Algorithm to Evaluate a Postfix
Expression
Stack usage: Store operands
• Initialize and empty stack.
• Repeat the following until the end of the expression is encountered:
– Get the next token (constant, variable, arithmetic operator) in
the RPN expression.
– If the token is an operand, push it onto the stack. If it is an
operator, then do the following:
• Pop the two top values from the stack. (If the stack does not
contain two items, an error due to a malformed RPN
expression has occurred and the evaluation is terminated.)
• Apply the operator to these two values.
• Push the resulting value back onto the stack.
• When the end of the expression is encountered, its value is at the
top of the stack (and in fact, must be the only value on the stack).
8 4 - 5 2 - * 6 2 / +
PUSH 8, PUSH 4. POP, POP
APPLY the – operator
PUSH RESULT = 4
Can you code the above algorithm?
8
DETAILED EVALUATION E.G.
Expression: INFIX: (8-4) * (5-2) + 6/2 is 4 * 3 + 3
= 15
Postfix 8 4 – 5 2 – * 6 2 / +
EVALUATION
SYM OPER? OPND? STACK COMMENT
8 Y 8 TOP PUSH 8
4 Y 8 4 TOP PUSH 4
- Y 4 TOP POP 4, 8, APPLY -, GET
4, PUSH RESULT (4)
5 Y 4 5 TOP PUSH 5
2 Y 4 5 2 TOP PUSH 2
- Y 4 3 TOP POP 2, 5, APPLY -,
PUSH(3)
9
DETAILED EVALUATION E.G.
Expression: INFIX: (8-4) * (5-2) + 6/2 is 4 * 3 + 3 = 15
Postfix 8 4 – 5 2 – * 6 2 / +
EVALUATION
SYM OPER? OPND? STACK COMMENT
* Y 12 TOP POP 3, 4 APPLY *,
PUSH RESULT 12
6 Y 12 6 TOP PUSH 6
2 Y 12 6 2 TOP PUSH 2
/ Y 12 3 TOP POP 2, 6 APPLY / PUSH
RESULT 3
+ Y 15 TOP POP 3, 12 APPLY +, PUSH
RESULT 15
15 IS FINAL ANSWER.
LATER: ALGORITHM FOR INFIX TO POSTFIX
10
INFIX TO POSTFIX CONVERSION
Stack usage: Store operators
Initialize an empty stack of operators.
While no error has occurred and the end of the infix expression has not been reached, do the
following:
Get the next input token (constant, variable, arithmetic operator, left parenthesis, right
parenthesis) in the infix expression.
If token is
A left parenthesis: Push it onto the stack.
A right parenthesis: Pop and display stack elements until a left parenthesis is
encountered. Do not display the left parenthesis. (It is an error if the stack becomes
empty with no left parenthesis found.)
An operator: If the stack is empty or token has higher priority than the top
stack element, push token onto the stack.
Otherwise, pop and display the top stack element; then repeat the comparison of
token with the new top stack item.
Note: A left parenthesis in the stack is assumed to have a lower priority than that of the
operators.
An operand: Display it.When end of expr is reached, pop and display all
remaining stack elements
-Leestma and Nyhoff
11
While no error has occurred and the end of the infix expression has not been
reached, do the following:
Get the next input token (constant, variable, arithmetic operator, left
parenthesis, right parenthesis) in the infix expression.
If token is
A left parenthesis: Push it onto the stack.
A right parenthesis: Pop and display stack elements until a left
parenthesis is encountered. Do not display the left parenthesis. (It is an
error if the stack becomes empty with no left parenthesis found.)
An operator: If the stack is empty or token has higher priority than
the top stack element, push token onto the stack.
Otherwise, pop and display the top stack element; then repeat the
comparison of token with the new top stack item.
Note: A left parenthesis in the stack is assumed to have a lower priority than that of
the operators.
An operand: Display it.
{At end, pop and display stack elements}
12
While no error has occurred and the end of the infix
expression has not been reached, do the following:
Get the next input token (constant, variable,
arithmetic operator, left parenthesis, right
parenthesis) in the infix expression.
If token is
A left parenthesis: Push it onto the stack.
A right parenthesis: Pop and display stack
elements until a left parenthesis is encountered.
Do not display the left parenthesis. (It is an
error if the stack becomes empty with no left
parenthesis found.)
13
An operator: If the stack is empty or token
has higher priority than the top stack element,
push token onto the stack.
Otherwise, pop and display the top stack
element; then repeat the comparison of token
with the new top stack item.
Note: A left parenthesis in the stack is assumed to
have a lower priority than that of the operators.
An operand: Display it.
{At end, pop and display stack elements}
14
Left brackets must be stacked
15
A left
parenthesis:
Push it onto the
stack.
A right
parenthesis:
Pop and display
stack elements
until a left
parenthesis is
encountered.
Do not display
the left
parenthesis.
An operator: If
the stack is
empty or token
has higher
priority than
the top stack
element, push
token onto the
stack.
An operand:
Display it.
Otherwise, pop and display
the top stack element; then
repeat the comparison of
token with the new top
stack item.
A left parenthesis in the stack is
assumed to have a lower priority than
that of the operators.
16
A left
parenthesis:
Push it onto the
stack.
A right
parenthesis:
Pop and display
stack elements
until a left
parenthesis is
encountered.
Do not display
the left
parenthesis.
An operator: If
the stack is
empty or token
has higher
priority than
the top stack
element, push
token onto the
stack.
An operand:
Display it. Otherwise, pop and display the top stack
element; then repeat the comparison of token
Practice
• Illustrate how (7-3) * (9-8/4) can be converted
to postfix.
-Postfix result on next slide.
• - Students to attempt (with diagram during
lecture)
17
(7-3) * (9-8/4) to postfix
POSTFIX
7 3 – 9 8 4 / - *
-
18
(7-3) * (9-8/4) to postfix
Sym Comment Stack Output
( Push ( ( T
7 Output operand ( T 7
- Push – as prior > ( ( - T 7
3 Output operand ( - T 7 3
) Pop and display until ( Empty 7 3 –
* Push * * T 7 3 -
( Push ( * ( T 7 3 -
9 Output operand * ( T 7 3 – 9
- Push – as prior > ( * (- T 7 3 - 9
8 Output operand * (- T 7 3 – 9 8
19
A left
parenthesis:
Push it onto the
stack.
A right
parenthesis:
Pop and display
stack elements
until a left
parenthesis is
encountered.
Do not display
the left
parenthesis.
An operator: If
the stack is
empty or token
has higher
priority than
the top stack
element, push
token onto the
stack.
An operand:
Display it.
Otherwise, pop and display the top stack element; then repeat the
(7-3) * (9-8/4) to postfix
Sym Comment Stack Output
LAST STEP:
8 Output operand * (- T 7 3 – 9 8
/ ‘/’ prior > ‘-’ prior . Push / * (- / T 7 3 – 9 8
4 Output operand * (- / T 7 3 – 9 8 4
) Pop and display until ( * (- / T 7 3 – 9 8 4 / -
Discard (
* T
END
Pop and display remaining stack elements
FINAL OUTPUT 7 3 – 9 8 4 / - *
20
A left
parenthesis:
Push it onto the
stack.
A right
parenthesis:
Pop and display
stack elements
until a left
parenthesis is
encountered.
Do not display
the left
parenthesis.
An operator: If
the stack is
empty or token
has higher
priority than
the top stack
element, push
token onto the
stack.
An operand:
Display it.
Otherwise, pop and display the top stack element; then repeat the
Practice
• Convert (7 – 8 / 2 / 2) * ((7 – 2) * 3 – 6)
• To postfix
21
Practice (longer)
• Convert (7 – 8 / 2 / 2) * ((7 – 2) * 3 – 6)
to postfix
• ANSWER
7 8 2 / 2 / - 7 2 – 3 * 6 - *
22
Checking balanced parentheses
(5 + 2 ) + 3(
Push (
When ) encountered, pop ( - stack is empty
Push (
End of expression reached. Error since stack is
not empty.
23
Wrap-Up
Stacks
• Review all code and applications
24

More Related Content

Similar to COMP1603 Stacks and RPN 2023 Recording (2).pptx

Team 3
Team 3Team 3
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
DEEPAK948083
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
arihantsherwani
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
AliJama14
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
Halid Assen
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stack
StackStack
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
MouDhara1
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
Baby81727
 
Stack application
Stack applicationStack application
Stack application
Ravi solanki
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
Krish_ver2
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
ashish bansal
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
Ramachandra Adiga G
 

Similar to COMP1603 Stacks and RPN 2023 Recording (2).pptx (20)

Team 3
Team 3Team 3
Team 3
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
STACK USING LISTS.pptx
STACK USING LISTS.pptxSTACK USING LISTS.pptx
STACK USING LISTS.pptx
 
Stack application
Stack applicationStack application
Stack application
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
 
Stacks
StacksStacks
Stacks
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)Data Structures: Stacks (Part 1)
Data Structures: Stacks (Part 1)
 

Recently uploaded

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 

Recently uploaded (20)

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 

COMP1603 Stacks and RPN 2023 Recording (2).pptx

  • 1. COMP1603 Stacks RPN (Postfix) Evaluation Infix to Postfix Algorithm and examples 1
  • 2. Covered so far • ADTs – shows data structures and the operations that can be performed on them, but no implementation. E.g. Stack - Push, Pop • Stack and Queue introduction • Stack implementations – arrays and linked list • Postfix evaluation – but this will be reviewed 2
  • 3. Extra: USAGE OF STACKS • Evaluation of postfix expressions : this idea is used in the writing of calculator apps • Checking if parentheses match in an expression ((5 + 2) –don’t match ((7-8) + (9-5)) - match how is stack used? • Converting from infix to postfix • Reverse a word and checking palindrome • Tracking function calls in compilers 3
  • 4. Using Stacks to Evaluate expressions • Expressions must be in postfix form. Need to convert from infix to postfix • INFIX 5 + 2 • PREFIX + 5 2 • POSTFIX 5 2 + (operators come after relevant operands) • OTHER EXAMPLES (next slide) 4
  • 5. INFIX ->POSTFIX EXAMPLE • INFIX (8-4) * (5-2) + 6/2 = 4 * 3 + 3 = 15 • POSTFIX 8 4 - 5 2 - * 6 2 / + A stack can be used to evaluate. (Later) • There is another way to evaluate: Scan from left to right and match operands with operators and simplify. (next slide) 8 4 - 5 2 - * 6 2 / + 5
  • 6. Evaluating a Postfix Expression (Scanning from left to right) INFIX (8-4) * (5-2) + 6/2 = 4 * 3 + 3 = 15 8 4 - 5 2 - * 6 2 / + 4 5 2 - * 6 2 / + 4 3 * 6 2 / + 12 6 2 / + 12 3 + 15 ANSWER 6
  • 7. Evaluating a Postfix Expression (Using a Stack) • We can use a stack to track the operands for the operators. Intermediate values are stored on the stack • See Algorithm on the following page (Available on My Elearning) 7
  • 8. Algorithm to Evaluate a Postfix Expression Stack usage: Store operands • Initialize and empty stack. • Repeat the following until the end of the expression is encountered: – Get the next token (constant, variable, arithmetic operator) in the RPN expression. – If the token is an operand, push it onto the stack. If it is an operator, then do the following: • Pop the two top values from the stack. (If the stack does not contain two items, an error due to a malformed RPN expression has occurred and the evaluation is terminated.) • Apply the operator to these two values. • Push the resulting value back onto the stack. • When the end of the expression is encountered, its value is at the top of the stack (and in fact, must be the only value on the stack). 8 4 - 5 2 - * 6 2 / + PUSH 8, PUSH 4. POP, POP APPLY the – operator PUSH RESULT = 4 Can you code the above algorithm? 8
  • 9. DETAILED EVALUATION E.G. Expression: INFIX: (8-4) * (5-2) + 6/2 is 4 * 3 + 3 = 15 Postfix 8 4 – 5 2 – * 6 2 / + EVALUATION SYM OPER? OPND? STACK COMMENT 8 Y 8 TOP PUSH 8 4 Y 8 4 TOP PUSH 4 - Y 4 TOP POP 4, 8, APPLY -, GET 4, PUSH RESULT (4) 5 Y 4 5 TOP PUSH 5 2 Y 4 5 2 TOP PUSH 2 - Y 4 3 TOP POP 2, 5, APPLY -, PUSH(3) 9
  • 10. DETAILED EVALUATION E.G. Expression: INFIX: (8-4) * (5-2) + 6/2 is 4 * 3 + 3 = 15 Postfix 8 4 – 5 2 – * 6 2 / + EVALUATION SYM OPER? OPND? STACK COMMENT * Y 12 TOP POP 3, 4 APPLY *, PUSH RESULT 12 6 Y 12 6 TOP PUSH 6 2 Y 12 6 2 TOP PUSH 2 / Y 12 3 TOP POP 2, 6 APPLY / PUSH RESULT 3 + Y 15 TOP POP 3, 12 APPLY +, PUSH RESULT 15 15 IS FINAL ANSWER. LATER: ALGORITHM FOR INFIX TO POSTFIX 10
  • 11. INFIX TO POSTFIX CONVERSION Stack usage: Store operators Initialize an empty stack of operators. While no error has occurred and the end of the infix expression has not been reached, do the following: Get the next input token (constant, variable, arithmetic operator, left parenthesis, right parenthesis) in the infix expression. If token is A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. (It is an error if the stack becomes empty with no left parenthesis found.) An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. Otherwise, pop and display the top stack element; then repeat the comparison of token with the new top stack item. Note: A left parenthesis in the stack is assumed to have a lower priority than that of the operators. An operand: Display it.When end of expr is reached, pop and display all remaining stack elements -Leestma and Nyhoff 11
  • 12. While no error has occurred and the end of the infix expression has not been reached, do the following: Get the next input token (constant, variable, arithmetic operator, left parenthesis, right parenthesis) in the infix expression. If token is A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. (It is an error if the stack becomes empty with no left parenthesis found.) An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. Otherwise, pop and display the top stack element; then repeat the comparison of token with the new top stack item. Note: A left parenthesis in the stack is assumed to have a lower priority than that of the operators. An operand: Display it. {At end, pop and display stack elements} 12
  • 13. While no error has occurred and the end of the infix expression has not been reached, do the following: Get the next input token (constant, variable, arithmetic operator, left parenthesis, right parenthesis) in the infix expression. If token is A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. (It is an error if the stack becomes empty with no left parenthesis found.) 13
  • 14. An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. Otherwise, pop and display the top stack element; then repeat the comparison of token with the new top stack item. Note: A left parenthesis in the stack is assumed to have a lower priority than that of the operators. An operand: Display it. {At end, pop and display stack elements} 14
  • 15. Left brackets must be stacked 15 A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. An operand: Display it. Otherwise, pop and display the top stack element; then repeat the comparison of token with the new top stack item.
  • 16. A left parenthesis in the stack is assumed to have a lower priority than that of the operators. 16 A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. An operand: Display it. Otherwise, pop and display the top stack element; then repeat the comparison of token
  • 17. Practice • Illustrate how (7-3) * (9-8/4) can be converted to postfix. -Postfix result on next slide. • - Students to attempt (with diagram during lecture) 17
  • 18. (7-3) * (9-8/4) to postfix POSTFIX 7 3 – 9 8 4 / - * - 18
  • 19. (7-3) * (9-8/4) to postfix Sym Comment Stack Output ( Push ( ( T 7 Output operand ( T 7 - Push – as prior > ( ( - T 7 3 Output operand ( - T 7 3 ) Pop and display until ( Empty 7 3 – * Push * * T 7 3 - ( Push ( * ( T 7 3 - 9 Output operand * ( T 7 3 – 9 - Push – as prior > ( * (- T 7 3 - 9 8 Output operand * (- T 7 3 – 9 8 19 A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. An operand: Display it. Otherwise, pop and display the top stack element; then repeat the
  • 20. (7-3) * (9-8/4) to postfix Sym Comment Stack Output LAST STEP: 8 Output operand * (- T 7 3 – 9 8 / ‘/’ prior > ‘-’ prior . Push / * (- / T 7 3 – 9 8 4 Output operand * (- / T 7 3 – 9 8 4 ) Pop and display until ( * (- / T 7 3 – 9 8 4 / - Discard ( * T END Pop and display remaining stack elements FINAL OUTPUT 7 3 – 9 8 4 / - * 20 A left parenthesis: Push it onto the stack. A right parenthesis: Pop and display stack elements until a left parenthesis is encountered. Do not display the left parenthesis. An operator: If the stack is empty or token has higher priority than the top stack element, push token onto the stack. An operand: Display it. Otherwise, pop and display the top stack element; then repeat the
  • 21. Practice • Convert (7 – 8 / 2 / 2) * ((7 – 2) * 3 – 6) • To postfix 21
  • 22. Practice (longer) • Convert (7 – 8 / 2 / 2) * ((7 – 2) * 3 – 6) to postfix • ANSWER 7 8 2 / 2 / - 7 2 – 3 * 6 - * 22
  • 23. Checking balanced parentheses (5 + 2 ) + 3( Push ( When ) encountered, pop ( - stack is empty Push ( End of expression reached. Error since stack is not empty. 23
  • 24. Wrap-Up Stacks • Review all code and applications 24