Who this book is for
Students moving beyond GCSE programming into recursive algorithms, trees and breadth-first search.
Before you begin
Write functions with return values, trace loops, use lists and dictionaries, and work with row and column indices. Start after Python GCSE Advanced or equivalent practice.
An independent programming resource, not a complete A level Computer Science specification. The further-study section extends the main chapters with additional tree tasks, Dijkstra's algorithm and disjoint sets.
Inside the book
The 14 chapters
- Understand and represent trees
- Trace recursive calls and returns
- Traverse trees and evaluate expressions
- Calculate results from subtrees
- Follow paths with recursion
- Search and update a binary search tree
- Explore graphs with a queue
- Find and reconstruct shortest paths
- Solve mazes and move across grids
- Find regions and use several starting points
- Search states rather than places
- Order tasks with dependencies
- Find routes with zero-or-one costs
- Build and test a route planner
Work through explanations and examples, try the independent practice, then use the explained answers to review your reasoning.
Try the approach
Trace a recursive return
For a non-negative integer, this function adds the values from that number down to zero. Keep track of where each call resumes.
def total_to(number):
if number == 0:
return 0
return number + total_to(number - 1)
print(total_to(3))What it prints
6The base case returns 0. The waiting calls then return 1, 3 and finally 6. Each call has its own number; returning from a call resumes its caller.
This short public example introduces an idea explored in the book.
