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:
Similar Reads
Preorder Tree Traversal of Binary Tree in C A binary tree is a hierarchical data structure composed of nodes where each node has at most two children. It can referred to as the left child and the right child. Due to having a non-linear structure, a binary tree can be traversed in multiple ways. One such way is preorder traversal which is a De
3 min read
Postorder Tree Traversal in Binary Tree in C A binary tree is a hierarchical data structure in computer science. Each node in a binary tree can have at most two children: a left child and a right child. There are several ways to traverse a binary tree and in this article, we will learn about the postorder traversal of a binary tree in C. Examp
3 min read
Inorder Tree Traversal in Binary Tree in C A binary Tree is a hierarchical data structure in which each node has at most two children and it can referred to as the left child and right child. Due to being a non-linear data structure, different traversal techniques are possible for it. Inorder tree traversal is one of the techniques used to v
3 min read
Postorder Traversal of Binary Tree in C++ Post-order binary tree traversal is a type of depth-first traversal that follows the order of visiting the left subtree, then the right subtree, and finally the node itself. It is useful for operations where you need to work with a node only after you've dealt with its children, such as when calcula
7 min read
Binary Tree Level Order Traversal in C++ Trees are the type of data structure that stores the data in the form of nodes having a parent or children or both connected through edges. It leads to a hierarchical tree-like structure that can be traversed in multiple ways such as âpreorder, inorder, postorder, and level order. A binary tree is a
8 min read
Java Program for the Preorder Tree Traversal in Binary Tree Preorder traversal is the method used to traverse the tree data structure. In the preorder traversal, the nodes are visited in the order: of root, left subtree, and right subtree. It is called as "preorder" because the root node is visited before its children. This traversal technique is widely used
2 min read