Session 3
Session 3
Postfix:
no
parentheses,
no
precedence
3.4 Evaluation of Expressions
(5/14)
Evaluating postfix expressions is much
simpler than the evaluation of infix
expressions:
◦ There are no parentheses to consider.
◦ To evaluate an expression we make a single left-
to-right scan of it.
◦ We can evaluate
an expression
easily by using
a stack
Figure 3.14 shows
this processing
when the input is
nine character
string 6 2/3-4 2*+
3.4 Evaluation of Expressions (6/14)
Representation
◦ We now consider the representation of both the
stack and the expression
3.4 Evaluation of Expressions
(7/14)
Get Token
3.4 (8/14)
Evaluation of
Postfix Expression
3.4 Evaluation of Expressions
(9/14)
string: 6 2/3-4 2*+
6 2 / 3 - 4 2 *
+ 2 2
the answer is
notnot
an an
is not
an
is an
operator,
anoperator,
not not
an
is an
an
is
end
operator,
an of
operator,
string,
operator,
operator,
popoperator,
pop
put
two
put
twooperator,
put
operator,
pop pop
two
pop
put
two
the
putstack 1 4*2
2
3
4
intointo
the
6/2-3 + 4
elements
the
stack
into
elements
thethe
stack
stack
the
into
ofstack
of
into
elements
theelements
stack thethe
and
the
stack
stack
stack
get
answer
of of
stack 0 6/2-3+4*2
6/2-3
6/2
6
*2 top
STACK
now, top must
+1
-2
-1
3.4 Evaluation of Expressions
(10/14)
We can describe am algorithm for producing a
postfix expression from an infix one as follows
(1) Fully parenthesize expression
a/b-c+d*e-a*c
((((a / b) - c) + (d * e)) - (a * c))
(2) All operators replace their corresponding
right parentheses
((((a / b) - c) + (d * e)) - (a * c))
* * two
/ - + - passes
(3)Delete all parentheses
ab/c-de*+ac*-
The order of operands is the same in infix and
postfix
3.4 Evaluation of Expressions (11/14)
Example 3.3 [Simple expression]: Simple expression a+b*c,
which yields abc*+ in postfix.
icp isp
1
2 1
1 2
1
3 1
3
0 3
1 1
3
2 30
0 0
1 1
2 1
2
1 match )2
1
1
9 1
3
3 3
1
0 1
3
3
3.4 Evaluation of Expressions
(12/14)
Algorithm to convert from infix to postfix
◦ Assumptions:
operators: (, ), +, -, *, /, %
operands: single digit integer or variable of one
character
1. Operands are taken out immediately
2. Operators are taken out of the stack as long as their
in-stack precedence (isp) is higher than or equal to the
incoming precedence (icp) of the new operator
3. ‘(‘ has low isp, and high icp
op ( ) + - * / %
eos
Isp0 19 12 12 13 13 13 0
Icp20 19 12 12 13 13 13 0
precedence stack[MAX_STACK_SIZE];
/* isp and icp arrays -- index is value of precedence
lparen, rparen, plus, minus, times, divide, mod, eos */
static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};
static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};
3.4 Evaluation of Expressions
(13/14)
a * ( b +
c )
operand
/
operato
d
operato
operand
operato
operand
operato
operato
operand
eos
, print
r r , print r , print
r r , print
out out out out
pushthe
pop into
stack
the and 2 +
stack
printout
the
operator
isp of“)”,
“(
“/
“+““ pop
is
is 13
0
12and
and
and
andprint
the
the
theicp
icp
out
icpof
of
of “*
“*
“
“(is“ 13
until is“(”
20
13
1 (
output 0 */
to
a b c + * d / p
stac
now, top must k
-+1
1
3.4 Evaluation of Expressions
(14/14)
Complexity: (n)
◦ The total time spent
here is (n) as the
number of tokens
that get stacked and
unstacked is linear
in n
◦ where n is the
number of tokens in
the expression
Sequences
0 N-1
V
0 1 2 r n
01/25/2025
01:24 PM
Vectors 22
Array based Vector:
Insertion
In operation insertAtRank(r,o) we need to
make room for the new element by shifting
forward the n - r elements V[r], …, V[n - 1]
In the worst case (r = 0), this takes O(n) time
V
0 1 2 r n
V
0 1 2 r n
V o
0 1 2 r n
01/25/2025
01:24 PM
Vectors 23
Deletion
In operation removeAtRank(r) we need to fill
the hole left by the removed element by
shifting backward the n - r - 1 elements V[r +
1], …, V[n - 1]
In the worst case (r = 0), this takes O(n) time
V o
0 1 2 r n
V
0 1 2 r n
V
0 1 2 r n
01/25/2025
01:24 PM
Vectors 24
Performance
In the array based implementation of a Vector
◦ The space used by the data structure is O(n)
◦ Size(), isEmpty(), elemAtRank(r) and
replaceAtRank(r,o) run in O(1) time
◦ insertAtRank(r,o) and removeAtRank(r) run in O(n)
time
If we use the array in a circular fashion,
insertAtRank(0,o) and removeAtRank(0) run
in O(1) time
In an insertAtRank(r,o) operation, when the
array is full, instead of throwing an exception,
we can replace the array with a larger one
01/25/2025
01:24 PM
Vectors 25
Stacks versus Vectors
• If you can only push_back and pop_back,
then this is LIFO access.
• What is the difference from a stack?
• Interface is different. For example, can
access elements in the middle in a vector
vs
26
Vectors vs Arrays
Apart from usual index operator (“[ ]”), elements can be
accessed by member function at which performs range
checking.
elemAtRank(r), O(1) ?
ReplaceAtRank(r,o)
01/25/2025
01:24 PM
Vectors 31
Position ADT
As in vectors, the Position ADT models the notion
of place within a data structure where a single
object is stored
Positions provide a unified view of diverse ways
of storing data, such as
◦ a cell of an array
◦ a node of a linked list
Member functions:
◦ Object& element(p): returns the element stored
at this position
◦ bool isNull(): returns true if this is a null position
01/25/2025
01:24 PM
Vectors 32
List ADT
ADT that captures linked
lists
The List ADT models a • Accessor methods:
sequence of positions
storing arbitrary – first(), last()
objects – before(p), after(p)
◦ establishes a before/after • Update methods:
relation between
positions – replaceElement(p, o),
It allows for insertion swapElements(p, q)
and removal in the – insertBefore(p, o),
“middle” insertAfter(p, o),
Query methods: – insertFirst(o),
◦ isFirst(p), isLast(p) insertLast(o)
Generic methods: – remove(p)
33
◦ size(), isEmpty()
List ADT
Query methods:
◦ isFirst(p), isLast(p) :
return boolean indicating if the given position is
the first or last, resp.
Accessor methods
◦ first(), last():
return the position of the first or last, resp.,
element of S
an error occurs if S is empty
◦ before(p), after(p):
return the position of the element of S
preceding or following, resp, the one at position
p
an error occurs if S is empty, or p is the first or
last, resp., position
01/25/2025
01:24 PM
Vectors 34
List ADT
Update Methods
◦ replaceElement(p, e)
Replace the element at position p with e
◦ swapElements(p, q)
Swap the elements stored at positions p & q
◦ insertBefore(p, o), insertAfter(p, o),
Insert a new element o into S before or after, resp., position p
Output: position of the newly inserted element
◦ insertFirst(o), insertLast(o)
Insert a new element o into S as the first or last, resp.,
element
Output: position of the newly inserted element
◦ remove(p)
Remove the element at position p from S
01/25/2025
01:24 PM
Vectors 35
Exercise:
A B C D 01/25/2025
01:24 PM
Vectors 36
Exercise:
Describe how to implement the following
list ADT operations using a doubly-linked
list
◦ list ADT operations: first(), last(), before(p),
after(p)
◦ For each operation, explain how it is
implemented and provide the running time
prev next
• Doubly-Linked List Nodes node
implement Position and store:
• element elem
• link to previous node
• link to next node heade trailer
r
• Special trailer and header
nodes
01/25/2025
element
01:24 PM
s 37
Vectors
Insertion
We visualize operation insertAfter(p, X) which returns position q
p
A B C
p
p
A B q C
X
p q
A B X C
01/25/2025
q 01:24 PM
Vectors 38
Deletion
We visualize remove(p), where p = last()
p
A B C D
A B C p
A B C
01/25/2025
01:24 PM
Vectors 39
Performance
In the implementation of the List ADT by
means of a doubly linked list
◦ The space used by a list with n elements is O(n)
◦ The space used by each position of the list is
O(1)
◦ All the operations of the List ADT run in O(1)
time
◦ Operation element() of the
Position ADT runs in O(1) time
01/25/2025
01:24 PM
Vectors 40
STL list class
Functions in the STL list class
◦ Size() - return #elts in list, empty() - boolean
◦ Front(), back() - return references to first/last elts
◦ Push_front(e), push_back(e) - insert e at front/end
◦ Pop_front(), pop_back() - remove first/last elt
◦ List() - creates an empty list
Similarities & Differences with book’s List ADT
◦ STL front() & back() correspond to first() & last() except the
STL functions return the element & not its position
◦ STL push() & pop() are equiv to List ADT insert and remove
when applied to the beginning & end of the list
◦ STL also provides fcns for inserting & removing from
arbitrary positions in the list - these use iterators
01/25/2025
01:24 PM
Vectors 41
List Summary
List Operation Complexity for different
implementations
01/25/2025
01:24 PM
Vectors 42
Sequence ADT
Generalizes vectors and lists