Download Lecture 5 (linked lists, 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/16/2006 12:24 PM
Singly Linked List
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
D
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;
}
}
/** Creates a node with the given element and next
node. */
public Node(Object e, Node n) {
element = e;
next = n;
}
Linked Lists and Vectors
C
Linked Lists and Vectors
The Node Class for List Nodes
public class Node {
// Instance variables:
private Object element;
private Node next;
© 2004 Goodrich, Tamassia
B
© 2004 Goodrich, Tamassia
3
Inserting at the Head
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
4
The SinglyLinkedList Class
class SinglyLinkedList {
1. Allocate a new
node
2. Insert new element
3. Have new node
point to old head
4. Update head to
point to new node
private Node head, tail;
public SinglyLinkedList() {
} // head and tail set to null
public void insertAtHead(Object newElem) {
Node newHead = new Node(newElem,head);
head = newHead;
if (tail == null) tail = head;
}
… (other methods – good exercise to implement them)
}
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
5
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
6
1
Sequences
10/16/2006 12:24 PM
Removing at the Head
Inserting at the Tail
1. Allocate a new
1. Update head to
node
point to next node
in the list
2. Allow garbage
collector to reclaim
the former first
node
2. Insert new element
3. Have new node
point to null
4. Have old last node
point to new node
5. Update tail to point
to new node
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
7
Removing at the Tail
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
8
Stack 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
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
nodes
∅
t
elements
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
9
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
10
Queue with a Singly Linked List
We can implement a queue with a singly linked list
„
„
Vectors and Array Lists
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
f
∅
elements
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
11
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
12
2
Sequences
10/16/2006 12:24 PM
The Vector ADT
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)
Applications of Vectors
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
© 2004 Goodrich, Tamassia
13
Array-based Vector
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
14
Insertion
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
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]
V
V
0 1 2
r
n
0 1 2
r
n
0 1 2
o
r
V
0 1 2
n
r
V
Linked Lists and Vectors
© 2004 Goodrich, Tamassia
15
Deletion
© 2004 Goodrich, Tamassia
In the array based implementation of a Vector
„
„
„
0 1 2
o
r
n
0 1 2
r
n
0 1 2
r
V
Linked Lists and Vectors
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
V
© 2004 Goodrich, Tamassia
16
Performance
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
n
Linked Lists and Vectors
n
17
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
18
3
Sequences
10/16/2006 12:24 PM
Growable Array-based Vector
Comparison of the Strategies
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
„
19
Incremental Strategy Analysis
Linked Lists and Vectors
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
20
Doubling 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)
© 2004 Goodrich, Tamassia
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
21
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 + …+ 2k =
n + 2k + 1 −1 = 2n −1
8
T(n) is O(n)
The amortized time of a push
operation is O(1)
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
22
java.util.Vector and java.util.ArrayList
Java.util package contains classes which
implement useful data structures:
„
„
ArrayList, LinkedList, Stack, HashMap,.. Are
Collections (implement Collection interface)
Vector, Hashtable: retro-fitted in the Collections
hierarchy, thread-safe
Java API – see
http://java.sun.com/j2se/1.5.0/docs/api/
(there should also be a local copy on the School
machines, but I can’t find it…)
© 2004 Goodrich, Tamassia
Linked Lists and Vectors
23
4