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.



10
11
12
13
# File 'lib/kruskal/tree.rb', line 10

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

Instance Method Details

#add(value, source, target) ⇒ Object



31
32
33
34
# File 'lib/kruskal/tree.rb', line 31

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

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

Returns:

  • (Boolean)


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

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



27
28
29
# File 'lib/kruskal/tree.rb', line 27

def hash
  @relations.hash
end

#inspectObject



43
44
45
# File 'lib/kruskal/tree.rb', line 43

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

#merge!(other_tree) ⇒ Object



36
37
38
39
40
41
# File 'lib/kruskal/tree.rb', line 36

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