Class: PatchworkInternal::Traversals

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

Class Method Summary collapse

Class Method Details

.depth_first(start_node, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/patchwork/traversals.rb', line 3

def self.depth_first(start_node, &block)
  visited_nodes_cache = {}
  nodes_to_visit = [start_node]
  until nodes_to_visit.empty?
    next_node = nodes_to_visit.pop
    next if visited_nodes_cache[next_node.id]
    visited_nodes_cache[next_node.id] = true
    next_node.visit(&block)
    nodes_to_visit.concat(next_node.linked_nodes)
  end
end