Class: Forest
- Inherits:
-
Object
- Object
- Forest
- Defined in:
- lib/forest.rb
Instance Attribute Summary collapse
-
#orphants ⇒ Object
Returns the value of attribute orphants.
-
#trees ⇒ Object
Returns the value of attribute trees.
Instance Method Summary collapse
- #add_children ⇒ Object
- #add_root ⇒ Object
- #avg ⇒ Object
- #biggest_node ⇒ Object
- #count ⇒ Object
- #heighest_node ⇒ Object
-
#initialize(data) ⇒ Forest
constructor
A new instance of Forest.
- #max_height ⇒ Object
- #max_sum ⇒ Object
- #max_width ⇒ Object
- #print_report(num = 3) ⇒ Object
- #sd ⇒ Object
- #sum ⇒ Object
- #top(n) ⇒ Object
- #widest_node ⇒ Object
Constructor Details
#initialize(data) ⇒ Forest
Returns a new instance of Forest.
10 11 12 13 14 15 16 |
# File 'lib/forest.rb', line 10 def initialize(data) grouped_tree = data.group_by{|d| [nil, "NULL", ""].include?(d[1])} @root = grouped_tree[true] @children = grouped_tree[false] add_root add_children end |
Instance Attribute Details
#orphants ⇒ Object
Returns the value of attribute orphants.
8 9 10 |
# File 'lib/forest.rb', line 8 def orphants @orphants end |
#trees ⇒ Object
Returns the value of attribute trees.
8 9 10 |
# File 'lib/forest.rb', line 8 def trees @trees end |
Instance Method Details
#add_children ⇒ Object
79 80 81 |
# File 'lib/forest.rb', line 79 def add_children add_recursive(@trees, @children) end |
#add_root ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/forest.rb', line 71 def add_root @trees = @root.reduce({}) do |final, current| key = current.first.to_s final[key] = Tree::TreeNode.new(key, get_content(current)) final end end |
#avg ⇒ Object
30 31 32 |
# File 'lib/forest.rb', line 30 def avg sum.to_f / @trees.size end |
#biggest_node ⇒ Object
34 35 36 |
# File 'lib/forest.rb', line 34 def biggest_node max_node(:size) end |
#count ⇒ Object
18 19 20 |
# File 'lib/forest.rb', line 18 def count @trees.size end |
#heighest_node ⇒ Object
38 39 40 |
# File 'lib/forest.rb', line 38 def heighest_node max_node(:node_height) end |
#max_height ⇒ Object
50 51 52 |
# File 'lib/forest.rb', line 50 def max_height heighest_node.node_height + 1 end |
#max_sum ⇒ Object
46 47 48 |
# File 'lib/forest.rb', line 46 def max_sum biggest_node.size end |
#max_width ⇒ Object
54 55 56 |
# File 'lib/forest.rb', line 54 def max_width widest_node.children_size end |
#print_report(num = 3) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/forest.rb', line 62 def print_report(num = 3) puts "Total #{sum}: Average :#{avg} Max size :#{max_sum} Max height :#{max_height} Max width :#{max_width}, Sandard Deviation #{sd}" puts "Top #{num} trees" top(num).each_with_index {|value, index| puts "##{index + 1}, sum #{value.size}, height #{value.node_height}" puts value.print_tree } end |
#sd ⇒ Object
26 27 28 |
# File 'lib/forest.rb', line 26 def sd standard_deviation(@trees.values.map{|v| v.size }) end |
#sum ⇒ Object
22 23 24 |
# File 'lib/forest.rb', line 22 def sum @trees.values.inject(0){|sum, current| sum + current.size} end |
#top(n) ⇒ Object
58 59 60 |
# File 'lib/forest.rb', line 58 def top(n) @trees.values.sort {|x,y| y.size <=> x.size }[0..(n - 1)] end |
#widest_node ⇒ Object
42 43 44 |
# File 'lib/forest.rb', line 42 def widest_node max_node(:children_size) end |