Created
May 4, 2016 03:58
-
-
Save mfrazi/1cf59ab0af2d773113ba0f5a77c928a6 to your computer and use it in GitHub Desktop.
Expression Tree - Infix to Postfix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct node { | |
int data; | |
struct node *left, | |
*right, | |
*parent; | |
}node_t; | |
void insertTreeNode(node_t **node, int input){ | |
if(*node == NULL){ | |
*node = (node_t*)malloc(sizeof(node_t)); | |
(*node)->data = input; | |
(*node)->left = (*node)->right = (*node)->parent = NULL; | |
} | |
else{ | |
if(input <= (*node)->data){ | |
insertTreeNode(&((*node)->left), input); | |
(*node)->left->parent = (*node); | |
} | |
else{ | |
insertTreeNode(&((*node)->right), input); | |
(*node)->right->parent = (*node); | |
} | |
} | |
} | |
void printTreepostOrder(node_t *node, char *data){ | |
if(node == NULL) return; | |
printTreepostOrder(node->left, data); | |
printTreepostOrder(node->right, data); | |
printf("%c ", data[node->data]); | |
} | |
bool isOperator(char a){ | |
if(a=='+' or a=='-' or a=='*' or a=='/' or a=='^' or a=='(' or a==')') | |
return true; | |
return false; | |
} | |
int valueOperator(char a){ | |
if(a=='+' or a=='-') return 1; | |
if(a=='*' or a=='/') return 2; | |
if(a=='^') return 3; | |
if(a=='(') return -4; | |
if(a==')') return 4; | |
} | |
int main(){ | |
node_t *root = NULL; | |
char infix[1000]; | |
char v_operator[1000], v_operand[1000]; | |
int p_operator[1000], p_operand[1000]; | |
for(int i=0; i<1000; i++){ | |
v_operand[i]=v_operator[i]='\0'; | |
} | |
gets(infix); | |
int cnttor=0, cntand=0; | |
for(int i=0; i<strlen(infix); i++){ | |
if(isOperator(infix[i])){ | |
v_operator[cnttor] = infix[i]; | |
p_operator[cnttor++] = i; | |
} | |
else{ | |
v_operand[cntand] = infix[i]; | |
p_operand[cntand++] = i; | |
} | |
} | |
bool flag[strlen(v_operator)]; | |
for(int i=0; i<strlen(v_operator); i++){ | |
flag[i]=false; | |
} | |
// puts(v_operand); | |
// printf("%d ", strlen(v_operator)); puts(v_operator); | |
int bracket=0; | |
while(true){ | |
int lowest = 99999, index=-1, real_index, tmp; | |
for(int i=strlen(v_operator)-1; i>=0; i--){ | |
tmp = valueOperator(v_operator[i]); | |
if(tmp==4 or tmp==-4) | |
bracket += tmp; | |
if(tmp+bracket < lowest and flag[i]==false and v_operator[i]!='(' and v_operator[i]!=')'){ | |
// printf("v = %c %d\n", v_operator[i], tmp+bracket); | |
lowest = tmp + bracket; | |
if(index!=-1) | |
flag[index] = false; | |
index = i; | |
real_index = p_operator[i]; | |
flag[i] = true; | |
} | |
} | |
// printf("index = %d %d\n", index, real_index); | |
insertTreeNode(&root, real_index); | |
int count = 0; | |
for(int i=0; i<strlen(v_operator); i++) | |
if(v_operator[i]=='(' or v_operator[i]==')' or flag[i]==true) count++; | |
if(count==strlen(v_operator)) break; | |
} | |
for(int i=0; i<strlen(v_operand); i++){ | |
// printf("haiii %d\n", i); | |
insertTreeNode(&root, p_operand[i]); | |
} | |
printTreepostOrder(root, infix); | |
return 0; | |
} | |
/* | |
(1*(1*(8+4)))*(1/(2+1)) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment