Inorder Binary Tree Traversal in C

Inorder traversal is a DFS traversal technique where we try to traverse as deep as possible in the tree from the current node. In the inorder traversal, we first visit all the left subtree, then visit the current node and at last, we visit the right subtree.

Inorder Traversal

  1. Visit the left subtree.
  2. Visit the root node.
  3. Visit the right subtree.

Consider the following example,

Here’s the step-by-step explanation of the inorder traversal:

  1. Start at the root node (1).
  2. Visit the left subtree of the root (2).
  3. Visit the left subtree of 2(4) then 4 does not have any children so print it.
  4. Backtrack to the node (2) and print it.
  5. Move to the right subtree of the node 2 (5). Print it.
  6. Backtrack to the root node (1).
  7. Move to the right subtree of the node 1 (3).
  8. Visit the left subtree of 3 and there is no nodes.
  9. Visit the node (3) and print it.

The in-order traversal of the tree is: 4, 2, 5, 1, 3

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 visit each node of the binary tree. In this article, we will learn how to implement the in-order traversal technique for binary tree traversal in C. We will also discuss its time and space complexities.

Similar Reads

Inorder Binary Tree Traversal in C

Inorder traversal is a DFS traversal technique where we try to traverse as deep as possible in the tree from the current node. In the inorder traversal, we first visit all the left subtree, then visit the current node and at last, we visit the right subtree....

C Program to Implement Inorder Binary Tree Traversal

C // C program to show how to implement the binary tree // traversal #include #include // node of the tree struct Node { int data; struct Node* left; struct Node* right; }; // utility function to create a node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } // Function to perform inorder traversal void inorderTraversal(struct Node* root) { // cheking if the current node is NULL if (!root) return; // traversing left subtree inorderTraversal(root->left); // traversing current node printf("%d ", root->data); // traversing right subtree inorderTraversal(root->right); } int main() { // Example tree creation struct Node* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); printf("Inorder Traversal: "); inorderTraversal(root); return 0; }...

Applications of Inorder Traversal of Binary Tree

Expression Trees: Binary trees can be used to the represent the expressions in the way that facilitates the evolution using the inorder traversal.Binary Search Trees(BSTs): Inorder traversal of the BST yields elements in the sorted order, making it is useful for the searching and sorting operations.Complier Design: Inorder traversal is used in the parsing and evaluating the mathematical expressions....

Contact Us