SlideShare une entreprise Scribd logo
1  sur  36
Lecture No.07
Data Structures
Infix to Postfix
Infix Postfix
A + B A B +
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) A B + C D – *
A  B * C – D + E/F A B  C*D – E F/+
Infix to Postfix
 Note that the postfix form an expression
does not require parenthesis.
 Consider ‘4+3*5’ and ‘(4+3)*5’. The
parenthesis are not needed in the first but
they are necessary in the second.
 The postfix forms are:
4+3*5 435*+
(4+3)*5 43+5*
Evaluating Postfix
 Each operator in a postfix expression
refers to the previous two operands.
 Each time we read an operand, we push it
on a stack.
 When we reach an operator, we pop the
two operands from the top of the stack,
apply the operator and push the result
back on the stack.
Evaluating Postfix
Stack s;
while( not end of input ) {
e = get next element of input
if( e is an operand )
s.push( e );
else {
op2 = s.pop();
op1 = s.pop();
value = result of applying operator ‘e’ to op1 and op2;
s.push( value );
}
}
finalresult = s.pop();
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
 7 2 49 49
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
 7 2 49 49
3 7 2 49 49,3
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
Evaluating Postfix
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
Converting Infix to Postfix
 Consider the infix expressions ‘A+B*C’
and ‘ (A+B)*C’.
 The postfix versions are ‘ABC*+’ and
‘AB+C*’.
 The order of operands in postfix is the
same as the infix.
 In scanning from left to right, the operand
‘A’ can be inserted into postfix expression.
Converting Infix to Postfix
 The ‘+’ cannot be inserted until its second
operand has been scanned and inserted.
 The ‘+’ has to be stored away until its
proper position is found.
 When ‘B’ is seen, it is immediately inserted
into the postfix expression.
 Can the ‘+’ be inserted now? In the case of
‘A+B*C’ cannot because * has
precedence.
Converting Infix to Postfix
 In case of ‘(A+B)*C’, the closing
parenthesis indicates that ‘+’ must be
performed first.
 Assume the existence of a function
‘prcd(op1,op2)’ where op1 and op2 are
two operators.
 Prcd(op1,op2) returns TRUE if op1 has
precedence over op2, FASLE otherwise.
Converting Infix to Postfix
 prcd(‘*’,’+’) is TRUE
 prcd(‘+’,’+’) is TRUE
 prcd(‘+’,’*’) is FALSE
 Here is the algorithm that converts infix
expression to its postfix form.
 The infix expression is without
parenthesis.
Converting Infix to Postfix
1. Stack s;
2. While( not end of input ) {
3. c = next input character;
4. if( c is an operand )
5. add c to postfix string;
6. else {
7. while( !s.empty() && prcd(s.top(),c) ){
8. op = s.pop();
9. add op to the postfix string;
10. }
11. s.push( c );
12. }
13. while( !s.empty() ) {
14. op = s.pop();
15. add op to postfix string;
16. }
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC * +
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC * +
ABC * +
Converting Infix to Postfix
 Handling parenthesis
 When an open parenthesis ‘(‘ is read, it
must be pushed on the stack.
 This can be done by setting prcd(op,‘(‘ ) to
be FALSE.
 Also, prcd( ‘(‘,op ) == FALSE which
ensures that an operator after ‘(‘ is pushed
on the stack.
Converting Infix to Postfix
 When a ‘)’ is read, all operators up to the
first ‘(‘ must be popped and placed in the
postfix string.
 To do this, prcd( op,’)’ ) == TRUE.
 Both the ‘(‘ and the ‘)’ must be discarded:
prcd( ‘(‘,’)’ ) == FALSE.
 Need to change line 11 of the algorithm.
Converting Infix to Postfix
if( s.empty() || symb != ‘)’ )
s.push( c );
else
s.pop(); // discard the ‘(‘
prcd( ‘(‘, op ) = FALSE for any operator
prcd( op, ‘(’ ) = FALSE for any operator
other than ‘(’
prcd( op, ‘)’ ) = TRUE for any operator
other than ‘(‘
prcd( ‘)’, op ) = error for any operator.

Contenu connexe

Similaire à data structure and algorithm by bomboat_3

Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxsandeep54552
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptxJAYAPRIYAR7
 
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 .pdfarihantsherwani
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdfsoniasharmafdp
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid ali rashid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfixSelf-Employed
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptxRockyIslam5
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionsecomputernotes
 

Similaire à data structure and algorithm by bomboat_3 (20)

Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
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
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
 
C applications
C applicationsC applications
C applications
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 

Dernier

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Dernier (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

data structure and algorithm by bomboat_3

  • 2. Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+
  • 3. Infix to Postfix  Note that the postfix form an expression does not require parenthesis.  Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second.  The postfix forms are: 4+3*5 435*+ (4+3)*5 43+5*
  • 4. Evaluating Postfix  Each operator in a postfix expression refers to the previous two operands.  Each time we read an operand, we push it on a stack.  When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.
  • 5. Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } } finalresult = s.pop();
  • 6. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6
  • 7. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2
  • 8. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3
  • 9. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5
  • 10. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1
  • 11. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3
  • 12. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8
  • 13. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2
  • 14. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4
  • 15. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7
  • 16. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7
  • 17. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2
  • 18. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49
  • 19. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3
  • 20. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52
  • 21. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52
  • 22. Converting Infix to Postfix  Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’.  The postfix versions are ‘ABC*+’ and ‘AB+C*’.  The order of operands in postfix is the same as the infix.  In scanning from left to right, the operand ‘A’ can be inserted into postfix expression.
  • 23. Converting Infix to Postfix  The ‘+’ cannot be inserted until its second operand has been scanned and inserted.  The ‘+’ has to be stored away until its proper position is found.  When ‘B’ is seen, it is immediately inserted into the postfix expression.  Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence.
  • 24. Converting Infix to Postfix  In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first.  Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators.  Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.
  • 25. Converting Infix to Postfix  prcd(‘*’,’+’) is TRUE  prcd(‘+’,’+’) is TRUE  prcd(‘+’,’*’) is FALSE  Here is the algorithm that converts infix expression to its postfix form.  The infix expression is without parenthesis.
  • 26. Converting Infix to Postfix 1. Stack s; 2. While( not end of input ) { 3. c = next input character; 4. if( c is an operand ) 5. add c to postfix string; 6. else { 7. while( !s.empty() && prcd(s.top(),c) ){ 8. op = s.pop(); 9. add op to the postfix string; 10. } 11. s.push( c ); 12. } 13. while( !s.empty() ) { 14. op = s.pop(); 15. add op to postfix string; 16. }
  • 27. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A
  • 28. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A +
  • 29. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB +
  • 30. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + *
  • 31. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + *
  • 32. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * +
  • 33. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * +
  • 34. Converting Infix to Postfix  Handling parenthesis  When an open parenthesis ‘(‘ is read, it must be pushed on the stack.  This can be done by setting prcd(op,‘(‘ ) to be FALSE.  Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘ is pushed on the stack.
  • 35. Converting Infix to Postfix  When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string.  To do this, prcd( op,’)’ ) == TRUE.  Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE.  Need to change line 11 of the algorithm.
  • 36. Converting Infix to Postfix if( s.empty() || symb != ‘)’ ) s.push( c ); else s.pop(); // discard the ‘(‘ prcd( ‘(‘, op ) = FALSE for any operator prcd( op, ‘(’ ) = FALSE for any operator other than ‘(’ prcd( op, ‘)’ ) = TRUE for any operator other than ‘(‘ prcd( ‘)’, op ) = error for any operator.