Class: Kruskal::Tree

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/kruskal/tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(index, relations = []) ⇒ Tree

Returns a new instance of Tree.



8
9
10
11
# File 'lib/kruskal/tree.rb', line 8

def initialize(index, relations = [])
  @index, @relations = index, relations
  reindex
end

Instance Method Details

#add(value, source, target) ⇒ Object



29
30
31
32
# File 'lib/kruskal/tree.rb', line 29

def add(value, source, target)
  @relations << [value, source, target]
  index(target)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
# File 'lib/kruskal/tree.rb', line 13

def eql?(other)
  if other.is_a?(self.class)
    other.relations_equal_to(@relations)
  elsif other.is_a?(Array)
    @relations == other
  else
    false
  end
end

#hashObject



25
26
27
# File 'lib/kruskal/tree.rb', line 25

def hash
  @relations.hash
end

#inspectObject



41
42
43
# File 'lib/kruskal/tree.rb', line 41

def inspect
  "Tree#{@relations.inspect}"
end

#merge!(other_tree) ⇒ Object



34
35
36
37
38
39
# File 'lib/kruskal/tree.rb', line 34

def merge!(other_tree)
  other_tree.add_relations_to(@relations)
  other_tree.each do |(v, s, t)|
    index(s, t)
  end
end