Sub topic 3
Data Structure or ADT? Keeping the Terms Apart
Assume you know Python classes, lists, references, exceptions, and basic Big-O notation.
A data structure is a concrete arrangement of data, such as a list, linked chain, or hash table. An abstract data type (ADT) is defined by the operations it offers and the behavior those operations promise. The ADT is the idea; the data structure is one implementation of that idea.
| Concept | Example |
|---|---|
| ADT | A set that supports `add`, `contains`, and `size` |
| Data structure | A list or hash table used to store that set |
How Languages Express ADTs
Languages express an ADT through an interface, abstract class, or module. These mechanisms describe which operations clients may call. They hide representation details and allow several implementations to satisfy the same contract.
An interface usually states method signatures and behavior. An abstract class can also provide shared code. A module can expose functions while keeping its data private. The mechanism differs, but the goal is the same: clients depend on behavior rather than storage.
Choosing an Implementation by Cost
Choose the representation according to expected operations and constraints.
| Implementation | `contains` average cost | Extra concern |
|---|---|---|
| Unsorted list | O(n) | Simple, but slow searches |
| Hash table | O(1) average | Needs hashable values and extra memory |
The client can remain unchanged when the implementation changes.
Representation Invariants
A representation invariant is a condition that must always be true about an implementation’s internal state. For a list-backed set, the invariant might be “the internal list contains no duplicates.” Check it after operations that could violate it.
The Abstraction Function
An abstraction function explains how concrete state represents an abstract value. If _items is [2, 5], the abstraction function for a list-backed set maps it to the mathematical set {2, 5}.
Representation Exposure and How to Prevent It
Representation exposure occurs when callers can directly modify internal storage. Return a copy or an immutable view instead of the internal list.
class ListSet:
def __init__(self):
self._items = []
self._check_rep()
def _check_rep(self):
assert len(self._items) == len(set(self._items))
def add(self, value):
if value not in self._items:
self._items.append(value)
self._check_rep()
def contains(self, value):
return value in self._items
def values(self):
return list(self._items) # protects the representationTesting Every Implementation Against One Specification
Write tests against the ADT’s promised behavior, then run the same tests for every implementation. This checks substitutability without testing private fields.
def check_set(make_set):
s = make_set()
assert s.size() == 0
s.add(4)
s.add(4)
assert s.contains(4)
assert s.size() == 1
check_set(ListSet)Exercise
Why can a hash-table-backed set replace a list-backed set without changing client code?
Show answer
Both implement the same operations and behavioral contract, so clients depend on the ADT rather than its representation.