Given a character array a[] representing a prefix expression. The task is to build an Expression Tree for the expression and then print the infix and postfix expression of the built tree.
Examples:
Input: a[] = “*+ab-cd”
Output: The Infix expression is:
a + b * c – d
The Postfix expression is:
a b + c d – *Input: a[] = “+ab”
Output: The Infix expression is:
a + b
The Postfix expression is:
a b +
Approach: If the character is an operand i.e. X then it’ll be the leaf node of the required tree as all the operands are at the leaf in an expression tree. Else if the character is an operator and of the form OP X Y then it’ll be an internal node with left child as the expressionTree(X) and right child as the expressionTree(Y) which can be solved using a recursive function.
Below is the implementation of the above approach:
// C program to construct an expression tree // from prefix expression #include <stdio.h> #include <stdlib.h> // Represents a node of the required tree typedef struct node { char data; struct node *left, *right; } node; // Function to recursively build the expression tree char* add(node** p, char* a) { // If its the end of the expression if (*a == '\0') return '\0'; while (1) { char* q = "null"; if (*p == NULL) { // Create a node with *a as the data and // both the children set to null node* nn = (node*)malloc(sizeof(node)); nn->data = *a; nn->left = NULL; nn->right = NULL; *p = nn; } else { // If the character is an operand if (*a >= 'a' && *a <= 'z') { return a; } // Build the left sub-tree q = add(&(*p)->left, a + 1); // Build the right sub-tree q = add(&(*p)->right, q + 1); return q; } } } // Function to print the infix expression for the tree void inr(node* p) // recursion { if (p == NULL) { return; } else { inr(p->left); printf("%c ", p->data); inr(p->right); } } // Function to print the postfix expression for the tree void postr(node* p) { if (p == NULL) { return; } else { postr(p->left); postr(p->right); printf("%c ", p->data); } } // Driver code int main() { node* s = NULL; char a[] = "*+ab-cd"; add(&s;, a); printf("The Infix expression is:\n "); inr(s); printf("\n"); printf("The Postfix expression is:\n "); postr(s); return 0; } |
The Infix expression is: a + b * c - d The Postfix expression is: a b + c d - *
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Building an undirected graph and finding shortest path using Dictionaries in Python
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Expression Tree
- Evaluation of Expression Tree
- Convert Ternary Expression to a Binary Tree
- Program to convert Infix notation to Expression Tree
- Convert ternary expression to Binary Tree using Stack
- Convert Infix To Prefix Notation
- Evaluation of Prefix Expressions
- Postfix to Prefix Conversion
- Prefix to Postfix Conversion
- Prefix to Infix Conversion
- Infix to Prefix conversion using two stacks
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Convert a Binary Tree into its Mirror Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a binary tree is subtree of another binary tree | Set 1
- Convert a given tree to its Sum Tree
- Binary Tree to Binary Search Tree Conversion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

