Lecture 02
Lecture 02
Circular Student
QUEST Academic
Array
Academic Record
Server
Any of the Abstract Data Types we will discuss can be implemented as either a simple container
or an associative container
We will focus on simple containers in this course
• Any container we discuss can be modified to store key-record pairs
Unique or Duplicate Objects
Another design requirement may be to either:
• Require that all objects in the container are unique, or
• Allow duplicate objects
2. Hierarchical orderings
3. Partial orderings
4. Equivalence relations
5. Weak orderings
1. Linear Orderings
A linear ordering is any relationship where any two objects x
and y that can be compared, exactly one of:
x < y , x = y, or y < x
is true and where the relation is transitive
• Such a relation is therefore anti-symmetric
• Any collection can therefore be sorted according to this relation
Examples of sets which are linearly ordered include:
• Integers 1, 2, 3, 4, 5, 6, ...
• Real numbers 1.2, 1.2001, 1.24, 1.35, 2.78, ...
• The alphabet a, b, c, d, e, f, ..., x, y, z
• Memory 0x00000000, 0x00000001, ..., 0xFFFFFFFF
1 2 3
4 5 6
Indexed Allocation 2/2
Matrices can be implemented using indexed allocation
• Most implementations of matrices (or higher-dimensional arrays) use indices
pointing into a single contiguous block of memory
Array C × × × × × ×
D × × ×
• Suppose we allow arbitrary relations between
E × ×
F × ×
any two objects in a container G × × ×
• We could represent this using a two-dimensional
H array
× × ×
• In this case, the matrix is symmetric I × ×
J × × ×
K × × ×
L × × ×
3. Array of Linked List
• Suppose we allow arbitrary relations between any two objects in
a container
• Alternatively, we could use a hybrid: an array of linked lists
• Other hybrids are linked lists of arrays
• Something like this is used for the C++ STL deque container
A
B
C
• For example, the alphabet could be stored either
D as:
• An array of 26 entries, or E
F
• A linked list of arrays of 8 entries G
H
I
J
K
L
4. Hybrid data structures
• Unix inode was used to store information about large files
• The first twelve entries can reference the first twelve blocks (48 KiB)
• The next entry is a pointer to an array that stores the next 1024 blocks
• The next entry has two levels of indirection for files up to 4 GiB
• The last entry has three levels of indirection for files up to 4 TiB
Arbitrary
Front/1st Location Back/nth
Find Good
? Okay
? Good
?
Insert Bad
? Bad
? Good* ? Bad
Erase
Erase Bad
? Bad
? Good
?
Operations on Lists
If the array is not sorted, only one operations changes:
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Bad Bad Good* Bad
Erase Bad Bad Good
*
only if the array is not full
Operations on Lists
However, for a singly linked list where we a head and tail pointer, we
have:
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Good Bad Good
Erase Good Bad Bad
Operations on Lists
If we have a pointer to the kth entry, we can insert or erase at that location quite easily
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Good Good Good
Erase Good Good Bad
• Note, this requires a little bit of trickery: we must modify the value stored in the kth node
• This is a common co-op interview question!
Operations on Lists
For a doubly linked list, one operation becomes more efficient:
Arbitrary
Front/1st Location Back/nth
Find Good Bad Good
Insert Good Good Good
Erase Good Good Good
Lecture Recap
Today we covered following:
• The storage of objects in containers
• We will focus on linear orderings:
• Implicitly defined linear orderings (sorted lists)
• Explicitly defined linear orderings
• We will summarize this information
• We will also look briefly at:
• Hierarchical orderings
• Partial orderings
• Equivalence relations
• Adjacency relations
• The concrete data structures that can be used to store information
• The basic forms of memory allocation
• Contiguous
• Linked
• Indexed
• The prototypical examples of these: arrays and linked lists
• Other data structures:
• Trees
• Hybrids
• Higher-dimensional arrays
• Finally, we will discuss the run-time of queries and operations on arrays and linked lists
End of Lecture
02