Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement