Signed up recently but had issues with the confirmation email?

Click Forgot password and follow the reset steps. You can choose the same password you used when you signed up. This should then provide access.

Remember to check Spam and Junk. The email will be from auth@progressbytes.co.uk.

Revision notesQuestionsFor schools
Revise Computing
Log inStart revising free
Revision notesQuestionsFor schoolsLog in

© 2026 Revise Computing · revisecomputing.co.uk

Worried about something?Question Bank UpdatesPrivacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).

Knowledge/A Level/H446/02 Algorithms and programming/Algorithms/Algorithms for stacks, queues, trees…

Algorithms for stacks, queues, trees, linked lists, depth-first (post-order) and breadth-first traversal

Algorithms for the main data structures, (stacks, queues, trees, linked lists, depth-first (post-order) and breadth-first traversal of trees).

2.3.1 Algorithms

Practise exam questions

Algorithms — Note 5 of 6

Candidates need to understand where stacks, queues, trees and linked lists can be used and justify their use in a scenario. Candidates need to be able to add and remove items (and search a linked list), and read, trace and write code to implement each structure, including using another structure such as a static array. Candidates need to be able to perform, and read, trace and write code for, depth-first (post-order) and breadth-first traversals of a tree.

What you need to know

You need to be able to read, trace and write code for the main data structures, and to perform depth-first (post-order) and breadth-first traversals of a tree. You also need to know where each structure is used.

What to know

  • Stack: push and pop at the top, checking for overflow and underflow. Uses: undo, call stack, backtracking, reversing data.
  • Queue: enqueue at the rear, dequeue from the front, checking for full and empty. Uses: print queues, task scheduling, breadth-first search.
  • Linked list: add, remove and search for items by following and updating pointers.
  • Tree: add and remove items; trees can be binary or multi-branch, and can be implemented using a linked list or arrays.
  • Backtracking, in the tree above: after C, depth-first returns to B (both its children are now done, so B is output), then to D, then goes down to F. Each time, it goes back up to the nearest node that still has an unvisited child.
  • Post-order traversal is used, for example, to delete a tree (children before parents) or to evaluate an expression tree.
  • Comparing the two: breadth-first reaches nodes close to the root first, so it suits finding the fewest steps from the start; depth-first reaches deep nodes sooner and is naturally recursive.

Code examples

Depth-first (post-order) and breadth-first traversal
python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

def post_order(node):
    if node is not None:
        post_order(node.left)     # left subtree
        post_order(node.right)    # right subtree
        print(node.data)          # then the node itself

def breadth_first(root):
    queue = [root]
    while len(queue) > 0:
        node = queue.pop(0)       # dequeue from the front
        print(node.data)
        if node.left is not None:
            queue.append(node.left)
        if node.right is not None:
            queue.append(node.right)
Traversal orders for an example tree
text
        D
      /   \
     B     F
    / \   / \
   A   C E   G

Post-order (left, right, node):  A C B E G F D
Breadth-first (level by level):   D B F A C E G

Key terms

Depth-first (post-order) traversal

Visit the left subtree, then the right subtree, then the node itself. Implemented recursively or with a stack.

Breadth-first traversal

Visit the root, then every node on the next level, left to right, level by level. Implemented with a queue.

Exam technique

  • Mistake:Thinking pop physically deletes the item, or just writing that it 'removes' it. Only the top POINTER moves (down by one); the value stays in the array until a later push overwrites it. A full description also checks whether the stack is empty first and returns the top item.
  • Tip:When performing a traversal by hand, write the order in which nodes are output, step by step.
  • Tip:To explain how a stack gives depth-first traversal its backtracking, say WHEN nodes are pushed and popped. A node is pushed when it is first reached; the stack is popped on reaching a leaf, or a node whose children are all visited, to go back to its parent. Use the given tree's names: in the tree above, push D, B, A; A is a leaf, so pop it to get back to B, then push C.
  • Mistake:Confusing post-order (left, right, node) with pre-order (node, left, right) or in-order (left, node, right). Also, don't list nodes in the order you first reach them: post-order outputs a node only after all its descendants, so the root comes last.

