expression tree using stack n tree
expression tree using stack n tree
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
char data;
};
int isOperator(char c) {
newNode->data = data;
if (!isOperator(infixExpression[i])) {
root = createNode(infixExpression[i]);
stack[++top] = root;
} else {
// Operator, pop two operands from the stack, create a node, and push it
back
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
stack[++top] = operatorNode;
}
}
return stack[top];
if (root == NULL) {
return;
do {
while (root) {
if (root->right) {
stack[++top] = root->right;
stack[++top] = root;
// Set root as root's left child
root = root->left;
root = stack[top--];
// If the popped item has a right child and the right child is not processed yet,
root = root->right; // Change root so that the right child is processed next
} else {
int main() {
char infixExpression[100];
scanf("%s", infixExpression);
postOrderTraversal(root);
return 0;
Output---
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
char data;
};
int isOperator(char c) {
newNode->data = data;
if (!isOperator(infixExpression[i])) {
root = createNode(infixExpression[i]);
stack[++top] = root;
} else {
// Operator, pop two operands from the stack, create a node, and push it
back
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
stack[++top] = operatorNode;
}
}
return stack[top];
if (root == NULL) {
return;
do {
while (root) {
if (root->right) {
stack[++top] = root->right;
stack[++top] = root;
// Set root as root's left child
root = root->left;
root = stack[top--];
// If the popped item has a right child and the right child is not processed yet,
root = root->right; // Change root so that the right child is processed next
} else {
int main() {
char infixExpression[100];
scanf("%s", infixExpression);
postOrderTraversal(root);
return 0;
Output---
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a node in the expression tree
struct TreeNode {
char data;
};
int isOperator(char c) {
newNode->data = data;
return newNode;
if (!isOperator(infixExpression[i])) {
stack[++top] = createNode(infixExpression[i]);
} else {
// Operator, pop two operands from the stack, create a node, and push it
back
operatorNode->right = stack[top--];
operatorNode->left = stack[top--];
stack[++top] = operatorNode;
return stack[top];
int main() {
char infixExpression[100];
// Function to perform non-recursive postorder traversal of the expression tree
if (root == NULL) {
return;
do {
while (root) {
if (root->right) {
stack[++top] = root->right;
stack[++top] = root;
root = root->left;
root = stack[top--];
// If the popped item has a right child and the right child is not processed yet,
} else {
scanf("%s", infixExpression);
postOrderTraversal(root);
return 0;