Sha256: 9f0319af6c49577f0d1539a635d6d54713d26642d43bed84d61494b49a49fc29
Contents?: true
Size: 1.5 KB
Versions: 1
Compression:
Stored size: 1.5 KB
Contents
class BasicTree class Children include Enumerable def each(&block) trees.each(&block) end def empty? trees.empty? end private def trees @trees ||= [] end def <<(tree) trees << tree end end include Enumerable def initialize(object, &block) self.object = object yield self if block_given? end attr_accessor :object attr_reader :parent def add(object, &block) self.class.new(object) do |child| children.send(:<<, child) child.send(:parent=, self) yield child if block_given? end end def each(&block) children.each(&block) end def path ancestors << self end def ancestors root? ? [] : (parent.ancestors << parent) end def descendants(depth = -1) trees = [] if depth != 0 children.each do |child| trees << child trees += child.descendants(depth - 1) end end trees end def subtree(depth = -1) [self] + descendants(depth) end def siblings parent.children.select { |child| child != self } end def root ancestors.first end def level path.size end def root? !parent end def leaf? children.empty? end def leaves trees = [] children.each do |child| child.leaf? ? (trees << child) : (trees += child.leaves) end trees end private attr_writer :parent def children @children ||= Children.new end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
basic_tree-0.0.1 | lib/basic_tree.rb |