Past exam questions

  • June 2026Paper 2Q1(a)(ii)3marks
  • June 2026Paper 2Q1(b)(i)5marks
  • June 2026Paper 2Q5(c)(i)2marks
  • June 2026Paper 2Q5(c)(ii)4marks
  • June 2025Paper 2Q3(b)4marks
  • June 2025Paper 2Q3(c)6marks
  • June 2025Paper 2Q3(d)5marks
  • June 2025Paper 2Q7(c)4marks
  • June 2025Paper 2Q8(c)(ii)5marks
  • June 2025Paper 2Q8(e)5marks
  • June 2024Paper 2Q5(a)4marks
  • June 2024Paper 2Q5(b)4marks
  • June 2024Paper 2Q7(b)(iii)5marks
  • June 2023Paper 2Q1(a)(iii)2marks
  • June 2023Paper 2Q1(a)(iv)4marks
  • June 2023Paper 2Q1(a)(v)4marks
  • June 2023Paper 2Q1(a)(vi)2marks
  • June 2023Paper 2Q3(d)4marks
  • June 2023Paper 2Q3(e)5marks
  • June 2022Paper 2Q2(d)9marks
  • June 2022Paper 2Q8(b)(i)5marks
  • June 2022Paper 2Q8(b)(ii)6marks
  • November 2021Paper 1Q7(a)(ii)4marks
  • November 2021Paper 1Q7(b)(ii)3marks
  • November 2021Paper 2Q1(d)6marks
  • November 2021Paper 2Q7(b)5marks
  • November 2020Paper 2Q1(d)4marks
  • November 2020Paper 2Q1(e)(i)3marks
  • November 2020Paper 2Q1(e)(ii)3marks
  • November 2020Paper 2Q5(c)(i)5marks
  • November 2020Paper 2Q5(c)(ii)6marks
  • November 2020Paper 2Q5(c)(iii)8marks
  • November 2020Paper 2Q5(d)3marks
  • November 2020Paper 2Q6(c)6marks
  • June 2019Paper 2Q1(b)(ii)3marks
  • June 2019Paper 2Q2(a)(ii)3marks
  • June 2019Paper 2Q2(a)(iii)4marks
  • June 2019Paper 2Q2(b)(iii)4marks
  • June 2019Paper 2Q2(b)(iv)5marks
  • June 2018Paper 2Q1(b)3marks
  • June 2018Paper 2Q1(c)(i)5marks
  • June 2018Paper 2Q1(c)(ii)2marks
  • June 2018Paper 2Q5(b)(i)4marks
  • June 2018Paper 2Q5(c)3marks
  • June 2018Paper 2Q6(b)(iv)6marks
  • June 2017Paper 1Q2(a)(ii)4marks
  • June 2017Paper 2Q2(c)(ii)3marks
  • June 2017Paper 2Q2(c)(iii)2marks
  • June 2017Paper 2Q2(c)(iv)4marks
  • June 2017Paper 2Q3(c)5marks
  • June 2017Paper 2Q3(d)5marks
  • June 2017Paper 2Q4(b)(i)4marks
  • June 2017Paper 2Q4(b)(ii)6marks

Ready to test yourself?

Practise 2.3.1 questions
← Previous noteComparison of the complexity of algorithms2.3.1 AlgorithmsNext note →Standard algorithms: sorts, Dijkstra's shortest path, A*, binary search and linear search2.3.1 Algorithms

Back to Algorithms

Algorithms

Note 5 of 6

  1. 1Analysis and design of algorithms for a given situation
  2. 2The suitability of different algorithms for a given task and data set, in terms of execution time and space
  3. 3Measures and methods to determine efficiency, Big O notation
  4. 4Comparison of the complexity of algorithms
  5. 5Algorithms for stacks, queues, trees, linked lists, depth-first (post-order) and breadth-first traversal
  6. 6Standard algorithms: sorts, Dijkstra's shortest path, A*, binary search and linear search
Topic overview