Preorder Tree Traversal in Binary Tree In C++

Preorder Tree Traversal

Preorder Tree Traversal In Binary Tree in C++ – When working with binary trees, one of the most important tasks is to visit or traverse each node in a specific order. One such method is called Preorder Traversal. In this type of traversal, we follow the order: Root → Left → Right. This means we first visit the root node, then move to the left subtree, and finally to the right subtree.

Preorder traversal is especially useful when we want to create a copy of the tree or display its structure. Preorder traversal is a depth first algorithm. There are three types of traversals in tree: Preorder, Inorder, Postorder. In this article, preorder tree traversal is performed using recursion.

Preorder Tree Traversal in binary tree in C++

Preorder Tree Traversal in Binary Tree In C++

Preorder traversal is a method of visiting nodes in a binary tree following the Root → Left → Right sequence:

  • First, the root node is visited, then the left subtree is explored recursively, and finally the right subtree is traversed in the same way.

More About Preorder Traversal

  1. We first print the value of the node,them move to the left subtree and finally to the right subtree.
  2. If we need to explore more about the node itself before inspecting its leaves, we use preorder traversal.
  3. Preorder traversal is used to find the prefix expression of an expression tree.
Preorder Tree Traversal in binary tree in C++
Preorder Tree Traversal in binary tree in C++

Steps To Find Preorder Traversal:

  1. Print the root node and traverse the left subtree.
  2. After printing 12, move to the left.
  3. 8 is printed and we move to the left subtree.
  4. Print 5 and move back,
  5. Since there is only 1 right child 13 is printed and we move to the right subtree.
  6. 54 and 18 are printed.

Algorithm To Find Preorder Traversal:

  1. If root is NULL, return NULL.
  2. Print the node.
  3. Visit left subtree.
  4. Visit right subtree.

Code Implementation for Preorder Tree traversal

Run
#include<bits/stdc++.h>
using namespace std;
class Tree
{
public:
  int data;
  Tree *left = NULL, *right = NULL;
  // Constructor initialised
    Tree (int x)
  {
    data = x;
    left = NULL;
    right = NULL;
  }
};
void preorder_traversal (Tree * root)
{
  if (root == NULL)
    return;
  // Print the data
  cout << root->data << " ";
  // Visit Left subtree
  preorder_traversal (root->left);
  // Visit right subtree
  preorder_traversal (root->right);
}

int main ()
{
  Tree *root = new Tree (15);
  root->left = new Tree (12);
  root->right = new Tree (54);
  root->left->left = new Tree (8);
  root->left->right = new Tree (13);
  root->left->left->left = new Tree (5);
  root->right->left = new Tree (18);
  preorder_traversal (root);
  return 0;
}

Output:

15 12 8 5 13 54 18

Conclusion

Preorder traversal is a simple and useful way to go through a binary tree. It visits the root first, then the left side, and finally the right side. This method is helpful when you want to print the structure of the tree or create a copy of it. Both recursive and iterative approaches are easy to learn with some practice.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

FAQs - Preorder Tree Traversal in Binary Tree In C++

FAQs - Preorder Tree Traversal in Binary Tree In C++

No, preorder traversal alone is not enough to reconstruct a binary tree uniquely. To fully reconstruct the tree, you need preorder along with either inorder or postorder traversal. This is because preorder only shows the order of visiting, not the exact structure of left and right subtrees.

  • Recursive approach uses the function call stack, so its space complexity is O(h), where h is the height of the tree.
  • Iterative approach uses an explicit stack and also takes O(h) space. In a skewed tree, this could go up to O(n) in the worst case.

Preorder traversal is commonly used for:

  • Cloning or copying a tree
  • Serializing tree data into a file or string
  • Evaluating or printing prefix expressions from expression trees
  • Generating structured outputs like XML or JSON from hierarchical data

In the iterative method, a stack is used to simulate recursion:

  • Start by pushing the root node onto the stack.
  • While the stack is not empty:
    • Pop a node, process it (print/store value),
    • Push its right child (if any),
    • Then push its left child (if any).
  • We push right before left because stacks are LIFO, and we want to process the left child first.

Yes, using Morris Traversal, it’s possible to perform preorder traversal in O(1) space, without recursion or a stack. However, this method temporarily modifies the tree structure by creating threaded links and is more complex to implement.

Preorder traversal works on any binary tree, including binary heaps or complete binary trees. However, in heaps, level-order traversal is more relevant since heaps are typically represented as arrays, and Preorder does not maintain the heap structure when traversing.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java