Implementing the Binary Tree Algorithm

Learn to implement the Binary Tree algorithm in Ruby.

Implementing the Binary Tree algorithm on the Grid

Let’s start with the Binary Tree algorithm. It works simply by visiting each cell in the grid and choosing to carve a passage either toward the north or east.

The following code does just that. We’re going to put it in its own class so that we can easily reuse this code whenever we want. We've saved it to a file named binary_tree.rb, and made sure it’s in the same directory as the cell.rb and grid.rb files we created earlier.


Get hands-on with 1200+ tech skills courses.