Module: Jumoku::ArborescenceBuilder
- Includes:
- Extended, RawDirectedTreeBuilder
- Included in:
- Arborescence
- Defined in:
- lib/jumoku/builders/arborescence.rb
Overview
This builder extends RawDirectedTreeBuilder implementation.
It provides a directed tree which acts as a hierarchical structure, known as an arborescence.
Constant Summary
Constants included from Shared
Instance Attribute Summary
Attributes included from Shared
Instance Method Summary collapse
- #add_branch!(u, v = nil, l = nil) ⇒ Object
-
#children(parent) ⇒ Array<Node>
(also: #children_of)
Return the list of a node’s children.
-
#leaf?(node) ⇒ true, false
Check whether a node is a leaf.
-
#leaves ⇒ Array<Node>
Return the list of leaf nodes.
-
#leaves?(*nodes) ⇒ true, false
Check whether all nodes from a list are leaves.
-
#neighbours(node, options = {:siblings => false}) ⇒ Array<Node>
(also: #cousins)
Return the list of a node’s neighbours, that is, children of node’s parent siblings (cousins).
-
#neighbours?(node1, node2, options = {:siblings => false}) ⇒ true, false
(also: #cousins?)
Check whether two nodes are neighbours.
-
#parent(node) ⇒ Node?
(also: #parent_of)
Return the parent node of another.
-
#parent?(node, maybe_child = nil) ⇒ Object
Check whether a node is a parent.
-
#root ⇒ Node
Find the root node.
-
#root?(node) ⇒ true, false
Check whether a node is the root.
-
#root_edges ⇒ Array<Plexus::Arc>
Return the list of arcs branched out from the root node.
-
#siblings(node) ⇒ Array<Node>
(also: #siblings_of)
Return the siblings for a node.
-
#siblings?(node1, node2) ⇒ true, false
Check whether two nodes are siblings.
Methods included from Extended
#add_branch, #add_branches, #add_branches!, #add_consecutive_nodes, #add_consecutive_nodes!, #add_node, #add_nodes, #add_nodes!, #branch?, #branches?, #branches_among?, #node?, #nodes?, #nodes_among?, #num_branches, #num_nodes, #remove_branch, #remove_branches, #remove_branches!, #remove_node, #remove_nodes, #remove_nodes!
Methods included from RawDirectedTreeBuilder
Methods included from Shared
#add_node!, #branches, #empty?, included, #nodes, #remove_branch!, #remove_node!, #terminal?, #terminal_nodes, #valid?
Instance Method Details
#add_branch!(u, v = nil, l = nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/jumoku/builders/arborescence.rb', line 11 def add_branch!(u, v = nil, l = nil) return super(u,v,l) if @_options[:free_flow] return add_branch!(u.source, u.target, l) if u.is_a? _branch_type return super(u,v,l) if nodes.size < 2 nodes.include?(u) ? super(u,v,l) : super(v,u,l) end |
#children(parent) ⇒ Array<Node> Also known as: children_of
Return the list of a node’s children.
49 50 51 |
# File 'lib/jumoku/builders/arborescence.rb', line 49 def children(parent) adjacent(parent) end |
#leaf?(node) ⇒ true, false
Check whether a node is a leaf.
169 170 171 |
# File 'lib/jumoku/builders/arborescence.rb', line 169 def leaf?(node) terminal?(node) && out_degree(node) == 0 end |
#leaves ⇒ Array<Node>
Return the list of leaf nodes.
158 159 160 161 162 |
# File 'lib/jumoku/builders/arborescence.rb', line 158 def leaves terminal_nodes.delete_if do |node| out_degree(node) > 0 end end |
#leaves?(*nodes) ⇒ true, false
Check whether all nodes from a list are leaves.
178 179 180 |
# File 'lib/jumoku/builders/arborescence.rb', line 178 def leaves?(*nodes) nodes.to_a.flatten.all? { |node| leaf?(node) } end |
#neighbours(node, options = {:siblings => false}) ⇒ Array<Node> Also known as: cousins
Return the list of a node’s neighbours, that is, children of node’s parent siblings (cousins).
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/jumoku/builders/arborescence.rb', line 119 def neighbours(node, = {:siblings => false}) # special case when the node is the root return [] if root?(node) # special case when the node is a root's child if root?(parent(node)) nghb = children(parent(node)) nghb.delete_if { |child| child == node } unless [:siblings] return nghb.map { |child| [child] } end # general case nghb = siblings(parent(node)).map do |sibling| children(sibling) end nghb << siblings(node) if [:siblings] nghb end |
#neighbours?(node1, node2, options = {:siblings => false}) ⇒ true, false Also known as: cousins?
Check whether two nodes are neighbours. To include the node’s siblings in the matching candidates, pass the :siblings option to true.
149 150 151 |
# File 'lib/jumoku/builders/arborescence.rb', line 149 def neighbours?(node1, node2, = {:siblings => false}) neighbours(node1, ).any? { |set| set.include? node2 } end |
#parent(node) ⇒ Node? Also known as: parent_of
Return the parent node of another.
60 61 62 63 64 |
# File 'lib/jumoku/builders/arborescence.rb', line 60 def parent(node) parent = adjacent(node, :direction => :in) raise JumokuError, "Inconsistent directed tree (more than one parent for the node!)" if parent.size > 1 parent.empty? ? nil : parent.first end |
#parent?(node) ⇒ true, false #parent?(node, maybe_child) ⇒ true, false
Check whether a node is a parent. If another node is provided as second parameter, check whether the former node is the parent of the latter node.
79 80 81 82 83 84 85 |
# File 'lib/jumoku/builders/arborescence.rb', line 79 def parent?(node, maybe_child = nil) if maybe_child.nil? !children(node).empty? else children(node).include? maybe_child end end |
#root ⇒ Node
Find the root node.
23 24 25 26 |
# File 'lib/jumoku/builders/arborescence.rb', line 23 def root return nodes.first if nodes.size == 1 nodes.find { |node| root?(node) } end |
#root?(node) ⇒ true, false
Check whether a node is the root.
40 41 42 |
# File 'lib/jumoku/builders/arborescence.rb', line 40 def root?(node) in_degree(node) == 0 && out_degree(node) > 0 end |
#root_edges ⇒ Array<Plexus::Arc>
Return the list of arcs branched out from the root node.
32 33 34 |
# File 'lib/jumoku/builders/arborescence.rb', line 32 def root_edges adjacent(root, :type => :edges) end |
#siblings(node) ⇒ Array<Node> Also known as: siblings_of
Return the siblings for a node. Siblings are other node’s parent children.
92 93 94 95 96 97 |
# File 'lib/jumoku/builders/arborescence.rb', line 92 def siblings(node) return [] if root?(node) siblings = children(parent(node)) siblings.delete(node) siblings end |
#siblings?(node1, node2) ⇒ true, false
Check whether two nodes are siblings.
106 107 108 |
# File 'lib/jumoku/builders/arborescence.rb', line 106 def siblings?(node1, node2) siblings(node1).include?(node2) end |