Elementary Data Structures - Stacks, Queues, & Lists, Amortized Analysis Trees
Elementary Data Structures - Stacks, Queues, & Lists, Amortized Analysis Trees
Elementary Data Structures - Stacks, Queues, & Lists, Amortized Analysis Trees
Structures
Stacks, Queues, & Lists
Amortized analysis
Trees
The Stack ADT (2.1.1)
The Stack ADT (Abstract
Data Type) stores arbitrary
objects Auxiliary stack
Insertions and deletions operations:
follow the last-in first-out object top(): returns the
scheme last inserted element
Think of a spring-loaded without removing it
plate dispenser
integer size(): returns
the number of elements
Main stack operations: stored
push(object): inserts an boolean isEmpty():
element indicates whether no
object pop(): removes and elements are stored
returns the last inserted
element
n 2k + 1 1 = 2n 1
8
T(n) is O(n)
The amortized time of a push
operation is O(1)
Elementary Data Structures 10
Accounting Method Analysis of
the Doubling Strategy
Amortization: to pay of gradually by making periodic payments
Rather than focusing on each operation separately, it consider the running time of a series of these operations.
We view a computer as a coin-operated device requiring 1 cyber-dollar for a constant amount of computing.
$ $ $ $
$
$ $ $ $
$
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
We charge $3 for a push. The $2 saved for a regular push are stored
in the second half of the array. Thus, we will have 2(i/2)=i cyber-dollars
saved at then end of phase i.
Therefore, each push runs in O(1) amortized time; n pushes run in O(n)
time. Elementary Data Structures 12
The Queue ADT (2.1.2)
The Queue ADT stores arbitrary Auxiliary queue operations:
objects object front(): returns the
Insertions and deletions follow element at the front without
the first-in first-out scheme removing it
Insertions are at the rear of the
integer size(): returns the
number of elements stored
queue and removals are at the
boolean isEmpty(): indicates
front of the queue
whether no elements are
Main queue operations: stored
enqueue(object): inserts an Exceptions
element at the end of the Attempting the execution of
queue
dequeue or front on an
object dequeue(): removes and empty queue throws an
returns the element at the front EmptyQueueException
of the queue
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
0 1 2 r f
Elementary Data Structures 15
Queue Operations
We use the Algorithm size()
modulo operator return (N f + r) mod N
(remainder of
Algorithm isEmpty()
division) return (f r)
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Elementary Data Structures 18
Growable Array-based Queue
In an enqueue operation, when the array is
full, instead of throwing an exception, we
can replace the array with a larger one
Similar to what we did for an array-based
stack
The enqueue operation has amortized
running time
O(n) with the incremental strategy
O(1) with the doubling strategy
A B C D
elements
Elementary Data Structures 21
List ADT (2.2.2)
elements
Elementary Data Structures 23
List ADT
How about array-based List?
2 5 9
1. Motivations 2. Methods References
6 7 8
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
2 3 b
a 1
e number of n 2e 1
external nodes
h i
i number of internal
nodes h (n 1)2
h height e 2h
h log e
2
h log2 (n 1) 1
1 4 7 9
3 5
2 3 b
((2 (a 1)) (3 b))
a 1
Elementary Data Structures 36
Euler Tour Traversal
Generic traversal of a binary tree
Includes a special cases the preorder, postorder and inorder traversals
Walk around the tree and visit each node three times:
on the left (preorder)
L R
B
2 3 2
5 1
A D F
C E
C E
Elementary Data Structures 38
Linked Data Structure for
Binary Trees
A node is represented
by an object storing
Element
Parent node
B
Left child node
Right child node
B A D
A D
C E C E
Space requirement
( n 1) / 2
N 2