Breadth-First Traversal Binary Tree

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This Python challenge is about implementing breadth-first traversal on a binary tree.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/graphs_trees -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} algorithm/recursion_dynamic -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/variables_data_types -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/strings -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/type_conversion -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/conditional_statements -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/while_loops -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/tuples -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/function_definition -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/default_arguments -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/importing_modules -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/using_packages -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/standard_libraries -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/classes_objects -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/constructor -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/polymorphism -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/encapsulation -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/raising_exceptions -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} python/build_in_functions -.-> lab-268834{{"`Breadth-First Traversal Binary Tree`"}} end

Tree BFS

Problem

Given a binary tree, implement a function that performs a breadth-first traversal on the tree. The function should call an input method visit_func on each node when it is processed.

Requirements

To solve this problem, the following requirements must be met:

  • A Node class with an insert method is already available.
  • The solution should fit in memory.
  • The visit_func method should be called on each node when it is processed.

Example

Breadth-First Traversal

Suppose we have a binary tree with the following structure:

    5
   / \
  2   8
 / \
1   3

Performing a breadth-first traversal on this tree would result in the following sequence of nodes being visited: 5, 2, 8, 1, 3.

Summary

In this Python challenge, we implemented breadth-first traversal on a binary tree. The solution required a Node class with an insert method, and the visit_func method was called on each node when it was processed. The solution was able to fit in memory and produced the correct sequence of nodes when performing a breadth-first traversal on a binary tree.

Other Algorithm Tutorials you may like