Class: BFS

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

Instance Method Summary collapse

Constructor Details

#initialize(visitor) ⇒ BFS

Returns a new instance of BFS.



2
3
4
5
# File 'lib/visitor/bfs.rb', line 2

def initialize(visitor)
  @visitor = visitor
  @q = Array.new
end

Instance Method Details

#traverse(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/visitor/bfs.rb', line 7

def traverse(node)
  @q.unshift(node)

  while (@q.size > 0) do
    node = @q.pop
    if (@visitor.preVisit(node)) then
      if (node.children != nil)  then
        node.children.values.each do |child|
          @q.unshift(child)
        end
      end
    end
  end

end