winter 2016 comp-250: introduction to computer...
TRANSCRIPT
Winter 2016COMP-250: Introduction
to Computer ScienceLecture 6, January 28, 2016
Java Generics
element next
|_,•|
Java Generics
Java Generics
(Doubly) Linked List
(Doubly) Linked List Node
element next prev
|9,•,•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
⊠
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
⊠
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|3
⊠ ⊠
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|•,•,⊠| |•,•,•| |•,•,•| |•,⊠,•|
head tail size
|•,•,4|
node
|•|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
dummy dummy . Head Tail size
|•,•,2|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
⊠
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
⊠
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
head tail size
|•,•,2|
node
|•|1
⊠ ⊠
Array vs Linked List
Linked Lists as ADT(Abstract Data Type)
Linked List operations
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
dummy dummy . Head Tail size
|•,•,2|
Java LinkedList
• implemented as doubly linked list(with dummies)
• Node class is private
Java LinkedList
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
|•,•,2|
Java LinkedList
}expensive
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
|•,•,2|
Java LinkedList
Time(n) is 𝜴(n2)
|⊠,•,⊠| |•,•,•| |•,•,•| |⊠,⊠,•|
|•,•,2|
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• when new space is needed - doubles the array size.
|•,•,•,•,•,•,•,•|
element
|•|
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• smaller array is copied into bigger array.
|•,•,•,•,⊠,⊠,⊠,⊠|
element
|•|
array size cap.
|•,4,8|
Java ArrayList
• new element added to new array
|•,•,•,•,⊠,⊠,⊠,⊠|
element
|•|
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
Cost ?
|•,•,•,•|
array size cap.
|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
|•,•,•,•|
array size cap.
|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationExtra cost of 2k+1 new steps only if 2k stepsalready spent before!
|•,•,•,•|
array size cap.
|•,4,4| |•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationExtra cost of 2k+1 new steps only if 2k stepsalready spent before!
Cost is amortized constant time!
|•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
|•,•,•,•| |•,•| |•|20
21
22
23
|•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
To build an arrayof 2k elements
you need 2k+1-1 steps!
|•,•,•,•| |•,•| |•|20
21
22
23
|•,•,•,•,⊠,⊠,⊠,⊠|
Java ArrayList
To build an arrayof 2k elements
you need 2k+1-1 steps!
Cost is amortized constant time!
|•,•,•,•| |•,•| |•|20
21
22
23
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
Cost is constant!
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
|•,•,•,•|
array size cap.
|•,4,4|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationget(2)
|•|
Cost is constant!
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notation
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻⤻
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻⤻⤻
|•,•,•,•,•,⊠,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
⤻⤻⤻⤻
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•|
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| 5
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| Cost is linear!5
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| Cost is linear!
Same for Remove!5
|•,•,•,•,•,•,⊠,⊠|
array size cap.
|•,4,8|
Java ArrayList
• implementation using arrays of growing sizes
• cannot access using a[i] notationelement
|•| Cost is linear!
Same for Remove!5
LinkedList vs ArrayList
Winter 2016COMP-250: Introduction
to Computer ScienceLecture 6, January 28, 2016