Class: Forest

Inherits:
Object
  • Object
show all
Defined in:
lib/forest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#orphantsObject

Returns the value of attribute orphants.



8
9
10
# File 'lib/forest.rb', line 8

def orphants
  @orphants
end

#treesObject

Returns the value of attribute trees.



8
9
10
# File 'lib/forest.rb', line 8

def trees
  @trees
end

Instance Method Details

#add_childrenObject



79
80
81
# File 'lib/forest.rb', line 79

def add_children
  add_recursive(@trees, @children)
end

#add_rootObject



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

#avgObject



30
31
32
# File 'lib/forest.rb', line 30

def avg
  sum.to_f / @trees.size
end

#biggest_nodeObject



34
35
36
# File 'lib/forest.rb', line 34

def biggest_node
  max_node(:size)
end

#countObject



18
19
20
# File 'lib/forest.rb', line 18

def count
  @trees.size
end

#heighest_nodeObject



38
39
40
# File 'lib/forest.rb', line 38

def heighest_node
  max_node(:node_height)
end

#max_heightObject



50
51
52
# File 'lib/forest.rb', line 50

def max_height
  heighest_node.node_height + 1
end

#max_sumObject



46
47
48
# File 'lib/forest.rb', line 46

def max_sum
  biggest_node.size
end

#max_widthObject



54
55
56
# File 'lib/forest.rb', line 54

def max_width
  widest_node.children_size
end


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

#sdObject



26
27
28
# File 'lib/forest.rb', line 26

def sd
  standard_deviation(@trees.values.map{|v| v.size })
end

#sumObject



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_nodeObject



42
43
44
# File 'lib/forest.rb', line 42

def widest_node
  max_node(:children_size)
end