Module: Tree::Utils::TreeMetricsHandler
- Included in:
 - TreeNode
 
- Defined in:
 - lib/tree/utils/metrics_methods.rb
 
Overview
Provides utility functions to measure various tree metrics.
Metrics and Measures collapse
- 
  
    
      #breadth  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Breadth of the tree at this node’s level.
 - 
  
    
      #in_degree  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
The incoming edge-count of this node.
 - 
  
    
      #length  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  deprecated
  
  
    Deprecated. 
This method name is ambiguous and may be removed. Use
 - 
  
    
      #level  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Alias for #node_depth.
 - 
  
    
      #node_depth  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Depth of this node in its tree.
 - 
  
    
      #node_height  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Height of the (sub)tree from this node.
 - 
  
    
      #out_degree  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
The outgoing edge-count of this node.
 - 
  
    
      #size  ⇒ Integer 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Total number of nodes in this (sub)tree, including this node.
 
Instance Attribute Details
#breadth ⇒ Integer (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.
      117 118 119  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 117 def breadth root? ? 1 : parent.children.size end  | 
  
#in_degree ⇒ Integer (readonly)
The incoming edge-count of this node.
In-degree is defined as:
- In-degree
 - 
Number of edges arriving at the node (0 for root, 1 for
 
all other nodes)
- 
In-degree = 0 for a root or orphaned node
 - 
In-degree = 1 for a node which has a parent
 
      132 133 134  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 132 def in_degree root? ? 0 : 1 end  | 
  
#length ⇒ Integer (readonly)
      66 67 68  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 66 def length size end  | 
  
#level ⇒ Object (readonly)
Alias for #node_depth
      104 105 106  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 104 def level node_depth end  | 
  
#node_depth ⇒ Integer (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.
#level is an alias for this method.
      94 95 96 97 98  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 94 def node_depth return 0 if root? 1 + parent.node_depth end  | 
  
#node_height ⇒ Integer (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.
 
      79 80 81 82 83  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 79 def node_height return 0 if leaf? 1 + @children.collect(&:node_height).max end  | 
  
#out_degree ⇒ Integer (readonly)
The outgoing edge-count of this node.
Out-degree is defined as:
- Out-degree
 - 
Number of edges leaving the node (zero for leafs)
 
      143 144 145  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 143 def out_degree leaf? ? 0 : children.size end  | 
  
#size ⇒ Integer (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.
 
      54 55 56  | 
    
      # File 'lib/tree/utils/metrics_methods.rb', line 54 def size inject(0) { |sum, node| sum + 1 if node } end  |