0% found this document useful (0 votes)
6 views15 pages

expression tree using stack n tree

The document provides a C program that constructs an expression tree from a given infix expression and performs a non-recursive postorder traversal of the tree. It includes functions for creating tree nodes, checking operators, constructing the expression tree, and executing the postorder traversal. The program prompts the user for an infix expression and outputs the postorder traversal result.

Uploaded by

vinita sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

expression tree using stack n tree

The document provides a C program that constructs an expression tree from a given infix expression and performs a non-recursive postorder traversal of the tree. It includes functions for creating tree nodes, checking operators, constructing the expression tree, and executing the postorder traversal. The program prompts the user for an infix expression and outputs the postorder traversal result.

Uploaded by

vinita sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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;

You might also like