โ† Back to subjects
โญ 0
IB Diploma Computer Science HL ยท Topic 5 โ€” Abstract data structures (HL)
Mini-Lesson

Abstract data structures

This HL-only Topic 5 covers recursion and the classic abstract data structures: stacks and queues, linked lists, and binary (search) trees with their pre-order, in-order and post-order traversals.

Work through each screen, answer the questions as you go (some are wordy, some are calculations) and collect โญ stars. Press Start when you are ready.

Recursion

Thinking recursively

A recursive subprogram calls itself. It needs a base case that stops the recursion and a recursive case that moves toward it. Each call is stored on the call stack.

factorial(n): base case factorial(1) = 1; recursive case n times factorial(n minus 1). So factorial(6) = 6 times 5 times 4 times 3 times 2 times 1 = 720.

Calculate

Recursion result

1Using factorial(n) = n times factorial(n minus 1) with base case factorial(1) = 1, what is factorial(6)?
Hint: 6 times 5 times 4 times 3 times 2 times 1.
Fib

Recursion example: Fibonacci

The Fibonacci sequence (starting 1, 1) adds the two previous terms: 1, 1, 2, 3, 5, 8, 13, 21, ... Each term can be defined recursively as fib(n) = fib(n minus 1) + fib(n minus 2).

Calculate

Fibonacci term

2Using the sequence 1, 1, 2, 3, 5, ... what is the 8th term?
Hint: 1, 1, 2, 3, 5, 8, 13, then the next.
Stack

Stacks (LIFO)

A stack is last in, first out. Operations: push (add on top), pop (remove from top), peek (look at the top). Stacks are used for the call stack, undo history and expression evaluation.

Quick check

Stack order

?You push 3, then 7, then 2 onto an empty stack, then pop once. Which value is removed?
Queue

Queues (FIFO)

A queue is first in, first out. Operations: enqueue (add at the rear), dequeue (remove from the front). Queues are used for print spoolers, buffers and scheduling.

Sort it

Stack, queue or tree?

Tap an item, then the structure it belongs to.

๐Ÿ“š Stack (LIFO)

๐Ÿšถ Queue (FIFO)

๐ŸŒณ Tree / linked list

Linked

Linked lists

A linked list is a chain of nodes; each node stores data and a pointer to the next node. Unlike arrays, they grow and shrink dynamically and allow cheap insertion and deletion, but they have no random access โ€” you must follow the pointers.

Trees

Binary trees and BSTs

A binary tree has nodes with up to two children (left and right). In a binary search tree (BST), every left descendant is smaller than the node and every right descendant is larger, giving fast searching.

Insert 50, 30, 70, 20, 40, 60, 80 into a BST: 50 is the root; 30 and 70 are its children; 20 and 40 sit under 30; 60 and 80 under 70.

Quick check

In-order traversal

?For the BST built from 50, 30, 70, 20, 40, 60, 80, what does an in-order traversal produce?
Quick check

Pre-order traversal

?For the same BST, what does a pre-order traversal produce?
Quick check

Post-order traversal

?For the same BST, what does a post-order traversal produce?
Match it

Match the traversal

Tap a traversal on the left, then what it does.

Traversal
Order of visits
2D arrays

Two-dimensional arrays

A two-dimensional array stores data in rows and columns, like a grid or table. An element is accessed by two indices, for example grid[row][column]. They model images, boards and spreadsheets.

Quick check

2D array

?To read the value in the 3rd row and 2nd column of a 2D array called grid (indices starting at 0), you would use:
Efficiency

Efficiency of the structures

Different structures suit different jobs. Stack and queue push/pop and enqueue/dequeue are O(1). Searching a balanced binary search tree is O(log n). A linked list must be traversed to find an item, which is O(n), but insertion once positioned is cheap.

Recap

The big ideas to know

Recursion: base case + recursive case; uses the call stack

Stack: LIFO โ€” push, pop, peek

Queue: FIFO โ€” enqueue, dequeue

Linked list: nodes with pointers, dynamic, no random access

BST: left is smaller, right is larger; in-order gives sorted order

Traversals: pre (root,L,R), in (L,root,R), post (L,R,root)

You have covered the whole of HL Topic 5 โ€” abstract data structures. Press Finish to see your score.

๐Ÿ†

Mini-lesson complete!

โญโญโญ

You have worked through Abstract data structures. ๐ŸŽ‰

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