The Binary Tree Algorithm

The Binary Tree algorithm is, quite possibly, the simplest algorithm around for generating a maze. As its name suggests, it merely requires you to choose between two possible options at each step. For each cell in the grid, you decide whether to carve a passage north or east. By the time you’ve done so for every cell, you have a maze!

This process of looking at cells is called visiting them. Visiting them in some order is walking the grid. Some walks might be random, choosing directions arbitrarily from step to step, like the ones we’ll see in Chapter 4, Avoiding Bias with Random Walks. Others are more predictable. For Binary Tree, it turns out that we can do it either way. The algorithm really doesn’t care what ...

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.