Download Lecture 5 (linked lists and vectors)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Java ConcurrentMap wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

Array data structure wikipedia , lookup

Binary search tree wikipedia , lookup

B-tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Sequences
10/14/2004 4:33 PM
Singly Linked List (§ 4.4.1)
A singly linked list is a
concrete data structure
consisting of a sequence
of nodes
Each node stores
Linked Lists
„
„
next
element
link to the next node
node
elem
∅
A
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
1
The Node Class for List Nodes
Linked Lists and Vectors
3
Inserting at the Head
Linked Lists and Vectors
2
// Accessor methods:
public Object getElement() {
return element;
}
public Node getNext() {
return next;
}
// Modifier methods:
public void setElement(Object newElem) {
element = newElem;
}
public void setNext(Node newNext) {
next = newNext;
}
}
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
4
1. Update head to
node
2. Insert new element
3. Have new node
point to old head
4. Update head to
point to new node
Linked Lists and Vectors
D
Removing at the Head
1. Allocate a new
© 2004 Goodrich, Tamassia
C
The Node Class for List Nodes
public class Node {
// Instance variables:
private Object element;
private Node next;
/** Creates a node with null references to its element and next
node. */
public Node()
{
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(Object e, Node n) {
element = e;
next = n;
}
© 2004 Goodrich, Tamassia
© 2004 Goodrich, Tamassia
B
point to next node
in the list
2. Allow garbage
collector to reclaim
the former first
node
5
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
6
1
Sequences
10/14/2004 4:33 PM
Inserting at the Tail
Removing at the Tail
1. Allocate a new
2.
3.
4.
5.
Removing at the tail
of a singly linked list
is not efficient!
There is no
constant-time way
to update the tail to
point to the previous
node
node
Insert new element
Have new node
point to null
Have old last node
point to new node
Update tail to point
to new node
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
7
Stack with a Singly Linked List
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
Queue with a Singly Linked List
We can implement a stack with a singly linked list
The top element is stored at the first node of the list
The space used is O(n) and each operation of the
Stack ADT takes O(1) time
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
nodes
∅
t
f
∅
elements
© 2004 Goodrich, Tamassia
8
Linked Lists and Vectors
elements
9
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
10
The Vector ADT (§5.1)
The Vector ADT
extends the notion of
array by storing a
sequence of arbitrary
objects
An element can be
accessed, inserted or
removed by specifying
its rank (number of
elements preceding it)
An exception is
thrown if an incorrect
rank is specified (e.g.,
a negative rank)
Vectors and Array Lists
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
11
© 2004 Goodrich, Tamassia
Main vector operations:
„ object elemAtRank(integer r):
returns the element at rank r
without removing it
„ object replaceAtRank(integer r,
object o): replace the element at
rank with o and return the old
element
„ insertAtRank(integer r, object o):
insert a new element o to have
rank r
„ object removeAtRank(integer r):
removes and returns the element
at rank r
Additional operations size() and
isEmpty()
Linked Lists and Vectors
12
2
Sequences
10/14/2004 4:33 PM
Applications of Vectors
Array-based Vector
Use an array V of size N
A variable n keeps track of the size of the vector
(number of elements stored)
Operation elemAtRank(r) is implemented in O(1)
time by returning V[r]
Direct applications
„
Sorted collection of objects (elementary
database)
Indirect applications
„
„
Auxiliary data structure for algorithms
Component of other data structures
Linked Lists and Vectors
© 2004 Goodrich, Tamassia
V
0 1 2
13
Insertion
V
0 1 2
r
n
V
0 1 2
r
0 1 2
o
r
n
n
0 1 2
r
n
0 1 2
n
Linked Lists and Vectors
15
The space used by the data structure is O(n)
size, isEmpty, elemAtRank and replaceAtRank run in
O(1) time
insertAtRank and removeAtRank run in O(n) time
If we use the array in a circular fashion,
insertAtRank(0) and removeAtRank(0) run in
O(1) time
In an insertAtRank operation, when the array
is full, instead of throwing an exception, we
can replace the array with a larger one
Linked Lists and Vectors
© 2004 Goodrich, Tamassia
r
n
Linked Lists and Vectors
16
Growable Array-based Vector
In the array based implementation of a Vector
© 2004 Goodrich, Tamassia
o
r
V
Performance
„
0 1 2
V
V
„
14
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
„
Linked Lists and Vectors
© 2004 Goodrich, Tamassia
Deletion
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
© 2004 Goodrich, Tamassia
n
r
In a push (insertAtRank(t)) Algorithm push(o)
operation, when the array
if t = S.length − 1 then
is full, instead of throwing
A ← new array of
an exception, we can
size …
replace the array with a
for i ← 0 to t do
larger one
A[i] ← S[i]
S←A
How large should the new
t←t+1
array be?
S[t] ← o
incremental strategy:
increase the size by a
constant c
„ doubling strategy: double
the
size
Linked Lists and Vectors
© 2004 Goodrich,
Tamassia
„
17
18
3
Sequences
10/14/2004 4:33 PM
Comparison of the Strategies
Incremental Strategy Analysis
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)
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
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
19
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
geometric series
20
Java.util package contains classes which
implement useful data structures:
„
„
2
4
1
Linked Lists and Vectors
java.util.Vector and java.util.ArrayList
Doubling Strategy Analysis
We replace the array k = log2 n
times
The total time T(n) of a series
of n push operations is
proportional to
n + 1 + 2 + 4 + 8 + …+ 2k =
n + 2k + 1 −1 = 2n −1
T(n) is O(n)
The amortized time of a push
operation is O(1)
© 2004 Goodrich, Tamassia
1
ArrayList, LinkedList, Stack, HashMap,..
Vector, Hashtable: retro-fitted in the Collections
hierarchy, thread-safe
Java API – see for example
http://www.cs.nott.ac.uk/TSG/manuals/java/JDK14/api/
Or see http://java.sun.com
8
21
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
22
4