From 03509cfa19d54e3ea6708698de6e9711c8baf61a Mon Sep 17 00:00:00 2001 From: Mohit Singh Date: Thu, 20 Dec 2018 19:21:58 +0530 Subject: [PATCH] Expression Tree program --- Expression Tree/ExpressionTree.c | 157 +++++++++++++++++++++++++++++++ Expression Tree/program | Bin 0 -> 12840 bytes 2 files changed, 157 insertions(+) create mode 100644 Expression Tree/ExpressionTree.c create mode 100644 Expression Tree/program diff --git a/Expression Tree/ExpressionTree.c b/Expression Tree/ExpressionTree.c new file mode 100644 index 0000000..eef65cf --- /dev/null +++ b/Expression Tree/ExpressionTree.c @@ -0,0 +1,157 @@ +#include +#include +#include +#include + +struct exptree +{ + char data; + struct exptree *left; + struct exptree *right; +}; + +struct exptree *getnode(char); +struct exptree *createExpTree(char[]); +int precedence(char); +void preOrder(struct exptree*); +void inOrder(struct exptree*); +void postOrder(struct exptree*); + +int main() +{ + struct exptree *root; + char exp[20]; + printf("Enter the Infix Expression: "); + scanf("%s", exp); + root = createExpTree(exp); + printf("PreOrder: "); + preOrder(root); + printf("\nInOrder: "); + inOrder(root); + printf("\nPostOrder: "); + postOrder(root); + return 0; +} + +struct exptree *getnode(char ch) +{ + struct exptree *newnode = (struct exptree*)malloc(sizeof(struct exptree)); + newnode ->left = NULL; + newnode -> right = NULL; + newnode -> data = ch; + return newnode; +} + +int precedence(char ch) +{ + switch(ch) + { + case '^': + return 3; + case '/': + case '*': + return 2; + case '-': + case '+': + return 1; + default: + return 0; + } +} + +void preOrder(struct exptree *root) +{ + if(root != NULL) + { + printf("%c ", root->data); + preOrder(root->left); + preOrder(root->right); + } +} + +void inOrder(struct exptree *root) +{ + if(root!=NULL) + { + inOrder(root->left); + printf("%c ", root->data); + inOrder(root->right); + } +} + +void postOrder(struct exptree *root) +{ + if(root != NULL) + { + postOrder(root->left); + postOrder(root->right); + printf("%c ", root->data); + } +} + +struct exptree *createExpTree(char exp[20]) +{ + struct exptree *operst[10], *treest[10]; + int top1 = -1, top2 = -1; + struct exptree *temp, *t1, *t2, *t3; + char symbol; + int i; + for(i=0;idata< precedence(symbol))) + { + operst[++top1] = temp; + } + else if(symbol == ')') + { + while(operst[top1]->data != '(' && top1 != -1 && top2 != -1 && precedence(operst[top1]->data) >= precedence(symbol)) + { + t3 = operst[top1--]; + t1 = treest[top2--]; + t2 = treest[top2--]; + t3->right = t1; + t3->left = t2; + treest[++top2] = t3; + } + if(operst[top1]->data =='(') + top1--; + } + else + { + while(top1 != -1 && top2 != -1 && precedence(operst[top1]->data >= precedence(symbol))) + { + t3 = operst[top1--]; + t1 = treest[top2--]; + t2 = treest[top2--]; + t3->right = t1; + t3->left = t2; + treest[++top2] = t3; + } + operst[++top1] = temp; + } + } + } + while(top1 != -1) + { + t3 = operst[top1--]; + t1 = treest[top2--]; + t2 = treest[top2--]; + t3->right = t1; + t3->left = t2; + treest[++top2] = t3; + } + return treest[top2]; +} \ No newline at end of file diff --git a/Expression Tree/program b/Expression Tree/program new file mode 100644 index 0000000000000000000000000000000000000000..34acd5032dd92c7adc91c5aea351d94d4020d37f GIT binary patch literal 12840 zcmeHNeQX@X6`%9j#39L@Ljp1RAX^~92Ia_i97u@Iv6FS{ByQqR@!>4zyLEiRzO(N3 zs1p?k97eesgZ}^#RFF_VP>Wg#q%=f=fP+HhUs^bh{W0DDBBTBcF!uiXO$h(Q56_d?jQAyel5zMo7O`{g(>M$Y5vfPQ2iCZ zWG|qdHY}L3-49_$aemWMp%k~M_QuOgw~C*s>aMu4GupOd*^Ql%g`Lq@s%K$O&5DI9 zmIadWz!KSS@=v-OHf@tF(Rg!eDdw5@5k~SySGS(o6h3^#RjGZ3x%$O5bFX>+1+wKI zs^7Rg1x>Ap_7Y&16~SkZgWoU?-Z~Ecd*B*=?y?F%v3947gC7R2;pZ-!09@?`!Pnwf z%)h|+glRZd2*ZdbpJ5xvnABlNq%>9#be5^VXGEZq(qcp{*=f$RU&6xiM%g( zP^Wpr^Ls`#I*slaafQv2e5u%}_?eRTinQWWv!j#AcRzS4e1G8UxvX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