Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Level order traversal #1392

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions data_structures/binary_trees/level_order_traversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @file
* @brief Implements level order traversal of a binary tree, also known as
* Breadth-First Search (BFS)
*
* @details
* This program systematically traverses every node in the tree, visiting all
* nodes at a particular level before moving to the next level. It starts from
* the root node and explores all of its neighbors at the present depth level
* before moving to the nodes at the next level.
*
*
* It is assumed that nodes and trees have been created as per create_node.c
* It is assumed that the enQueue and deQueue functions have been initialized
* and defined elsewhere.
*
* [Further Details here](https://en.wikipedia.org/wiki/Breadth-first_search)
* @author [Diwas Dahal](https://github.com/di-was)
*/

#include <stdio.h>

void level_order_traversal(struct node *root)
{
struct node *temp_node = root;

while (temp_node) // if tree is not empty
{
printf("%d \n", temp_node->data);
if (temp_node->left)
{
enQueue(temp_node->leftNode);
}
if (temp_node->right)
{
enQueue(temp_node->rightNode);
}

temp_node = deQueue();
}
}

int main(void)
{
/* level order traversal can be done by simply invoking
the function with a pointer to the root node given that
enQueue and deQueue are within the scope of this program
and function.
*/
return 0;
}
Loading