โ† Back to subjects
โญ 0
AQA A-level Computer Science (7517) ยท Fundamentals of data structures
Mini-Lesson

Fundamentals of data structures

This mini-lesson covers AQA 4.2 โ€” Fundamentals of data structures. Choosing the right structure is what separates a program that runs in milliseconds from one that runs for a week.

  • Arrays, records and files
  • Stacks (LIFO) and queues โ€” linear, circular and priority
  • Graphs โ€” adjacency matrix vs adjacency list
  • Trees โ€” binary search trees and pre/in/post-order traversal
  • Hash tables, collisions and dictionaries
  • Vectors โ€” addition, scalar multiplication and the dot product

Work through each screen, answer the questions as you go (some are recall, some are calculations you must actually work out) and collect โญ stars. Press Start when you're ready.

4.2 ยท Foundations

Arrays, records and abstract data types

An array is a fixed-size, ordered set of elements all of the same type, accessed by index. Because every element is the same size, the address of element i is found by arithmetic โ€” so access is O(1), instantly, at any index.

address(i) = base + i ร— elementSize

A record groups fields of different types under one name (a row in a file or database table). A file is a collection of records held in secondary storage.

An abstract data type (ADT) is defined by its operations, not its implementation. A stack is an ADT: it promises push, pop and peek. Whether it is built on an array or a linked list is an implementation detail the user never needs to know โ€” that is abstraction at work.

Static vs dynamic: a static structure has its memory fixed at compile time (a plain array) โ€” fast, but it can overflow or waste space. A dynamic structure grows and shrinks at run time using the heap โ€” flexible, but with pointer overhead and a risk of running out of heap memory.

4.2 ยท Stacks

Stacks โ€” last in, first out

A stack is LIFO: the last item pushed is the first popped. It has a single stack pointer holding the index of the top element.

PUSH(x): IF top = maxSize-1 THEN OVERFLOW top โ† top + 1 ; stack[top] โ† x POP(): IF top = -1 THEN UNDERFLOW x โ† stack[top] ; top โ† top - 1 ; RETURN x PEEK(): RETURN stack[top] // look, do not remove

Overflow = pushing onto a full stack. Underflow = popping an empty one. Both must be tested for.

Stacks are everywhere:

  • the call stack โ€” return addresses, parameters and locals for each subroutine call (this is why deep recursion causes a stack overflow);
  • undo in an editor;
  • evaluating reverse Polish notation;
  • backtracking in depth-first search.
4.2 ยท Queues

Queues โ€” including the circular queue

A queue is FIFO: first in, first out. It needs two pointers โ€” front and rear.

A linear queue has a fatal flaw. Every dequeue advances front, so the used space at the low end is never reclaimed: eventually rear hits the end of the array while the front of the array sits empty.

The circular queue fixes this by wrapping the pointers round with MOD:

rear โ† (rear + 1) MOD maxSizefront โ† (front + 1) MOD maxSize

A separate size counter (or one deliberately wasted slot) distinguishes "full" from "empty", since in both cases front and rear can coincide.

Priority queue: items are dequeued in order of priority, not arrival. Equal priorities are served in arrival order. Used by operating-system schedulers and by Dijkstra's algorithm.

Calculate

Your turn โ€” circular queue

1A circular queue is held in an array of size 8 (indices 0โ€“7). The rear pointer holds the index of the last item and is currently 6. Three more items are enqueued. What is the new value of rear?
Hint: Each enqueue does rear โ† (rear + 1) MOD 8. Do it three times, or compute (6 + 3) MOD 8 in one go.
Quick check

Stack or queue?

?Which structure does a processor use to store the return address, parameters and local variables of each active subroutine call?
4.2 ยท Graphs

Graphs

A graph is a set of vertices (nodes) joined by edges (arcs). Edges may be directed (one-way) or undirected, and weighted (carrying a cost, distance or time) or unweighted.

Two standard representations:

Adjacency matrixAdjacency list
An n ร— n table; cell [i][j] holds the weight of the edge iโ†’j (or 0).For each vertex, a list of the vertices it connects to.
Space O(nยฒ) regardless of the number of edges.Space O(n + e) โ€” only real edges are stored.
Testing "is i joined to j?" is O(1).Testing that edge needs a scan of one list.
Best for dense graphs; convenient to program.Best for sparse graphs โ€” e.g. a road network.

Sparse means: the number of edges is far smaller than the maximum n(nโˆ’1)/2. A road network with 10 000 junctions has perhaps 25 000 roads, not 50 million โ€” an adjacency matrix would be over 99.9% zeros.

Quick check

Choosing a graph representation

?A satnav stores a road network of 50 000 junctions in which the average junction connects to just 3 others. Which representation should it use, and why?
4.2 ยท Trees

Trees and binary search trees

A tree is a connected, undirected graph with no cycles. A rooted tree adds a designated root; every other node has exactly one parent. Nodes with no children are leaves.

In a binary search tree (BST), every node has at most two children and obeys one invariant:

left subtree < node < right subtree

