×

Post-order traversal in a binary tree

We all know that postorder is a form of tree traversal to visit the tree's nodes, and it helps us reach out to the tree's nodes. Postorder means visiting the lft and rt subtree recursively in a traversal operation. The postorder traversal means visiting the nodes of the trees, which implies the lft subtree firstly and then the rt subtree in the postman-order and then reaching the root node. In simple words, it follows the lft-rt-root mechanism. This type of traversal helps us get the other expression of the tree.

Algorithm

In this section, we will see the steps and understand the working of postorder traversal.

  1. Step 1: Repeat Steps 2 to 4 while TREE != NILL  
  2. Step 2: POSTORDER(TREE -> LFT)  
  3. Step 3: POSTORDER(TREE -> RT)  
  4. Step 4: Write TREE -> RECORD  
  5. [END OF LOOP]  
  6. Step 5: END  

Implementation

In this article section, we will see the implementation of the postorder traversal operation in the Binary tree and understand its working.

Example1)

#include <iostream>
using namespace std;


/* Given there is a B tree that has info, a ptr to the left node and ptr to the right node*/
struct Node {
	int record;
	struct Node *lft, *rt;
};


//Utility function to create a new tree node
Node* newNod(int record)
{
	Node* temp = new Nod;
	temp->record = record;
	temp->lft = temp->rt = NILL;
	return temp;
}


/* In a binary tree, we have to print all the nodes that were present in the binary tree. */
void printPO(struct Node* node)
{
	if (node == NILL)
		return;


	// first recursion found on the left sub-tree
	printPO(node->lft);


	// then recursion found on the left sub-tree
	printPO(node->rt);


	//Countering the nodes present in the tree now
	cout << node->record << " ";
}


/* In a binary tree, we have to print all the nodes that were present in the binary tree. */
void printIO(struct Node* nod)
{
	if (node == NILL)
		return;


	// first recursion found on the left sub-tree


	printIO(node->lft);
/* In a binary tree, we have to print all the nodes that were present in the binary tree. */
	cout << node->record << " ";


	// then recursion found on the rigth sub-tree
	printIO(node->rt);
}


/* In a binary tree we have to print all the nodes that were present in the binary tree. */
void printPO(struct Node* nod)
{
	if (node == NILL)
		return;


	/* first print record of node */
	cout << node->record << " ";


	/* then recur on lft subtree */
	printPO(node->lft);


	/* now recur on rt subtree */
	printPO(node->rt);
}


/* Driver program to test above functions*/
int main()
{
	struct Node* root = newNod(1);
	root->lft = newNod(2);
	root->rt = newNod(3);
	root->lft->lft = newNod(4);
	root->lft->rt = newNod(5);


	cout << "\nPreorder traversal of binary tree is \n";
	printPO(root);


	cout << "\nInorder traversal of binary tree is \n";
	printIO(root);


	cout << "\nPostorder traversal of binary tree is \n";
	printPO(root);


	return 0;
}

Output:

Post-order traversal in a binary tree

Example2)

#include <stdio.h>  
#include <stdlib.h>  
  
struct node {  
s  
    struct node* lft;  
    struct node* rt;  
};  
  
/*Constructing a new node*/  
struct node* createNode(int record)  
{  
    struct node* Node = (struct node*)malloc(sizeof(struct node));  
    Node->elem = record;  
    Node->lft = NILL;  
    Node->rt = NILL;  
  
    return (Node);  
}  
  
  
/*Visiting the nodes of the binary tree in the postorder form*/  
void traversePO(struct node* root)  
{  
    if (root == NILL)  
        return;  
    traversePO(root->lft);  
    traversePO(root->rt);  
    printf(" %d ", root->elem);  
}  
  
int main()  
{  
    struct node* root = createNode(40);  
    root->lft = createNode(30);  
    root->rt = createNode(50);  
    root->lft->lft = createNode(25);  
    root->lft->rt = createNode(35);  
    root->lft->lft->lft = createNode(15);  
    root->lft->lft->rt = createNode(28);  
    root->rt->lft = createNode(45);  
    root->rt->rt = createNode(60);  
    root->rt->rt->lft = createNode(55);  
    root->rt->rt->rt = createNode(70);  
      
    printf("\n The Postorder traversal of given binary tree is -\n");  
    traversePO(root);  
    return 0;  
}  

Output:

Post-order traversal in a binary tree

Example3)

#include <iostream>  
  
using namespace std;  
  
struct node {  
    int elem;  
    struct node* lft;  
    struct node* rt;  
};  
  
/*Constructing a new node*/  
struct node* createNode(int record)  
{  
    struct node* Node = (struct node*)malloc(sizeof(struct node));  
    Node->elem = record;  
    Node->lft = NILL;  
    Node->rt = NILL;  
  
    return (Node);  
}  
  
  
/*Visiting the nodes of the binary tree in the postorder form*/  
void traversePO(struct node* root)  
{  
    if (root == NILL)  
        return;  
    traversePO(root->lft);  
    traversePO(root->rt);  
    cout<<" "<<root->elem<<" ";  
}  
  
int main()  
{  
    struct node* root = createNode(38);  
    root->lft = createNode(28);  
    root->rt = createNode(48);  
    root->lft->lft = createNode(23);  
    root->lft->rt = createNode(33);  
    root->lft->lft->lft = createNode(13);  
    root->lft->lft->rt = createNode(26);  
    root->rt->lft = createNode(43);  
    root->rt->rt = createNode(58);  
    root->rt->rt->lft = createNode(53);  
    root->rt->rt->rt = createNode(68);  
      
    cout<<"\n The Postorder traversal of given binary tree is -\n";  
    traversePO(root);  
    return 0;  
}  

Output:

Post-order traversal in a binary tree

Related Topics

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Difference between Structured and Object-Oriented Analysis

Analysis means observing and collecting relevant information about the structure of something or the basic details of a system's requirements. Structured and Object Oriented Analysis are both widely used in...

2 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

Bottom view of the binary tree

The bottom of the binary tree is generally defined as the number of nods present in the bottom-most part of the tree. In this article, we will see the implementation...

3 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 minutes read.

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Bitonical Sort

Arranging an unordered collecttion of things into asignificant order. •Comparision Based Model: Bubble Sort, Selection Sort -->Non-Comparison Based. Model: Bucket Sort or on the other hand a Count Sort Bitonic Sort: Bitonic sort Algorithm was made...

5 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.