Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Preorder Recursive Traversal of a Given Binary Tree in C++



In this article, we'll show you how to write a C++ program to perform a preorder recursive traversal of a binary tree. A binary tree is a tree where each node has zero, one, or two children. Even an empty tree is called a valid binary tree.

Traversal means visiting all the nodes in a tree exactly once. In preorder traversal, we visit the root first, then the left child, and finally the right child.

Let's take a small binary tree and see an example:

Here, we start at the root(3), then go to the left child (6), then its left(5), then right(2), then come back and move to the right subtree of 3 that is (4), then 9, and finally 8.

Using Recursion for Preorder Traversal

To perform preorder traversal using recursion, we start by visiting the current node. Recursion is a technique where a function calls itself to break down the problem into smaller parts until it reaches a base case. After visiting the root node, we recursively move to the left child, followed by the right child.

Here's how we do this:

  • First, we create a function called PreOrder() that takes the root node of the tree as input.
  • Inside the function, we check if the node is NULL. If it is, we return. This is our base case.
  • If the node is valid, we print its value (i.e., we visit it).
  • Then, we recursively call PreOrder() for the left child.
  • After that, we call PreOrder() for the right child.

By following these steps, we visit all nodes in the order: root -> left -> right.

C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree

Here's a complete C++ program to perform preorder traversal using recursion:

#include <iostream>
using namespace std;

// Define the structure of the binary tree node
struct BinaryTreeNode {
    int data;  // Store node value
    BinaryTreeNode* left;  // Pointer to left child
    BinaryTreeNode* right; // Pointer to right child

    BinaryTreeNode(int value) {  // Constructor to initialize node
        data = value;
        left = right = nullptr;  // Initialize child pointers to nullptr
    }
};

// Recursive function for preorder traversal
void PreOrder(BinaryTreeNode* root) {
    if (root != nullptr) {  // Base case: if node is not null
        cout << root->data << " ";  // Visit the root node (print value)
        PreOrder(root->left);  // Recursively traverse the left subtree
        PreOrder(root->right); // Recursively traverse the right subtree
    }
}

int main() {
    // Creating the binary tree
    BinaryTreeNode* root = new BinaryTreeNode(3);  // Root node with value 3
    root->left = new BinaryTreeNode(6);  // Left child of root with value 6
    root->right = new BinaryTreeNode(5); // Right child of root with value 5
    root->left->left = new BinaryTreeNode(2);  // Left child of node 6 with value 2
    root->left->right = new BinaryTreeNode(4); // Right child of node 6 with value 4
    root->right->right = new BinaryTreeNode(9); // Right child of node 5 with value 9
    root->right->right->left = new BinaryTreeNode(8); // Left child of node 9 with value 8
    // Output the preorder traversal
    cout << "Preorder Traversal: ";
    PreOrder(root);  // Call preorder traversal function
    return 0;
}

Once we run the above program, we'll get the following output, which shows the nodes visited in preorder:

Preorder Traversal: 3 6 2 4 5 9 8

Time Complexity: O(n), where n is the number of nodes in the tree. Each node is visited only once.

Space Complexity: O(h), where h is the height of the tree. This is because of the recursive calls.

Updated on: 2025-05-16T19:13:22+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements