Elementary Data Structures - Stacks, Queues, & Lists, Amortized Analysis Trees

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 41

Elementary Data

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

Elementary Data Structures 2


Applications of Stacks
Direct applications
Page-visited 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 3


Method Stack in the JVM
The Java Virtual Machine (JVM) main() {
keeps track of the chain of int i = 5; bar
active methods with a stack foo(i); PC = 1
When a method is called, the } m=6
JVM pushes on the stack a foo(int j) {
frame containing foo
int k; PC = 3
Local variables and return value

Program counter, keeping track of


k = j+1; j=5
the statement being executed bar(k); k=6

When a method ends, its frame }


main
is popped from the stack and bar(int m) { PC = 2
control is passed to the method i=5
on top of the stack
}
Elementary Data Structures 4
Array-based Stack (2.1.1)
Algorithm pop():
if isEmpty() then
A simple way of throw EmptyStackException
implementing the else
Stack ADT uses an tt1
array
return S[t + 1]
We add elements
from left to right Algorithm push(o)
A variable t keeps if t = S.length 1 then
track of the index of throw FullStackException
the top element (size else
is t+1) tt+1
S[t] o

S
0 1 2 t
Elementary Data Structures 5
Performance and Limitations
Performance
Let n be the number of elements in the stack
The space used is O(n)
Each operation runs in time O(1)
Limitations
The maximum size of the stack must be defined a
priori and cannot be changed
Trying to push a new element into a full stack
causes an implementation-specific exception

Elementary Data Structures 6


Growable Array-based
Stack (1.5)
In a push operation, when
the array is full, instead of
throwing an exception, we Algorithm push(o)
can replace the array with a if t = S.length 1 then
larger one A new array of
size
How large should the new for i 0 to t do
array be? A[i] S[i]
incremental strategy: SA
increase the size by a
tt+1
constant c
S[t] o
doubling strategy: double the
size
Elementary Data Structures 7
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
Elementary Data Structures 8
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 + 2c + 3c + 4c + + kc =
n + c(1 + 2 + 3 + + k) =
n + ck(k + 1)/2
Since c is a constant, T(n) is O(n + k2), i.e.,
O(n2)
The amortized time of a push operation is O(n)

Elementary Data Structures 9


Direct Analysis of the
Doubling Strategy
We replace the array k = log2 n
times
geometric series
The total time T(n) of a series of
n push operations is 2
proportional to 4
1 1
n + 1 + 2 + 4 + 8 + + 2 =
k

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.

We set up a scheme for charging operations. This is known


as an amortization scheme.
The scheme must give us always enough money to pay for

the actual cost of the operation.


The total cost of the series of operations is no more than the

total amount charged.


