DSA Series Chapter 13 - Binary Tree traversal, BFS, DFT/DFS, Preorder Traversal In Binary Tree


[Follow here] for daily coding insights

DSA Series Chapter 13 - Binary Tree traversal, BFS, DFT/DFS, Preorder Traversal In Binary Tree

    In Binary Tree traversal, the terms BFS and DFS (often written as DFT – Depth First Traversal) are high-level traversal categories, not individual algorithms. 


1. What is BFS in Binary Tree Traversal?

BFS – Breadth First Search (Traversal)

Meaning:
BFS visits nodes level by level, from top to bottom and left to right.

Key Characteristics

  • Traverses breadth-wise

  • Uses a Queue

  • Also called Level Order Traversal

Order Pattern

Level 0 → Level 1 → Level 2 → ...

Example Output

For a tree:

        1
       / \
      2   3
     / \
    4   5

BFS Output:

1 2 3 4 5

Exam Note

BFS in a binary tree is implemented using a queue.


2. What is DFS / DFT in Binary Tree Traversal?

DFS / DFT – Depth First Traversal

Meaning:
DFS goes deep into a subtree before visiting siblings.

Key Characteristics

  • Traverses depth-wise

  • Uses recursion or stack

  • DFS is a category, not a single traversal


3. Types of DFS (Very Important)

DFS has three standard types in a binary tree:

1. Preorder Traversal

Root → Left → Right

2. Inorder Traversal

Left → Root → Right

3. Postorder Traversal

Left → Right → Root

Exam Note

DFS in binary trees is performed using preorder, inorder, or postorder traversal.


5. Are There Any Other Traversal Types?

Standard Traversals (Syllabus Level)

✔ BFS
✔ DFS (Preorder, Inorder, Postorder)

Advanced / Special Traversals (Optional Knowledge)

These exist but are not mandatory for basic DSA:

  1. Morris Traversal

    • Inorder traversal without stack or recursion

  2. Zig-Zag Traversal

    • Alternate left-right order per level

  3. Boundary Traversal

  4. Vertical Order Traversal

  5. Diagonal Traversal

These are variations built on BFS or DFS, not new fundamental categories.


PREORDER TRAVERSAL (BINARY TREE)

Objective of this Post

This post explains Preorder Traversal of a Binary Tree in complete detail, using:

  • A single clear diagram

  • Step-by-step traversal logic

  • Standalone C program

  • Dry run, complexity, and exam notes

This is an individual post, meant to be taught and revised independently.


1. What is Preorder Traversal?

Definition (Exam-ready)

Preorder traversal is a depth-first traversal technique in which the root node is visited first, followed by the left subtree and then the right subtree.

Traversal Order

Root → Left → Right

2. Binary Tree Diagram Used

            1
          /   \
         2     3
        / \
       4   5

This same tree will be used for logic explanation, dry run, and program output.


3. Step-by-Step Preorder Traversal on Diagram

Traversal rule: Root → Left → Right

  1. Visit root → 1

  2. Go to left subtree of 1

    • Visit 2

  3. Go to left subtree of 2

    • Visit 4

  4. Left and right of 4 are NULL → return

  5. Visit right child of 2 → 5

  6. Left and right of 5 are NULL → return

  7. Go to right subtree of root 1

    • Visit 3

Final Preorder Output

1 2 4 5 3

4. Algorithm (Recursive – Plain English)

  1. If the current node is NULL, return

  2. Visit the current node

  3. Traverse the left subtree recursively

  4. Traverse the right subtree recursively


5. Recursive Algorithm (Pseudo Code)

PREORDER(node)
{
    if(node != NULL)
    {
        visit(node)
        PREORDER(node->left)
        PREORDER(node->right)
    }
}

6. C Program – Preorder Traversal (Recursive)

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *left;
    struct Node *right;
};

struct Node* createNode(int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

void preorder(struct Node* root)
{
    if (root != NULL)
    {
        printf("%d ", root->data);
        preorder(root->left);
        preorder(root->right);
    }
}

int main()
{
    struct Node* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    printf("Preorder Traversal: ");
    preorder(root);

    return 0;
}

7. Dry Run of Recursive Calls

preorder(1)
  print 1
  preorder(2)
    print 2
    preorder(4)
      print 4
    preorder(NULL)
    preorder(5)
      print 5
  preorder(3)
    print 3

8. Time and Space Complexity

Time Complexity

  • O(n) where n is number of nodes

  • Each node is visited exactly once

Space Complexity

  • O(h) due to recursion stack

  • h = height of tree

Worst case (skewed tree): O(n)


9. Important Applications of Preorder Traversal

  • Creating a copy of a tree

  • Prefix expression generation

  • Tree serialization

  • Structure reconstruction


10. Common Exam & Interview Mistakes

  • Printing root after left subtree (that becomes inorder)

  • Forgetting base condition (root != NULL)

  • Assuming preorder always gives sorted output


11. Practice Questions

  1. Write preorder traversal for the following tree (drawn in exam).

  2. Predict preorder output for a skewed binary tree.

  3. Convert recursive preorder traversal into iterative logic.

  4. What happens to recursion stack in worst case?


12. Learning Outcome 

After this post, a student can:

  • Explain preorder traversal confidently

  • Dry-run preorder traversal on any diagram

  • Write recursive preorder traversal in C

  • Analyze time and space complexity

ALSO CHECK 

DS(Data Structure) Python&C Edition

DSA Series Chapter 1 Time & Space Complexity ( C Edition)

DSA Series Chapter 2 Arrays  (C Version)


DSA Series Chapter 2 Arrays  (Python Version)

DSA Series Chapter 3 Strings (C Version)


DSA Series Chapter 3 Strings (Python Version)


DSA Series Chapter 4 Linked Lists (Python Version)


DSA Series Chapter 4 Linked Lists (C Version)


DSA Series Chapter 5 Stacks (Python Version)


DSA Series Chapter 5  Stacks  (C Version)
























Tail Recursion Optimization: How Compilers Convert Recursion Into Iteration


Advanced Recursion: Memoization vs. Tabulation — The Deep Optimization Blueprint for Professionals


Advanced Sorting Algorithms: Merge Sort Internals — Merge Tree, Cache Behavior & Real Cost Analysis


Enjoyed this post? [Follow here] for daily coding insights.

Comments