-
-
Save anonymous/63446b11880df7ac961f8a747d3dba94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tree | |
attr_accessor :children, :node_name | |
def initialize(other={}) | |
if not other.nil? | |
key, children = other.first | |
@node_name = key; | |
@children = [] | |
other.values.each {|x| @children << Tree.new(x)} | |
end | |
end | |
def visit_all(&block) | |
visit &block | |
children.each {|c| c.visit_all &block} unless children.nil? | |
end | |
def visit(&block) | |
block.call self | |
end | |
end | |
t = Tree.new({'grandpa'=>{'dad'=>{'child1'=>{},'child2'=>{}},'uncle'=>{'child3'=>{},'child4'=>{}}}}) | |
t.visit_all {|x| puts x.node_name} | |
gets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment