Preorder in C++

Last updated: July 4, 2023

Preorder traversal is a tree traversal algorithm that visits the nodes of a binary tree in a pre-order fashion. This means that the root node is visited first, followed by the traversal of the left subtree, and then the traversal of the right subtree.

C++ Implementation of Preorder Traversal:


  #include <iostream>
  using namespace std;
  // Definition of a binary tree node
  struct TreeNode {
      int val;
      TreeNode* left;
      TreeNode* right;
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  };
  // Function to perform preorder traversal
  void preorderTraversal(TreeNode* root) {
      if (root == nullptr) {
          return;
      }
      cout << root->val << " ";            // Process current node
      preorderTraversal(root->left);       // Traverse left subtree
      preorderTraversal(root->right);      // Traverse right subtree
  }
  int main() {
      // Create the binary tree
      TreeNode* root = new TreeNode(5);
      root->left = new TreeNode(3);
      root->right = new TreeNode(8);
      root->left->left = new TreeNode(2);
      root->left->right = new TreeNode(4);
      root->right->left = new TreeNode(7);
      root->right->right = new TreeNode(9);
      // Perform preorder traversal
      cout << "Preorder Traversal: ";
      preorderTraversal(root);
      cout << endl;
      return 0;
  }
  

Output:

 Preorder Traversal: 5 3 2 4 8 7 9

 

Explanation:

1. We define a structure called TreeNode to represent a binary tree node. Each node contains an integer value (val) and pointers to its left and right child nodes (left and right). The constructor is used to initialize a node with a given value and set the left and right pointers to nullptr.

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

2. We define the preorderTraversal function, which performs the preorder traversal of a binary tree. It takes a pointer to the root node as a parameter.

void preorderTraversal(TreeNode* root) {
    if (root == nullptr) {
        return;
    }

    cout << root->val << " ";            // Process current node
    preorderTraversal(root->left);       // Traverse left subtree
    preorderTraversal(root->right);      // Traverse right subtree
}
 

In the preorderTraversal function, 

  • we first check if the root is nullptr. If so, we have reached the end of the current branch, and we return from the function. 
  • We process the current node by printing its value using cout << root->val << " ";
  • We then recursively call preorderTraversal on the left subtree using preorderTraversal(root->left);
  • After traversing the left subtree, we recursively call preorderTraversal on the right subtree using preorderTraversal(root->right);.

 

3. In the main function:

  • We create a binary tree by allocating memory for the nodes and setting their values.
  • We connect the nodes to form the tree structure by assigning appropriate left and right child nodes.
  • We call the preorderTraversal function, passing the root node as an argument, to perform the preorder traversal.
  • Finally, we print the result, which is the preorder traversal of the tree, by using cout << "Preorder Traversal: "; before calling preorderTraversal, and then cout << endl; to output a newline character.
int main() {
    // Create the binary tree
    TreeNode* root = new TreeNode(5);
    root->left = new TreeNode(3);
    root->right = new TreeNode(8);
    root->left->left = new TreeNode(2);
    root->left->right = new TreeNode(4);
    root->right->left = new TreeNode(7);
    root->right->right = new TreeNode(9);

    // Perform preorder traversal
    cout << "Preorder Traversal: ";
    preorderTraversal(root);
    cout << endl;

    return 0;
}

This updated explanation includes code snippets at relevant points, providing a clearer understanding of how the preorder traversal algorithm is implemented in the given C++ code.

Related post