Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program of Expression tree

Posted By: Easy Tutor     Category: C++ Programming     Views: 20319

Write a Program of Expression tree.

Code for Program of Expression tree in C++ Programming

#include <iostream.h>
#include <conio.h>

struct node{
    char data;
    node *left;
    node *right;
};

char postfix[35];
int top=-1;
node *arr[35];


int  r(char inputchar){ //for checking symbol is operand or operatorif(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/')
      return(-1);
     elseif(inputchar>='a' || inputchar<='z')
      return(1);
     elseif(inputchar>='A' || inputchar<='Z')
      return(1);
     elsereturn(-99);  //for error
}

//it is used for inseting an single element in//a tree, i.e. is pushing of single element.void push(node *tree){
    top++;
    arr[top]=tree;
}

node *pop(){
    top--;
    return(arr[top+1]);
}


void create_expr_tree(char *suffix){
    char symbol;
    node *newl,*ptr1,*ptr2;
    int flag; //flag=-1 when operator and flag=1 when operand
    symbol = suffix[0]; //Read the first symbol from the postfix expr.for(int i=1;symbol!=NULL;i++){ //continue till end of the expr.
        flag=r(symbol); //check symbol is operand or operator.if(flag == 1)//if symbol is operand.
        {
            newl = new node;
            newl->data = symbol;
            newl->left = NULL;
            newl->right = NULL;
            push(newl);
        }
        else{    //If the symbol is operator//pop two top elements.
            ptr1=pop();
            ptr2=pop();
            newl = new node;
            newl->data = symbol;
            newl->left = ptr2;
            newl->right = ptr1;
            push(newl);
        }
        symbol=suffix[i];
    }
}

void preOrder(node *tree){
    if( tree!=NULL){
        cout<<tree->data;
        preOrder(tree->left);
        preOrder(tree->right);
    }
}

void inOrder(node *tree){
    if( tree!=NULL){
        inOrder( tree->left);
        cout<< tree->data;
        inOrder(tree->right);
    }
}


void postOrder(node *tree){
    if( tree!=NULL){
        postOrder( tree->left);
        postOrder( tree->right);
        cout<<tree->data;
    }
}



void main(){
    clrscr();
    cout<<"*****Expression Tree*****\n";
    cout<<"Enter Postfix Expression : ";
    cin>>postfix;

    //Creation of Expression Tree
    create_expr_tree(postfix);

    //Traversal Operation on expression tree
    cout<<"\nIn-Order Traversal :   ";
    inOrder(arr[0]);

    cout<<"\nPre-Order Traversal :  ";
    preOrder(arr[0]);

    cout<<"\nPost-Order Traversal : ";
    postOrder(arr[0]);
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program of Expression tree Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of Expression tree is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Cullen Miller from United States Comment on: Apr 07
Errors in this code.

View All Comments