That single rule makes searching a halving process. Searching, inserting and deleting are O(log n) in a balanced tree โ€” but if you insert already-sorted data the tree degenerates into a linked list and collapses to O(n).

ABC DEFG
Root A; children B and C; leaves D E F G.

Traversals โ€” the name tells you when the root is visited:

  • Pre-order: root, left, right โ†’ A B D E C F G. (Used to copy a tree, or to output prefix expressions.)
  • In-order: left, root, right โ†’ D B E A F C G. (On a BST this outputs the data in sorted order.)
  • Post-order: left, right, root โ†’ D E B F G C A. (Used to delete a tree, and to produce reverse Polish.)
Calculate

Your turn โ€” post-order traversal

2For the tree above (root A; A's children B and C; B's children D and E; C's children F and G), write the post-order traversal. Type the seven letters in order, e.g. ABCDEFG.
Hint: Post-order = left subtree, right subtree, then the root โ€” the root always comes last.
Quick check

Binary search trees

?Which traversal of a binary search tree outputs the stored values in ascending sorted order?
4.2 ยท Hashing

Hash tables and dictionaries

A hash table converts a key straight into an array index using a hashing algorithm:

index = h(key)e.g. h(k) = k MOD tableSize

No searching is needed โ€” the key computes its own address, so lookup, insert and delete are O(1) on average. That is the whole point.

A collision happens when two different keys hash to the same index. Two standard fixes:

  • Open addressing / linear probing โ€” walk forward to the next free slot. Simple, but causes clustering, which degrades performance.
  • Chaining โ€” each slot holds a list of all items that hashed there.

As the load factor (items รท slots) rises, collisions rise. Above roughly 0.7 the table is usually rehashed into a bigger array โ€” every key is recomputed against the new size.

Dictionary: an ADT of <key, value> pairs, almost always implemented as a hash table. A good hash function is fast, deterministic and distributes keys uniformly. Table sizes are usually prime to spread the MOD results out.

Calculate

Your turn โ€” hashing with linear probing

3A hash table has 11 slots (0โ€“10) and uses h(k) = k MOD 11 with linear probing. The keys 23, then 45, then 12 are inserted in that order into an empty table. At which index does 12 end up?
Hint: Work out 23 MOD 11, then 45 MOD 11, then 12 MOD 11. All three want the same home slot โ€” probe forward one slot at a time.
4.2 ยท Vectors

Vectors

AQA treats a vector in three equivalent ways: as a list of numbers [2, 3, โˆ’1], as a function from an index set to the reals, and as an arrow in n-dimensional space.

Vector addition and scalar multiplication work component by component:

[2, 3, -1] + [4, 0, 5] = [6, 3, 4] 3 * [2, 3, -1] = [6, 9, -3]

The dot product multiplies matching components and sums the results, giving a single number (scalar):

a ยท b = aโ‚bโ‚ + aโ‚‚bโ‚‚ + โ€ฆ + aโ‚™bโ‚™

A convex combination of a and b is ฮฑa + ฮฒb where ฮฑ, ฮฒ โ‰ฅ 0 and ฮฑ + ฮฒ = 1. Geometrically it lands somewhere on the straight line between the two vectors.

Why a computer scientist cares: the dot product measures similarity. Machine-learning models, search engines and recommender systems compare documents or images by turning them into vectors and taking dot products.

Calculate

Your turn โ€” the dot product

4Compute the dot product [2, 3, โˆ’1] ยท [4, 0, 5].
Hint: Multiply matching components then add: (2 ร— 4) + (3 ร— 0) + (โˆ’1 ร— 5).
Quick check

Hash tables

?A hash table's load factor rises to 0.95. What is the most likely consequence?
Sort it

Which structure fits the job?

Tap a use case, then tap the structure it needs.

๐Ÿ“š Stack (LIFO)

๐Ÿšถ Queue (FIFO)

๐ŸŒ Non-linear structure

Match it

Match the structure to its defining feature

Tap an item on the left, then its partner on the right.

Defining feature
Structure
Recap

The big ideas to know

Arrays & records: array = same type, indexed, O(1) access; record = mixed-type fields

Stack: LIFO ยท push/pop/peek ยท overflow & underflow ยท call stack, undo, RPN, DFS

Queue: FIFO ยท front & rear ยท circular queue wraps with MOD ยท priority queue serves by priority

Graph: adjacency matrix O(nยฒ) for dense; adjacency list O(n + e) for sparse

Tree / BST: left < node < right ยท O(log n) balanced, O(n) degenerate ยท pre / in (sorted) / post-order

Hash table: index = h(key) ยท collisions โ†’ probing or chaining ยท rehash above ~0.7 load factor

Vector: component-wise add and scale ยท dot product gives a scalar ยท convex combination lies between

That is the whole of AQA 4.2. Press Finish to see your score.

๐Ÿ†

Mini-lesson complete!

โญโญโญ

You've worked through Fundamentals of data structures for AQA A-level Computer Science (7517). ๐ŸŽ‰

Your stars: 0 / 0

Next: test yourself in the Evaluate stage Confidence Quiz, then lock it in with Verify.

๐Ÿ“ฃ Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

โ†’ Back to all subjects