Module: Tree::UpTreeAlgorithms

Includes:
BranchesProperty, NodeProperty
Included in:
ArrayTree, PathAlgorithms
Defined in:
lib/modular_tree/algorithms.rb

Overview

A down tree can only be traversed bottom-up

TODO: Add FilteredUpTreeAlgorithms

Instance Method Summary collapse

Methods included from BranchesProperty

#bare?, #branches, #each_branch

Methods included from NodeProperty

#node, #node_value

Instance Method Details

#ancestorsObject

Top-down # TODO: rename #ancestors



26
27
28
29
30
31
# File 'lib/modular_tree/algorithms.rb', line 26

def ancestors
  curr = self
  a = []
  a.unshift curr.node while curr = curr.branch
  a
end

#depthObject



33
# File 'lib/modular_tree/algorithms.rb', line 33

def depth = @depth ||= ancestors.size

#parents(*filter) ⇒ Object

Bottom-up



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/modular_tree/algorithms.rb', line 13

def parents(*filter)
  filter = Filter.new(*filter)
  curr = self
  a = []
  while curr = curr.branch
    select, continue = filter.match(curr.node)
    a.push curr.node if select
    break if !continue
  end
  a
end