(amortized time) (total $ charged) / (# operations)

Elementary Data Structures 11


Amortization Scheme for
the Doubling Strategy
Consider again the k phases, where each phase consisting of twice as many pushes as the
one before.
It costs one cyber-dollar for to push one element, excluding the growth of the array.
Growing the array from k to 2k costs k cyber-dollars for copying elements.
At the end of a phase we must have saved enough to pay for the array-growing push of the
next phase.
At the end of phase i we want to have saved i cyber-dollars, to pay for the array growth for
the beginning of the next phase.

$ $ $ $
$
$ $ $ $
$

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

Elementary Data Structures 13


Applications of Queues
Direct applications
Waiting lines
Access to shared resources (e.g., printer)
Multiprogramming
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures

Elementary Data Structures 14


Array-based Queue
Use an array of size N in a circular fashion
Two variables keep track of the front and rear
f index of the front element
r index immediately past the rear element
Array location r is kept empty

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

Elementary Data Structures 16


Queue Operations (cont.)
Operation enqueue Algorithm enqueue(o)
throws an exception if if size() = N 1 then
the array is full throw FullQueueException
This exception is else
implementation- Q[r] o
dependent r (r + 1) mod N

Q
0 1 2 f r
Q
0 1 2 r f

Elementary Data Structures 17


Queue Operations (cont.)
Operation dequeue Algorithm dequeue()
throws an exception if isEmpty() then
if the queue is empty throw EmptyQueueException
This exception is else
specified in the o Q[f]
queue ADT f (f + 1) mod N
return o

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

Elementary Data Structures 19


Singly Linked List
A singly linked list is a
concrete data structure next
consisting of a sequence
of nodes
Each node stores
element elem node
link to the next node

A B C D

Elementary Data Structures 20


Queue with a Singly Linked List
We can implement a queue with a singly linked list
The front element is stored at the first node
The rear element is stored at the last node
The space used is O(n) and each operation of the
Queue ADT takes O(1) time r
nodes

elements
Elementary Data Structures 21
List ADT (2.2.2)

The List ADT models a Accessor methods:


sequence of positions first(), last()
storing arbitrary objects before(p), after(p)
It allows for insertion Update methods:
and removal in the replaceElement(p, o),
middle swapElements(p, q)
Query methods:
insertBefore(p, o),
insertAfter(p, o),
isFirst(p), isLast(p)
insertFirst(o),
insertLast(o)
remove(p)

Elementary Data Structures 22


Doubly Linked List
A doubly linked list provides a natural prev next
implementation of the List ADT
Nodes implement Position and store:
element
link to the previous node elem node
link to the next node
Special trailer and header nodes

header nodes/positions trailer

elements
Elementary Data Structures 23
List ADT
How about array-based List?

Elementary Data Structures 24


Trees (2.3)
In computer science, a
tree is an abstract model ComputersRUs
of a hierarchical
structure
Sales Manufacturing R&D
A tree consists of nodes
with a parent-child
relation
US International Laptops Desktops
Applications:
Organization charts
File systems Europe Asia Canada
Programming
environments

Elementary Data Structures 25


Tree Terminology
Root: node without parent (A) Subtree: tree consisting of
Internal node: node with at least a node and its
one child (A, B, C, F) descendants
External node (a.k.a. leaf ): node A
without children (E, I, J, K, G, H, D)
Ancestors of a node: parent,
grandparent, grand-grandparent, B C D
etc.
Depth of a node: number of
ancestors E F G H
Height of a tree: maximum depth
of any node (3)
Descendant of a node: child, I J K
grandchild, grand-grandchild, etc.

Elementary Data Structures subtree26


Tree ADT (2.3.1)
Generic methods: Query methods:
integer size() boolean isInternal(p)
boolean isEmpty() boolean isExternal(p)
objectIterator elements() boolean isRoot(p)
positionIterator positions() Update methods:
Accessor methods: swapElements(p, q)
position root() object replaceElement(p, o)
position parent(p) Additional update methods
positionIterator children(p) may be defined by data
structures implementing the
Tree ADT

Elementary Data Structures 27


Preorder Traversal (2.3.2)
A traversal visits the nodes of a Algorithm preOrder(v)
tree in a systematic manner
In a preorder traversal, a node is
visit(v)
visited before its descendants for each child w of v
Application: print a structured preorder (w)
document
1
Make Money Fast!

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

Elementary Data Structures 28


Postorder Traversal (2.3.2)
In a postorder traversal, a node is
visited after its descendants Algorithm postOrder(v)
Application: compute space used for each child w of v
by files in a directory and its
subdirectories postOrder (w)
The directory itself
Its children directories visit(v)
The files
9
cs16/

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

Elementary Data Structures 29


Amortized Analysis of
Tree Traversal
Time taken in preorder or postorder traversal
of an n-node tree is proportional to the sum,
taken over each node v in the tree, of the time
needed for the recursive call for v.
The call for v costs $(cv + 1), where cv is the
number of children of v
For the call for v, charge one cyber-dollar to v and
charge one cyber-dollar to each child of v.
Each node (except the root) gets charged twice:
once for its own call and once for its parents call.
Therefore, traversal time is O(n).
Elementary Data Structures 30
Binary Trees (2.3.3)
A binary tree is a tree with the Applications:
following properties: arithmetic expressions
Each internal node has at most
two children (proper) decision processes
The children of a node are an searching
ordered pair (left child comes
before right child) A
We call the children of an internal
node left child and right child
Alternative recursive definition: a B C
binary tree is either
a tree consisting of a single node,
or
D E F G
a tree whose root has an ordered
pair of children, each of which is a
binary tree
H I

Elementary Data Structures 31


Arithmetic Expression Tree
Binary tree associated with an arithmetic expression
internal nodes: operators
external nodes: operands
Example: arithmetic expression tree for the
expression (2 (a 1) (3 b))

2 3 b

a 1

Elementary Data Structures 32


Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision

Want a fast meal?


Yes No

How about coffee? On expense account?


Yes No Yes No

Starbucks In N Out Antoine's Dennys

Elementary Data Structures 33


Properties of Binary Trees
Notation Properties:
n number of nodes e i 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

Elementary Data Structures 34


Inorder Traversal
In an inorder Algorithm inOrder(v)
traversal a node is if isInternal (v)
visited after its left
subtree and before inOrder (leftChild (v))
its right subtree visit(v)
if isInternal (v)
6 inOrder (rightChild (v))
2 8

1 4 7 9

3 5

Elementary Data Structures 35


Printing Arithmetic Expressions
Specialization of an inorder Algorithm printExpression(v)
traversal
print ( before traversing
if isInternal (v)
left subtree print(()
print operand or operator inOrder (leftChild (v))
when visiting node
print ) after traversing print(v.element ())
right subtree
if isInternal (v)
inOrder (rightChild (v))
print ())

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)

from below (inorder)

on the right (postorder)

L R
B
2 3 2

5 1

Elementary Data Structures 37


Linked Data Structure for
Representing Trees (2.3.4)
A node is represented by
an object storing
Element
Parent node B
Sequence of children
nodes
A D F
B

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

Elementary Data Structures 39


Array-Based Representation of
Binary Trees
nodes are stored in an array
1
A

2 3
B D

let rank(node) be defined as follows:


rank(root) = 1 4 5 6 7
E F C J
if node is the left child of parent(node),
rank(node) = 2*rank(parent(node))
if node is the right child of parent(node),
rank(node) = 2*rank(parent(node))+1 10 11
G H
Elementary Data Structures 40
Array-Based Representation of
Binary Trees

Space requirement
( n 1) / 2
N 2

Elementary Data Structures 41

You might also like