C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Binary Search Tree — Level Order Traversal (BFS)

Level Order Traversal (Breadth-First Search) of a Binary Search Tree (BST) — an important traversal technique used in many real-world tree operations like serialization, printing hierarchical structures, and computing height/width levels.

 

Binary Search Tree — Level Order Traversal (BFS)

Concept Overview

Level Order Traversal means visiting the nodes level by level from top to bottom, and left to right within each level.

This traversal is implemented using a Queue data structure.

How It Works

  1. Start from the root node.
  2. Enqueue the root node.
  3. While the queue is not empty:
    • Dequeue a node and print its data.
    • Enqueue its left child, if it exists.
    • Enqueue its right child, if it exists.

 

C Program: Binary Search Tree Traversals

Level Order Traversal of BST

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for BST Node

struct Node {

    int data;

    struct Node *left, *right;

};

 

// Function to create a new node

struct Node* createNode(int value) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = value;

    newNode->left = newNode->right = NULL;

    return newNode;

}

 

// Function to insert a new node into BST

struct Node* insert(struct Node* root, int value) {

    if (root == NULL)

        return createNode(value);

 

    if (value < root->data)

        root->left = insert(root->left, value);

    else if (value > root->data)

        root->right = insert(root->right, value);

 

    return root;

}

 

// Queue node for level order traversal

struct QueueNode {

    struct Node* data;

    struct QueueNode* next;

};

 

// Queue structure

struct Queue {

    struct QueueNode *front, *rear;

};

 

// Function to create an empty queue

struct Queue* createQueue() {

    struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));

    q->front = q->rear = NULL;

    return q;

}

 

// Enqueue function

void enqueue(struct Queue* q, struct Node* node) {

    struct QueueNode* temp = (struct QueueNode*)malloc(sizeof(struct QueueNode));

    temp->data = node;

    temp->next = NULL;

    if (q->rear == NULL) {

        q->front = q->rear = temp;

        return;

    }

    q->rear->next = temp;

    q->rear = temp;

}

 

// Dequeue function

struct Node* dequeue(struct Queue* q) {

    if (q->front == NULL)

        return NULL;

 

    struct QueueNode* temp = q->front;

    struct Node* node = temp->data;

    q->front = q->front->next;

 

    if (q->front == NULL)

        q->rear = NULL;

 

    free(temp);

    return node;

}

 

// Check if queue is empty

int isEmpty(struct Queue* q) {

    return q->front == NULL;

}

 

// Level Order Traversal function

void levelOrder(struct Node* root) {

    if (root == NULL)

        return;

 

    struct Queue* q = createQueue();

    enqueue(q, root);

 

    printf("Level Order Traversal: ");

    while (!isEmpty(q)) {

        struct Node* current = dequeue(q);

        printf("%d ", current->data);

 

        if (current->left != NULL)

            enqueue(q, current->left);

        if (current->right != NULL)

            enqueue(q, current->right);

    }

    printf("\n");

}

 

int main() {

    struct Node* root = NULL;

 

    // Construct the BST

    root = insert(root, 50);

    root = insert(root, 30);

    root = insert(root, 70);

    root = insert(root, 20);

    root = insert(root, 40);

    root = insert(root, 60);

    root = insert(root, 80);

 

    // Display traversals

    levelOrder(root);

 

    return 0;

}

Output

 
OUTPUT :
Level Order Traversal: 50 30 70 20 40 60 80

Visual Representation

For the BST:

        50

       /  \

     30    70

    / \    / \

  20  40  60  80

Level Order Traversal visits nodes as follows:

Level

Nodes Visited

Output

Level 1

50

50

Level 2

30, 70

30 70

Level 3

20, 40, 60, 80

20 40 60 80

Final Output

50 30 70 20 40 60 80

 

 Complexity Analysis

Operation

Time Complexity

Space Complexity

Level Order Traversal

O(n)

O(n)

  • Each node is visited exactly once → O(n)
  • Queue stores nodes level by level → O(n) in the worst case

 Key Points

  • Level Order Traversal = Breadth-First Search (BFS).
  • Implemented using queue, not recursion.
  • Used in applications like:
    • Printing hierarchical data
    • Tree serialization/deserialization
    • Finding height or width of a tree