Massimo Caliman
by Massimo Caliman
~1 min read

Categories

  • Programming

Tags

  • algorithms
  • code
  • data-structures
  • en

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” (Jon Ribbens)

We start with the generic traversal algorithm and using a queue to represent S we obtain breadth-first traversal (Breadth-First Search or BFS).

Nodes are visited by levels, first root, then children of the root, then children of the children.

procedure BFS(node r)
   Queue C
   C.enqueue(r)
   while not C.isEmpty() do 
          u ← C.dequeue()
          if u ≠ null then
              visit(u)
              C.enqueue(leftChildOf(u))
              C.enqueue(rightChildOf(u))
          fi
      od
end