Inorder Postorder Preorder Tree Traversals in Binary Tree
Inorder Postorder Preorder Tree Traversals In C
There are three most popular ways in which we can traverse a binary tree which are –- Inorder
- Postorder
- PreOrder
How each of the Postorder, Inorder, Preorder work?
Inorder
- Traverse the left sub-tree, (recursively call inorder(root -> left).
- Visit and print the root node.
- Traverse the right sub-tree, (recursively call inorder(root -> right).
Tips to remember –
- Direction : Clockwise direction
- Rule : LCR i.e. Left Center(root) Right
Preorder
- Visit and print the root node.
- Traverse the left sub-tree, (recursively call Preorder(root -> left).
- Traverse the right sub-tree, (recursively call Preorder(root -> right)
Tips to remember –
- Direction : Anti-Clockwise direction
- Rule : CLR i.e. Center(root) left Right
Postorder
- Traverse the left sub-tree, (recursively call Postorder(root -> left).
- Traverse the right sub-tree, (recursively call Postorder(root -> right).
- Visit and print the root node
Tips to remember –
- Direction : Anti-Clockwise direction
- Rule : LRC i.e. Left Right Center(root)
Code for Inorder PostOrder PreOrder in Binary Tree
Run
// Program for tree traversal inorder, postorder, preorder in Binary Tree
#include<stdio.h>
#include<stdlib.h>
// We are creating struct for the binary tree below
struct node
{
int data;
struct node *left, *right;
};
// newNode function for initialisation of the newly created node
struct node *newNode (int item)
{
struct node *temporary = (struct node *) malloc (sizeof (struct node));
temporary->data = item;
temporary->left = temporary->right = NULL;
return temporary;
}
// Here we print the postorder recursively
void postorder (struct node *root)
{
if (root != NULL)
{
postorder (root->left);
postorder (root->right);
printf ("%d ", root->data);
}
}
// Here we print the preorder recursively
void preorder (struct node *root)
{
if (root != NULL)
{
printf ("%d ", root->data);
preorder (root->left);
preorder (root->right);
}
}
// Here we print the inorder recursively
void inorder (struct node *root)
{
if (root != NULL)
{
inorder (root->left);
printf ("%d ", root->data);
inorder (root->right);
}
}
// Basic Program to insert new node at the correct position in BST
struct node *insert (struct node *node, int data)
{
/* When there no node in the tree(subtree) then create
and return new node using newNode function */
if (node == NULL)
return newNode (data);
/* If not then we recur down the tree to find correct position for insertion */
if (data < node->data)
node->left = insert (node->left, data);
else if (data > node->data)
node->right = insert (node->right, data);
return node;
}
int main ()
{
/* What our binary search tree looks like really
9
/ \
7 14
/ \ / \
5 8 11 16 */
struct node *root = NULL;
root = insert (root, 9);
insert (root, 7);
insert (root, 5);
insert (root, 8);
insert (root, 14);
insert (root, 11);
insert (root, 16);
printf ("The postorder is :\n");
postorder (root);
printf ("\nThe preorder is :\n");
preorder (root);
printf ("\nThe inorder is :\n");
inorder (root);
return 0;
}
Output:
The postorder is : 5 8 7 11 16 14 9 The preorder is : 9 7 5 8 14 11 16 The inorder is : 5 7 8 9 11 14 16
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java

Login/Signup to comment