Elementary Data
Structures
Stacks, Queues, & Lists
Amortized analysis
Trees
Elementary Data Structures
2
The Stack ADT (§4.2.1)
The
Stack
ADT stores
arbitrary objects
Insertions and deletions
follow the lastin firstout
scheme
Think of a springloaded
plate dispenser
Main stack operations:
push
(Object o): inserts
element o
pop
(): removes and returns
the last inserted element
Auxiliary stack
operations:
top
(): returns the last
inserted element without
removing it
size
(): returns the
number of elements
stored
isEmpty
(): a Boolean
value indicating whether
no elements are stored
Elementary Data Structures
3
Applications of Stacks
Direct applications
Pagevisited history in a Web browser
Undo sequence in a text editor
Chain of method calls in the Java Virtual
Machine or C++ runtime environment
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
Elementary Data Structures
4
Arraybased Stack (§4.2.2)
A simple way of
implementing the
Stack ADT uses an
array
We add elements
from left to right
A variable t keeps
track of the index of
the top element
(size is t+1)
S
0
1
2
t
…
Algorithm
pop
():
if
isEmpty
()
then
throw
EmptyStackException
else
t
←
t
−
1
return
S
[
t
+ 1]
Algorithm
push
(
o
)
if
t
=
S.length
−
1
then
throw
FullStackException
else
t
←
t
+ 1
S
[
t
]
←
o
Elementary Data Structures
5
Growable Arraybased
Stack
In a push operation, when
the array is full, instead of
throwing an exception, we
can replace the array with
a larger one
How large should the new
array be?
incremental strategy:
increase the size by a
constant
c
doubling strategy: double
the size
Algorithm
push
(
o
)
if
t
=
S.length
−
1
then
A
←
new array of
size …
for
i
←
0
to
t
do
A
[
i
]
←
S
[
i
]
S
←
A
t
←
t
+ 1
S
[
t
]
←
o
Elementary Data Structures
6
Comparison of the
Strategies
We compare the incremental strategy and
the doubling strategy by analyzing the total
time
T
(
n
)
needed to perform a series of
n
push operations
We assume that we start with an empty
stack represented by an array of size
1
We call
amortized time
of a push operation
the average time taken by a push over the
series of operations, i.e.,
T
(
n
)/
n
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Elementary Data Structures
7
Analysis of the
Incremental Strategy
We replace the array
k
=
n
/
c
times
The total time
T
(
n
)
of a series of
n
push
operations is proportional to
n
+
c
+
2
c
+ 3
c
+ 4
c
+
…
+
kc
=
n
+
c
(1 + 2 + 3 + … +
k
)
=
n
+
ck
(
k
+ 1)/2
Since
c
is a constant,
T
(
n
)
is
O
(
n
+
k
2
)
, i.e.,
O
(
n
2
)
The amortized time of a push operation is
O
(
n
)
Elementary Data Structures
8
Direct Analysis of the
Doubling Strategy
We replace the array
k
=
log
2
n
times
The total time
T
(
n
)
of a series
of
n
push operations is
proportional to
n
+
1 + 2 + 4 + 8 + …+ 2
k
=
n
+
2
k
+ 1
−
1
=
2
n
−
1
T
(
n
)
is
O
(
n
)
The amortized time of a push
operation is
O
(1)
geometric series
1
2
1
4
8
Elementary Data Structures
9
The
accounting method
determines the amortized
running time with a system of credits and debits
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Staff
 Data Structures, Tree traversal, Elementary Data Structures

Click to edit the document details