Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Breadth-First Search/Traversal in a Binary Tree

Breadth-First Search ( or Tra­ver­sal) also know as Level Order Traversal.

Exam­ple:

What is Breadth First Search:

Breadth-first search (BFS) is an algo­rithm for tra­vers­ing or search­ing tree or graph data struc­tures. It starts at the tree root and explores the neigh­bor nodes first, before mov­ing to the next level neigh­bors. (Ref­er­ence — Wiki)

Approach:

  • Take a Empty Queue.
  • Start from the root, insert the root into the Queue.
  • Now while Queue is not empty,
    • Extract the node from the Queue and insert all its chil­dren into the Queue.
    • Print the extracted node.

Com­plete Code:

Output:
Breadth First Search : 
 5 10 15 20 25 30 35

 

You may also like...