Escolar Documentos
Profissional Documentos
Cultura Documentos
h>
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; }
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;
void inOrder(node *tree){ if( tree!=NULL){ inOrder( tree->left); cout<< tree->data; inOrder(tree->right); } }
void main(){ clrscr(); cout<<"*****Expression Tree*****\n"; cout<<"Enter Postfix Expression : "; cin>>postfix;
getch(); }