Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
12Activity
0 of .
Results for:
No results containing your search query
P. 1
Data Structures

Data Structures

Ratings: (0)|Views: 570 |Likes:
Published by hiscasio
The Stack ADT stores
arbitrary objects
Insertions and deletions
follow the last-in first-out
scheme
Think of a spring-loaded
plate dispenser
Main stack operations:
The Stack ADT stores
arbitrary objects
Insertions and deletions
follow the last-in first-out
scheme
Think of a spring-loaded
plate dispenser
Main stack operations:

More info:

Published by: hiscasio on Mar 22, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/11/2011

pdf

text

original

 
Elementary DataStructures
Stacks, Queues, & Lists Amortized analysisTrees
Elementary Data Structures2
The Stack ADT (§4.2.1)
TheStack  ADT storesarbitrary objectsInsertions and deletionsfollow the last-in first-outschemeThink of a spring-loadedplate dispenserMain stack operations:
push(Object o): insertselement o
pop(): removes and returnsthe last inserted element
 Auxiliary stack operations:
top(): returns the lastinserted element withoutremoving it
size(): returns thenumber of elementsstored
isEmpty(): a Booleanvalue indicating whetherno elements are stored
Elementary Data Structures3
 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 VirtualMachine or C++ runtime environment
Indirect applications
 Auxiliary data structure for algorithms
Component of other data structures
Elementary Data Structures4
 Array-based Stack (§4.2.2)
 A simple way of implementing theStack ADT uses anarrayWe add elementsfrom left to right A variable t keepstrack of the index of the top element(size is t+1)
 S 
012
Algorithm
 pop
():
if 
isEmpty
()
thenthrow
 EmptyStackException
else
1
return
 S 
[
+1]
Algorithm
 push
(
o
)
if 
=
 S.length
1
thenthrow
FullStackException
else
+1
 S 
[
]
o
Elementary Data Structures5
Growable Array-basedStack 
In a push operation, whenthe array is full, instead of throwing an exception, wecan replace the array witha larger oneHow large should the newarray be?
incremental strategy:increase the size by aconstant
c
doubling strategy: doublethe size
Algorithm
 push
(
o
)
if 
=
 S.length
1
then
 A
new array of size …
for
0
to
do
 A
[
]
 S 
[
]
 S 
 A
+1
 S 
[
]
o
Elementary Data Structures6
Comparison of theStrategies
We compare the incremental strategy andthe doubling strategy by analyzing the totaltime
(
n
)
needed to perform a series of 
n
push operationsWe assume that we start with an emptystack represented by an array of size
1
We call
amortized time
of a push operationthe average time taken by a push over theseries of operations, i.e.,
(
n
)/
n
 
Elementary Data Structures7
 Analysis of theIncremental Strategy
We replace the array
=
n
 /
c
timesThe total time
(
n
)
of a series of 
n
pushoperations is proportional to
n
+
c
+
2
c
+ 3
c
+ 4
c
+
+
kc
=
n
+
c
(1 + 2 + 3 + … +
)
=
n
+
ck 
(
+ 1)/2
Since
c
is a constant,
(
n
)
is
O
(
n
+
2
)
,i.e.,
O
(
n
2
)
The amortized time of a push operation is
O
(
n
)
Elementary Data Structures8
Direct Analysis of theDoubling Strategy
We replace the array
=
log
2
n
timesThe total time
(
n
)
of a seriesof 
n
push operations isproportional to
n
+
1 + 2 + 4 + 8 + …+ 2
=
n
+
2
+ 1
1
=
2
n
1
(
n
)
is
O
(
n
)
The amortized time of a pushoperation is
O
(1)
geometric series
12148
Elementary Data Structures9
The
accounting method
determines the amortizedrunning time with a system of credits and debitsWe view a computer as acoin-operated devicerequiring1 cyber-dollar for a constant amount of computing.
 Accounting Method Analysisof the Doubling Strategy
We set up a scheme for charging operations. Thisis known as an
amortization scheme
.
The scheme must give us always enough money topay for the actual cost of the operation.
The total cost of the series of operations is no morethan the total amount charged.(amortized time)
(total $ charged) / (# operations)
Elementary Data Structures10
 Amortization Scheme forthe Doubling Strategy
Consider again the
phases, where each phase consisting of twiceas many pushes as the one before. At the end of a phase we must have saved enough to pay for thearray-growing push of the next phase. At the end of phase
we want to have saved
cyber-dollars, to payfor the array growth for the beginning of the next phase.
02456731
$$$$$$$$
0245678911310121314151
$$
We charge$3for a push. The$2saved for a regular push are  “stored” in the second half of the array. Thus, we will have2(
 /2)=
cyber-dollars saved at then end of phase
i.
Therefore, each push runs in
(1) amortized time;
pushes runin
(
) time.
 
Elementary Data Structures11
The Queue ADT
(§4.3.1)
TheQueue ADT storesarbitrary objectsInsertions and deletions followthe first-in first-out schemeInsertions are at the rear of the queue and removals areat the front of the queueMain queue operations:
enqueue(object o): insertselement o at the end of thequeue
dequeue(): removes andreturns the element at thefront of the queue
 Auxiliary queue operations:
front(): returns the elementat the front without removingit
size(): returns the number of elements stored
isEmpty(): returns a Booleanvalue indicating whether noelements are stored
Exceptions
 Attempting the execution of dequeue or front on anempty queue throws anEmptyQueueException
 
Elementary Data Structures12
 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 Structures13
Singly Linked List
 A singly linked list is aconcrete data structureconsisting of a sequenceof nodesEach node stores
element
link to the next nodenextelemnode ABCD
Elementary Data Structures14
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 theQueue ADT takes
O
(1)
time
 f 
nodeselements
Elementary Data Structures15
List ADT
(§5.2.2)
TheList ADT models asequence of 
positions
storing arbitrary objectsIt allows for insertionand removal in the “middle” Query methods:
isFirst(p),isLast(p)
 Accessor methods:
first(),last()
before(p),after(p)
Update methods:
replaceElement(p, o),swapElements(p, q)
insertBefore(p, o),insertAfter(p, o),
insertFirst(o),insertLast(o)
remove(p)
Elementary Data Structures16
Doubly Linked List
 A doubly linked list provides a naturalimplementation of the List ADTNodes implement Position and store:
element
link to the previous node
link to the next node
Special trailer and header nodesprevnextelemtrailerheadernodes/positionselementsnode
Elementary Data Structures17
Trees
(§6.1)
In computer science, atree is an abstract modelof a hierarchicalstructure A tree consists of nodeswith a parent-childrelation Applications:
Organization charts
File systems
Programmingenvironments
Computers”R”UsSalesR&DManufacturingLaptopsDesktopsUSInternationalEuropeAsiaCanada
Elementary Data Structures18
Tree ADT
(§6.1.2)
We use positions to abstractnodesGeneric methods:
integersize()
booleanisEmpty()
objectIteratorelements()
positionIteratorpositions()
 Accessor methods:
positionroot()
positionparent(p)
positionIteratorchildren(p)
Query methods:
booleanisInternal(p)
booleanisExternal(p)
booleanisRoot(p)
Update methods:
swapElements(p, q)
objectreplaceElement(p, o)
 Additional update methodsmay be defined by datastructures implementing theTree ADT

Activity (12)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
syriluit liked this
shivagowdain liked this
Vivek Singh liked this
prakash_vit liked this
dev_panwar liked this
Manchaiah liked this
mar liked this
hevi_web liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->