# A BST class. Again, the purpose is a demo, not because you need one when
# you already have the Ruby datatypes.
# Again, we include the follower class.
@val = d
@lft, @rgt = nil
attr_reader :lft, :rgt, :val
attr_writer :lft, :rgt
# Our next function just moves right. (See below)
# Insert a new node into the subtree rooted here.
if new.val < @val then
if @lft == nil then
@lft = new
if @rgt == nil then
@rgt = new
# This runs for each value in the tree in sorted order. The block
# parameter is an object known as a closure. They contain executable
# code, and blocks can become closures (see below).
if @lft then @lft.each(block) end
if @rgt then @rgt.each(block) end
# Get the printing facility.
@root = Node.new(first)
# Insert a value. Most of the work in Node#insert
# Stepping right from the root until nil gives the max value in the tree.
# The &blk notation converts the block used in the iterator into a
# closure object, which we send to Node#each.
class also includes the common operations.
This one also implements an
each iterator. This one uses
& converts the code block
to a Closure object. Closures are executable code. It is bound to
blk which is sent to
Node#each. There it
becomes a parameter
block, and is run with the