Preorder Tree Traversal of Binary Tree in C++
Last Updated :
28 Jun, 2024
A binary tree is a non-linear hierarchical data structure where each node has at most two children which are referred to as the left child and the right child. As it has non-linear structure it can be traversed in multiple ways one such way is pre-order traversal which is a technique in which nodes are recursively visited in the order: left child, root, right child.
In this article, we will learn how to implement preorder binary tree traversal in C++, its algorithm and analyze its space and time complexity.
Preorder Traversal of Binary Tree in C++
Preorder traversal is a traversal technique used to visit all the nodes in a specific sequence of the binary tree. This process involves visiting the root node first, then the left subtree, and finally the right subtree.
Pre-order Traversal in Binary TreeSteps for Preorder Traversal:
- Visit the root node.
- Recursively perform a preorder traversal of the left subtree.
- Recursively perform a preorder traversal of the right subtree.
Consider the following example:
Preorder Traversal ExampleFor preorder traversal of the above binary tree:
- Start with the root node, which is 1.
- Move to the left child, which is 2.
- Visit the left child of 2, which is 4.
- Move back to 2 and visit its right child, which is 5.
- Move back to the root node and visit the right child, which is 3.
So, the preorder traversal of the tree is: 1, 2, 4, 5, 3.
Algorithm for Pre-order Binary Tree Traversal
Below is the algorithm for the preorder binary tree traversal:
preorder(TreeNode* root) {
if (root == NULL)
return;
cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}
C++ Program to Implement Preorder Traversal of the Binary Tree
The following program demonstrates how we can implement the Preorder traversal in a binary tree in C++.
C++
// C++ Program for Preorder Traversal in a Binary Tree
#include <iostream>
using namespace std;
// BINARY TREE IMPLEMENTATION
// Class definition for a binary tree node
class Node {
public:
int data;
Node* left;
Node* right;
// Constructor to create a new node
Node(int data)
{
this->data = data;
this->left = nullptr;
this->right = nullptr;
}
};
// Class definition for a binary tree
class BinaryTree {
public:
Node* root;
// Constructor to initialize the root
BinaryTree() { root = nullptr; }
// PREORDER TRAVERSAL
// Function to perform preorder traversal
void preorderTraversal(Node* node)
{
if (node != nullptr) {
cout << node->data << " ";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
}
};
int main()
{
// Initialize a binary tree
BinaryTree btree;
// Create the binary tree
btree.root = new Node(1);
btree.root->left = new Node(2);
btree.root->right = new Node(3);
btree.root->left->left = new Node(4);
btree.root->left->right = new Node(5);
btree.root->right->left = new Node(6);
btree.root->right->right = new Node(7);
// Perform the preorder traversal
cout << "Preorder traversal of the binary tree is:"
<< endl;
btree.preorderTraversal(btree.root);
cout << endl;
return 0;
}
OutputPreorder traversal of the binary tree is:
1 2 4 5 3 6 7
Time Complexity: O(N) where N is the total number of nodes as it traverses all the nodes at least once.
Auxiliary Space:
- O(1) if no recursion stack space is considered.
- Otherwise, O(h) where h is the height of the tree
- In the worst case, h can be the same as N (when the tree is a skewed tree)
- In the best case, h can be the same as logN (when the tree is a complete tree)
Applications of Preorder Traversal
Following are some common applications of the preorder traversal:
- It is used to evaluate or print the prefix notation of an expression.
- It is used to create a deep copy of a tree by visiting each node before it's children.
- It is used for the serialization and deserialization of a tree.
- It is also used in operating systems to list the contents of a directory in a hierarchical order.
Related Articles
You can also go through these articles to improve your knowledge about the binary tree and it's traversals: