22c:31 Algorithms: Ch3: Data Structures
22c:31 Algorithms: Ch3: Data Structures
Collections
A collection is an object that serves as a repository for other
objects
A collection usually provides services such as adding,
removing, and otherwise managing the elements it
contains
Abstraction
Our data structures should be abstractions, so
they can be reused to save cost
That is, they should hide unneeded details
We want to separate the interface of the
structure from its underlying implementation
This helps manage complexity and makes it
possible to change the implementation without
changing the interface
4
Collection Classes
The Java standard library contains several classes that
represent collections, often referred to as the Java
Collections API
Their underlying implementation is implied in the class
names such as ArrayList and LinkedList
ArrayList is an implementation of List based on arrays.
LinedList is an implementation of List based on nodes.
Generics Classes
The Java Collection classes are implemented as
generic types
We give the type of object a collection will
hold when we create the collection, like this:
ArrayList<String> myStringList =
new ArrayList<String>();
List<Book> myBookList =
new ArrayList<Book>();
If no type is given, the collection is defined as containing references to
the Object class
6
10
Arrays
An array is a structure of fixed-size data records such that
each element can be efficiently located by its index or
(equivalently) address.
Advantages of contiguously-allocated arrays include:
Constant-time access given the index.
Dynamic Arrays
Unfortunately we cannot adjust the size of simple arrays in
the middle of a programs execution.
Compensating by allocating extremely large arrays can waste
a lot of space.
With dynamic arrays we start with an array of size 1, and
double its size from m to 2m each time we run out of space.
How many times will we double for n elements? Only
dlog 2 ne.
lg
Xn
i=1
i n/2 = n
lgXn
i=1
i/2 n
X
i=1
i/2i = 2n
Object References
Recall that an object reference is a variable
that stores the address of an object
A reference also can be called a pointer
References often are depicted graphically:
student
John Smith
40725
3.58
12
References as Links
Object references can be used to create links
between objects
Suppose a Student class contains a
reference to another Student object
John Smith
40725
3.57
13
Jane Jones
58821
3.72
References as Links
References can be used to create a variety of
linked structures, such as a linked list:
studentList
14
Intermediate Nodes
The objects being stored should not be
concerned with the details of the data structure
in which they may be stored
For example, the Student class should not
have to store a link to the next Student object
in the list
Instead, we can use a separate node class with
two parts: 1) a reference to an independent
object and 2) a link to the next node in the list
The internal representation becomes a linked
list of nodes
15
Inserting a Node
A method called insert could be defined to add a
node anywhere in the list, to keep it sorted, for example
Inserting at the front of a linked list is a special case
16
Inserting a Node
When inserting a node in the middle of a linked
list, we must first find the spot to insert it
Let current refer to the node before the spot
where the new node will be inserted
17
Deleting a Node
A method called delete could be defined to
remove a node from the list
Again the front of the list is a special case:
18
19
20
count: 4
front
rear
Queues
A queue is similar to a list but adds items only
to the rear of the list and removes them only
from the front
It is called a FIFO data structure: First-In, Firstdequeue
Out enqueue
Analogy: a line of people at a bank tellers
window
22
Queues
We can define the operations for a queue
enqueue - add an item to the rear of the queue
dequeue (or serve) - remove an item from the front of the
queue
isEmpty - returns true if the queue is empty
Stacks
A stack ADT is also linear, like a list or a queue
Items are added and removed from only one
end of a stack
It is therefore LIFO: Last-In, First-Out
Analogies: a stack of plates in a cupboard, a
stack of bills to be paid, or a stack of hay bales
in a barn
24
Stacks
Stacks often are drawn vertically:
push
25
pop
Stacks
Some stack operations:
Stacks
The Stack class is part of the Java Collections
API and thus is a generic class
Stack<String> strStack = new Stack<String>();
27
Solution Blank
singly
singly doubly doubly
unsorted sorted unsorted sorted
Search(L, k)
Insert(L, x)
Delete(L, x)
Successor(L, x)
Predecessor(L, x)
Minimum(L)
Maximum(L)
Solution
singly
Dictionary operation unsorted
Search(L, k)
O(n)
Insert(L, x)
O(1)
Delete(L, x)
O(n)
Successor(L, x)
O(n)
Predecessor(L, x)
O(n)
Minimum(L)
O(n)
Maximum(L)
O(n)
double
unsorted
O(n)
O(1)
O(1)
O(n)
O(n)
O(n)
O(n)
singly
sorted
O(n)
O(n)
O(n)
O(1)
O(n)
O(1)
O(1)
doubly
sorted
O(n)
O(n)
O(1)
O(1)
O(1)
O(1)
O(1)