def printDendrogram(t, sep=3) isPair = lambda do |t| return t.class == Array && t.length == 2 end maxHeight = lambda do |t| if isPair.call(t) h = [maxHeight.call(t[0]), maxHeight.call(t[1])].max else h = t.to_s.length end return h + sep end activeLevels = {} traverse = lambda do |t, h, isFirst| if isPair.call(t) traverse.call(t[0], h-sep, 1) s = [" "]*(h-sep) s << "|" else s = t.chars s << (" ") end while s.length < h s << ("-") end if (isFirst >= 0) s << ("+") if isFirst > 0 activeLevels[h] = 1 else activeLevels.delete(h) end end a = activeLevels.keys a.sort! a.each do |l| if s.length < l while s.length < l s << (" ") end s << ("|") end end puts s.join("") if isPair.call(t) traverse.call(t[1], h-sep, 0) end end traverse.call(t, maxHeight.call(t), -1) end printDendrogram [ [ ['a' , 'b'] , 'c' ], [ [ 'd' , 'e' ], [ 'f', 'g' ] ] ]