Module: Tree::Utils::TreeMetricsHandler
 Included in:
 TreeNode
 Defined in:
 lib/tree/utils/metrics_methods.rb
Overview
Provides utility functions to measure various tree metrics.
Instance Attribute Summary (collapse)

 (Integer) breadth
readonly
Breadth of the tree at this node's level.

 (Integer) depth
readonly
deprecated
Deprecated.
This method returns an incorrect value. Use the #node_depth method instead.

 (Integer) in_degree
readonly
The incoming edgecount of this node.

 (Integer) length
readonly
deprecated
Deprecated.
This method name is ambiguous and may be removed. Use #size instead.

 (Object) level
readonly
Alias for #node_depth.

 (Integer) node_depth
readonly
Depth of this node in its tree.

 (Integer) node_height
readonly
Height of the (sub)tree from this node.

 (Integer) out_degree
readonly
The outgoing edgecount of this node.

 (Integer) size
readonly
Total number of nodes in this (sub)tree, including this node.
Instance Attribute Details
 (Integer) breadth (readonly)
Breadth of the tree at this node's level. A single node without siblings has a breadth of 1.
Breadth is defined to be:
 Breadth

Number of sibling nodes to this node + 1 (this node itself),
i.e., the number of children the parent of this node has.
140 141 142 
# File 'lib/tree/utils/metrics_methods.rb', line 140 def breadth is_root? ? 1 : parent.children.size end 
 (Integer) depth (readonly)
This method returns an incorrect value. Use the #node_depth method instead.
Depth of the tree from this node. A single leaf node has a depth of 1.
This method is DEPRECATED and may be removed in the subsequent releases. Note that the value returned by this method is actually the:
height + 1 of the node, NOT the depth.
For correct and conventional behavior, please use #node_depth and #node_height methods instead.
124 125 126 127 128 129 
# File 'lib/tree/utils/metrics_methods.rb', line 124 def depth warn DeprecatedMethodWarning, 'This method is deprecated. Please use node_depth() or node_height() instead (bug # 22535)' return 1 if is_leaf? 1 + @children.collect { child child.depth }.max end 
 (Integer) in_degree (readonly)
The incoming edgecount of this node.
Indegree is defined as:
 Indegree

Number of edges arriving at the node (0 for root, 1 for all other nodes)

Indegree = 0 for a root or orphaned node

Indegree = 1 for a node which has a parent
154 155 156 
# File 'lib/tree/utils/metrics_methods.rb', line 154 def in_degree is_root? ? 0 : 1 end 
 (Integer) length (readonly)
67 68 69 
# File 'lib/tree/utils/metrics_methods.rb', line 67 def length size() end 
 (Object) level (readonly)
Alias for #node_depth
105 106 107 
# File 'lib/tree/utils/metrics_methods.rb', line 105 def level node_depth end 
 (Integer) node_depth (readonly)
Depth of this node in its tree. Depth of a node is defined as:
 Depth

Length of the node's path to its root. Depth of a root node is zero.
Note that the deprecated method #depth was incorrectly computing this value. Please replace all calls to the old method with #node_depth instead.
#level is an alias for this method.
96 97 98 99 
# File 'lib/tree/utils/metrics_methods.rb', line 96 def node_depth return 0 if is_root? 1 + parent.node_depth end 
 (Integer) node_height (readonly)
Height of the (sub)tree from this node. Height of a node is defined as:
 Height

Length of the longest downward path to a leaf from the node.

Height from a root node is height of the entire tree.

The height of a leaf node is zero.
80 81 82 83 
# File 'lib/tree/utils/metrics_methods.rb', line 80 def node_height return 0 if is_leaf? 1 + @children.collect { child child.node_height }.max end 
 (Integer) out_degree (readonly)
The outgoing edgecount of this node.
Outdegree is defined as:
 Outdegree

Number of edges leaving the node (zero for leafs)
165 166 167 
# File 'lib/tree/utils/metrics_methods.rb', line 165 def out_degree is_leaf? ? 0 : children.size end 
 (Integer) size (readonly)
Total number of nodes in this (sub)tree, including this node.
Size of the tree is defined as:
 Size

Total number nodes in the subtree including this node.
56 57 58 
# File 'lib/tree/utils/metrics_methods.rb', line 56 def size inject(0) {sum, node sum + 1 if node} end 