Deign an Expression Tree Using Design Patterns
The expression tree is a binary tree in which each internal node corresponds to the operator and each leaf node corresponds to the operand so for example expression tree for 3 + ((5+9)*2) would be:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Inorder traversal of expression tree produces infix version of given postfix expression (same with postorder traversal it gives postfix expression)
Evaluating the expression represented by an expression tree:
Let t be the expression tree If t is not null then If t.value is operand then Return t.value A = solve(t.left) B = solve(t.right) // calculate applies operator 't.value' // on A and B, and returns value Return calculate(A, B, t.value)
Construction of Expression Tree:
Now For constructing an expression tree we use a stack. We loop through input expression and do the following for every character.
- If a character is an operand push that into the stack
- If a character is an operator pop two values from the stack make them its child and push the current node again.
In the end, the only element of the stack will be the root of an expression tree.
Examples:
Input: A B C*+ D/ Output: A + B * C / D
The first three symbols are operands, so create tree nodes and push pointers to them onto a stack as shown below.
In the Next step, an operator '*' will going read, so two pointers to trees are popped, a new tree is formed and a pointer to it is pushed onto the stack
In the Next step, an operator '+' will read, so two pointers to trees are popped, a new tree is formed and a pointer to it is pushed onto the stack.
A3f.png
Similarly, as above cases first we push 'D' into the stack and then in last step first , will read '/' and then as previous step top most element will pop out and then will be right sub tree of root '/' and other node will be right subtree.
Final Constructed Expression Tree is:
A4f.png
Below is the code of the above approach:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class node {
public :
char value;
node* left;
node* right;
node* next = NULL;
node( char c)
{
this ->value = c;
left = NULL;
right = NULL;
}
node()
{
left = NULL;
right = NULL;
}
friend class Stack;
friend class expression_tree;
};
class Stack {
node* head = NULL;
public :
void push(node*);
node* pop();
friend class expression_tree;
};
class expression_tree {
public :
void inorder(node* x)
{
if (x == NULL)
return ;
else {
inorder(x->left);
cout << x->value << " " ;
inorder(x->right);
}
}
};
void Stack::push(node* x)
{
if (head == NULL) {
head = x;
}
else {
x->next = head;
head = x;
}
}
node* Stack::pop()
{
node* p = head;
head = head->next;
return p;
}
int main()
{
string s = "ABC*+D/" ;
Stack e;
expression_tree a;
node *x, *y, *z;
int l = s.length();
for ( int i = 0; i < l; i++) {
if (s[i] == '+' || s[i] == '-' || s[i] == '*'
|| s[i] == '/' || s[i] == '^' ) {
z = new node(s[i]);
x = e.pop();
y = e.pop();
z->left = y;
z->right = x;
e.push(z);
}
else {
z = new node(s[i]);
e.push(z);
}
}
cout << " The Inorder Traversal of Expression Tree: " ;
a.inorder(z);
return 0;
}
Java
import java.util.Stack;
class Node{
char data;
Node left,right;
public Node( char data){
this .data = data;
left = right = null ;
}
}
public class Main {
public static boolean isOperator( char ch){
if (ch== '+' || ch== '-' || ch== '*' || ch== '/' || ch== '^' ){
return true ;
}
return false ;
}
public static Node expressionTree(String postfix){
Stack<Node> st = new Stack<Node>();
Node t1,t2,temp;
for ( int i= 0 ;i<postfix.length();i++){
if (!isOperator(postfix.charAt(i))){
temp = new Node(postfix.charAt(i));
st.push(temp);
}
else {
temp = new Node(postfix.charAt(i));
t1 = st.pop();
t2 = st.pop();
temp.left = t1;
temp.right = t2;
st.push(temp);
}
}
temp = st.pop();
return temp;
}
public static void inorder(Node root){
if (root== null ) return ;
inorder(root.left);
System.out.print(root.data);
inorder(root.right);
}
public static void main(String[] args) {
String postfix = "ABC*+D/" ;
Node r = expressionTree(postfix);
inorder(r);
}
}
C
#include <stdio.h>
#include <stdlib.h>
struct node {
char data;
struct node* left;
struct node* right;
struct node* next;
};
struct node *head=NULL;
struct node* newNode( char data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
node->next = NULL;
return (node);
}
void printInorder( struct node* node)
{
if (node == NULL)
return ;
else {
printInorder(node->left);
printf ( "%c " , node->data);
printInorder(node->right);
}
}
void push( struct node* x)
{
if (head==NULL)
head = x;
else
{
(x)->next = head;
head = x;
}
}
struct node* pop()
{
struct node* p = head;
head = head->next;
return p;
}
int main()
{
char s[] = { 'A' , 'B' , 'C' , '*' , '+' , 'D' , '/' };
int l = sizeof (s) / sizeof (s[0]) ;
struct node *x, *y, *z;
for ( int i = 0; i < l; i++) {
if (s[i] == '+' || s[i] == '-' || s[i] == '*'
|| s[i] == '/' || s[i] == '^' ) {
z = newNode(s[i]);
x = pop();
y = pop();
z->left = y;
z->right = x;
push(z);
}
else {
z = newNode(s[i]);
push(z);
}
}
printf ( " The Inorder Traversal of Expression Tree: " );
printInorder(z);
return 0;
}
Output:
A+B*C/D
Deign an Expression Tree Using Design Patterns
Source: https://www.geeksforgeeks.org/expression-tree/
0 Response to "Deign an Expression Tree Using Design Patterns"
Post a Comment