Program— For given expression eg.
a-b*c-d/e+f construct inorder sequence and
traverse it using postorder traversal(non recursive).
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the expression tree
struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
};
// Function to check if a character is an operator
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
// Function to create a new TreeNode
struct TreeNode *createNode(char data) {
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct
TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
// Function to construct the expression tree
struct TreeNode *constructExpressionTree(char *infixExpression) {
struct TreeNode *stack[100]; // Assuming a maximum of 100 nodes
int top = -1;
struct TreeNode *root = NULL;
for (int i = 0; infixExpression[i] != '\0'; i++) {
if (!isOperator(infixExpression[i])) {
// Operand, create a node and push onto the stack
root = createNode(infixExpression[i]);
stack[++top] = root;
} else {
// Operator, pop two operands from the stack, create a node, and push it
back
struct TreeNode *operatorNode = createNode(infixExpression[i]);
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
stack[++top] = operatorNode;
}
}
// The final result is on the top of the stack
return stack[top];
// Function to perform non-recursive postorder traversal of the expression tree
void postOrderTraversal(struct TreeNode *root) {
if (root == NULL) {
return;
struct TreeNode *stack[100]; // Assuming a maximum of 100 nodes
int top = -1;
do {
while (root) {
// Push root's right child and then root to the stack
if (root->right) {
stack[++top] = root->right;
stack[++top] = root;
// Set root as root's left child
root = root->left;
// Pop an item from the stack
root = stack[top--];
// If the popped item has a right child and the right child is not processed yet,
// then make sure right child is processed before root
if (root->right && stack[top] == root->right) {
stack[top--]; // Remove the right child from the stack
stack[++top] = root; // Push root back to stack
root = root->right; // Change root so that the right child is processed next
} else {
// Print root's data
printf("%c ", root->data);
root = NULL; // Set root to NULL after processing
} while (top >= 0);
int main() {
char infixExpression[100];
// Input infix expression
printf("Enter the infix expression: ");
scanf("%s", infixExpression);
// Construct the expression tree
struct TreeNode *root = constructExpressionTree(infixExpression);
// Perform postorder traversal
printf("Postorder traversal: ");
postOrderTraversal(root);
return 0;
Output---
Enter the infix expression: a-b*c-d/e+f
Postorder traversal: abc*de/f+-
Program— For given expression eg. a-b*c-d/e+f construct inorder sequence and
traverse it using postorder traversal(non recursive).
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the expression tree
struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
};
// Function to check if a character is an operator
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
// Function to create a new TreeNode
struct TreeNode *createNode(char data) {
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct
TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
// Function to construct the expression tree
struct TreeNode *constructExpressionTree(char *infixExpression) {
struct TreeNode *stack[100]; // Assuming a maximum of 100 nodes
int top = -1;
struct TreeNode *root = NULL;
for (int i = 0; infixExpression[i] != '\0'; i++) {
if (!isOperator(infixExpression[i])) {
// Operand, create a node and push onto the stack
root = createNode(infixExpression[i]);
stack[++top] = root;
} else {
// Operator, pop two operands from the stack, create a node, and push it
back
struct TreeNode *operatorNode = createNode(infixExpression[i]);
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
stack[++top] = operatorNode;
}
}
// The final result is on the top of the stack
return stack[top];
// Function to perform non-recursive postorder traversal of the expression tree
void postOrderTraversal(struct TreeNode *root) {
if (root == NULL) {
return;
struct TreeNode *stack[100]; // Assuming a maximum of 100 nodes
int top = -1;
do {
while (root) {
// Push root's right child and then root to the stack
if (root->right) {
stack[++top] = root->right;
stack[++top] = root;
// Set root as root's left child
root = root->left;
// Pop an item from the stack
root = stack[top--];
// If the popped item has a right child and the right child is not processed yet,
// then make sure right child is processed before root
if (root->right && stack[top] == root->right) {
stack[top--]; // Remove the right child from the stack
stack[++top] = root; // Push root back to stack
root = root->right; // Change root so that the right child is processed next
} else {
// Print root's data
printf("%c ", root->data);
root = NULL; // Set root to NULL after processing
} while (top >= 0);
int main() {
char infixExpression[100];
// Input infix expression
printf("Enter the infix expression: ");
scanf("%s", infixExpression);
// Construct the expression tree
struct TreeNode *root = constructExpressionTree(infixExpression);
// Perform postorder traversal
printf("Postorder traversal: ");
postOrderTraversal(root);
return 0;
Output---
Enter the infix expression: a-b*c-d/e+f
Postorder traversal: abc*de/f+-
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the expression tree
struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
};
// Function to check if a character is an operator
int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
// Function to create a new TreeNode
struct TreeNode *createNode(char data) {
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct
TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
// Function to construct the expression tree
struct TreeNode *constructExpressionTree(char *infixExpression) {
struct TreeNode *stack[100]; // Assuming a maximum of 100 nodes
int top = -1;
for (int i = 0; infixExpression[i] != '\0'; i++) {
if (!isOperator(infixExpression[i])) {
// Operand, create a node and push onto the stack
stack[++top] = createNode(infixExpression[i]);
} else {
// Operator, pop two operands from the stack, create a node, and push it
back
struct TreeNode *operatorNode = createNode(infixExpression[i]);
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
stack[++top] = operatorNode;
// The final result is on the top of the stack
return stack[top];
int main() {
char infixExpression[100];
// Function to perform non-recursive postorder traversal of the expression tree
void postOrderTraversal(struct TreeNode *root) {
if (root == NULL) {
return;
struct TreeNode *stack[100]; // Assuming a maximum of 100 nodes
int top = -1;
do {
while (root) {
// Push root's right child and then root to the stack
if (root->right) {
stack[++top] = root->right;
stack[++top] = root;
// Set root as root's left child
root = root->left;
// Pop an item from the stack
root = stack[top--];
// If the popped item has a right child and the right child is not processed yet,
// then make sure the right child is processed before root
if (root->right && stack[top] == root->right) {
top--; // Remove the right child from the stack
} else {
// Print root's data
printf("%c ", root->data);
root = NULL; // Set root to NULL after processing
} while (top >= 0);
// Input infix expression
printf("Enter the infix expression: ");
scanf("%s", infixExpression);
// Construct the expression tree
struct TreeNode *root = constructExpressionTree(infixExpression);
// Perform postorder traversal
printf("Postorder traversal: ");
postOrderTraversal(root);
return 0;