Skip to content

Commit

Permalink
added useful_code dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Kay Rhodes committed May 12, 2017
1 parent edf4cdc commit 79baa72
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions useful_code/ruby/util/tree_printer.rb
@@ -0,0 +1,49 @@
module Util
class TreePrinter
attr_accessor :is_branch
attr_accessor :get_children
attr_accessor :format_node
attr_accessor :skip_root_node

def initialize
@arms = Hash.new("| ")
@arms[""] = ""
@arms["`"] = " "
@out = ""
@is_branch = proc{|node| node.has_children? }
@get_children = proc{|node| node.children}
@format_node = proc{|node| node.to_s}
@skip_root_node = false
end

def visit(path, leader, tie, arm, node)
if(@root == node) && skip_root_node
# skip
else
node_str = @format_node.call(node)
@out << "#{leader}#{arm}#{tie}#{node_str}\n"
end
visitChildren(node, leader + @arms[arm])
@out
end

def visitChildren(path, leader)
kids = []
if(@root == path) && skip_root_node
kids = path
else
return unless @is_branch.call(path)
kids = @get_children.call(path)
end
return if kids.empty?
arms = Array.new(kids.length - 1, "|") << "`"
pairs = kids.zip(arms)
pairs.each { |e| visit(path, leader, "-- ", e[1], e[0]) }
end

def format(root)
@root = root
visit root, "", "", "", root
end
end
end

0 comments on commit 79baa72

Please sign in to comment.