Published by Patrick Mutisya · 14 days ago
An Abstract Data Type (ADT) defines a set of values and a collection of operations that can be performed on those values, without specifying how the data is stored or how the operations are carried out.
Typical ADTs studied at A‑Level include:
Implementing an ADT in terms of another ADT has several benefits:
To implement ADT \$A\$ using ADT \$B\$ we follow these steps:
A stack supports push(x), pop() and top(). If we have a dynamic array‑based list \$L\$, we can implement a stack as follows:
push(x):
L.append(x) // add at the end of the list
pop():
if L.isEmpty() then error
return L.removeLast() // remove the last element
top():
if L.isEmpty() then error
return L.get(L.size()‑1) // read the last element without removing
All stack operations run in \$O(1)\$ amortised time because list append and removeLast are constant‑time for an array‑list.
A queue requires enqueue(x) and dequeue(). Using two stacks \$S1\$ and \$S2\$ we obtain:
enqueue(x):
S1.push(x)
dequeue():
if S2.isEmpty() then
while not S1.isEmpty() do
S2.push(S1.pop())
if S2.isEmpty() then error
return S2.pop()
This implementation guarantees \$O(1)\$ amortised time for both operations.
The binary heap provides insert, deleteMin (or deleteMax) and peek. The priority‑queue ADT can be directly mapped to these heap operations.
| ADT Implemented | Underlying ADT | Operation | Complexity (Worst‑case) | Complexity (Amortised) |
|---|---|---|---|---|
| Stack | Dynamic List | push | \$O(1)\$ | \$O(1)\$ |
| Stack | Dynamic List | pop | \$O(1)\$ | \$O(1)\$ |
| Queue | Two Stacks | enqueue | \$O(1)\$ | \$O(1)\$ |
| Queue | Two Stacks | dequeue | \$O(n)\$ | \$O(1)\$ |
| Priority Queue | Binary Heap (array) | insert | \$O(\log n)\$ | \$O(\log n)\$ |
| Priority Queue | Binary Heap (array) | deleteMin | \$O(\log n)\$ | \$O(\log n)\$ |
For each wrapper algorithm we must show that the sequence of underlying operations preserves the abstract specification of the target ADT.
push adds an element at that position, and pop removes it, matching the LIFO property.peek and deleteMin expose directly.Implementing an ADT using another ADT is a powerful design technique. By carefully mapping operations and analysing the resulting algorithms, we can: