×

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

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Lowest common ancestor in a binary search tree

Suppose you have given two values of nodes in a binary search tree. You have to find out the lowest common ancestor between the nodes. Let’s take an example tree- For the...

4 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Preorder Traversal of Binary Trees

In general, Stack, Array, Queue, and other linear data structures only have one way to traverse the data. However, there are numerous ways to traverse through the data in a hierarchical...

3 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 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.

What is B tree?

What do you mean by B Tree in Data Structures? In the technological world, a B tree is simply a well-managed and coordinated tree and an integral part of the data...

6 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.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Bookshop management system using file handling in C++

We see different software in every hospitals or library to manage their database. It is very important to store organization’s data. So we use this software. Now we are going...

5 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such...

4 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

Digital Search Tree in Data Structures

What is a digital search Tree in Data Structures? The Digital search tree is known for its application and diversity in the way it has impacted our world in the field...

3 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.