Graphs and Trees-III Prefix, Postfix and Infix Notations

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Graphs and Trees-III

Prefix, Postfix and Infix


notations
Mujtaba Husnain
Lecture 5 (part -3), 31 May 2020
MCS 2nd Semester (Fall 2019)
Morning, Evening-B
DCS&IT, IUB
Agenda of Today Lecture
• Depth First Search
• Breadth First Search
• Infix, postfix and prefix notations
• Summary
Depth First Search (DFS)
• Depth-first search (DFS) is
an algorithm for traversing or
searching tree (or graph) data
structures. 
• The algorithm starts at the root In above graph, let we consider 1 as root.
node (selecting some arbitrary According to definition, we start from 1 and
node as the root node in the case traverse the remaining vertices in any direction
until a dead end is reached i.e. no vertex remain
of a graph) and explores as far as unvisited. Then backtrack to the root node and
possible along each branch. traverse the other nodes in same way until all
the vertices are visited.
One of DFS traversal will be 1 2 3 4 5 6 7 8 9 10
11 12.
Other DFS may be 1 8 12 9 11 10 7 2 6 3 5 4 and
so on.
Breadth First Search (BFS)
• Breadth-first search (BFS) is
an algorithm for traversing or
-searching tree or graph data In above graph, let we consider 1 as root.
structures. According to definition, we start from 1 and
traverse the neighbor vertices in any
• It starts at the tree root (or some direction until a dead end is reached i.e. no
vertex remain unvisited. Then backtrack to
arbitrary node of a graph), and the first visited neighbor and start this
explores all of the neighbor process in same way until all the vertices are
visited.
nodes at the present depth prior One of BFS traversal will be 1 2 7 8 3 6 9 12 4
to moving on to the nodes at the 5 10 11.
Other DFS may be 1 8 7 2 9 12 3 6 10 11 4 5
next depth level. and so on.
Infix, postfix and prefix notations
• The mathematical and algebraic statements are also used in computer
programming. The notation we use in writing the algebraic instruction is
called as infix notation. e.g. “a = 3 + 2 X 5” is infix notation because the
operators (+ and *) are appearing “in” between the operands.
• The computer does not know the order in which the operators will apply
first to operands. We also apply the parenthesis to the expression that
should be calculated first.
• If the parenthesis are not written manually, the computer converts the
infix to either pre- or post-fix notations. In pre-fix notation, the operator
comes before the operands and in post-fix the operator appears after the
operands.
Infix, postfix and prefix notations
• For example, the instruction written in-fix notation, below
B+C–D
can be written as pre-fix as +BC – D. The rule to write in pre-fix is to
write the operator first and then the operands on which it is applied. In
example above, first the values of B and C are added and then
subtracted with value of D. That’s why we have mentioned first the +
operator before B and C and then sum of B and C is subtracted with
value of D.
Similarly, in post-fix, the B + C – D can be written as BCD+-.
Infix, postfix and prefix notations
Infix Expression Prefix Expression Postfix Expression
A+B +AB AB+
A+B+C (+AB )+ C (AB+) + C
++ABC AB+C+
A+B*C A + *(BC) A + (BC)*
+A*(BC) ABC*+

Infix Expression Prefix Expression Postfix Expression


A+B*C+D A + *(BC) + D A + (BC)* + D
(+A*BC) + D (ABC*+) + D
++A*BCD ABC*+D+
(A + B) * (C + D) *+AB+CD AB+CD+*
A*B+C*D +*AB*CD AB*CD*+

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy