Slide 47
Slide 47 text
Detect Violations with Pattern Matching
Handle all four cases
private_class_method def self.balance color, key, left, right
# x, y, z are keys
# a, b, c, d are nodes
case [color, key, left, right]
# HEAD LEFT LEFT-LEFT
in [:black, z, [:red, y, [:red, x, a, b], c], d]
# HEAD LEFT LEFT-RIGHT
in [:black, z, [:red, x, a, [:red, y, b, c]], d]
# HEAD RIGHT RIGHT-RIGHT
in [:black, x, a, [:red, y, b, [:red, z, c, d]]]
# HEAD RIGHT RIGHT-LEFT
in [:black, x, a, [:red, z, [:red, y, b, c], d]]
else
return Node.new(color, key, left, right)
end
Node.new(:red, y, Node.new(:black, x, a, b), Node.new(:black, z, c, d))
end