Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tree
- attr_accessor :children, :node_name
- def initialize(hash)
- @children = []
- hash.keys.each { |k|
- @node_name = k
- hash[k].keys.each { |ck|
- @children << Tree.new(hash[k][ck])
- }
- }
- end
- # def initialize(name, hash)
- # @children = []
- # @node_name = name
- # hash[name].keys.each { |k|
- # @children << Tree.new(k, hash[name][k])
- # }
- # end
- def visit_all(&block)
- visit &block
- children.each {|c| c.visit_all &block}
- end
- def visit(&block)
- block.call self
- end
- end
- x = {"grandpa" => { "dad" => {"child 1" => {}, "child 2" => {} }, "uncle" => {"child 3" => {}, "child 4" => {} } } }
- y = Tree.new(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement