Guest User

Untitled

a guest
Nov 14th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4.  
  5. struct exptree{
  6. char data;
  7. struct exptree *left,*right;
  8. };
  9. struct exptree *getnode(char);
  10. struct exptree *createexptree(char[]);
  11. int prcd(char);
  12. void preorder(struct exptree *);
  13. void inorder(struct exptree *);
  14. void postorder(struct exptree *);
  15.  
  16. int main()
  17. {
  18. struct exptree *root=NULL;
  19. char infix[20];
  20. printf("ENTER THE INFIX EXP\n");
  21. scanf("%s",infix);
  22. root=createexptree(infix);
  23. printf("\nINFIX\n");
  24. inorder(root);
  25. printf("\nPOSTFIX\n");
  26. postorder(root);
  27. printf("\nPREFIX\n");
  28. preorder(root);
  29. return 0;
  30. }
  31. struct exptree *getnode(char item)
  32. {
  33. struct exptree *newnode;
  34. newnode=(struct exptree *)malloc(sizeof(struct exptree));
  35. newnode->data=item;
  36. newnode->left=NULL;
  37. newnode->right=NULL;
  38. return newnode;
  39. }
  40. int prcd(char x)
  41. {
  42. switch(x)
  43. {
  44. case '^':return 3;break;
  45. case '/':
  46. case '*':return 2;break;
  47. case '+':
  48. case '-':return 1;break;
  49. }
  50. return 0;
  51. }
  52. struct exptree *createexptree(char infix[20])
  53. {
  54. int i =0;
  55. char symbol;
  56. struct exptree *temp,*t1,*t2,*t3;
  57. struct exptree *operst[10],*treest[10];//operator stack and operand stack
  58. int top1=-1,top2=-1;
  59. for(i=0;infix[i]!='\0';i++)
  60. {
  61. symbol=infix[i];
  62. if(isalnum(symbol))
  63. {
  64. temp=getnode(symbol);
  65. treest[++top2] = temp;
  66. }
  67. else
  68. {
  69. temp=getnode(symbol);
  70. if(symbol=='(')
  71. {operst[++top1]=temp;}
  72. else if(top1 ==-1 || prcd(operst[top1]->data)< prcd(symbol))
  73. {operst[++top1]=temp;}
  74. else if(symbol==')')
  75. {
  76. while((operst[top1]->data)!='(' && (top1!= -1) && top2!= -1 && prcd(operst[top1]->data) >= prcd(symbol))
  77. {
  78. t3=operst[top1--]; //take operator
  79. t1=treest[top2--]; //take operand
  80. t2=treest[top2--]; //another operand
  81. t3->right=t1;
  82. t3->left=t2;
  83. treest[++top2]=t3; //push tree to stack
  84. }
  85. if(operst[top1]->data=='(')
  86. {top1--;}
  87. }
  88. else
  89. {
  90. while((top1!=-1)&&(top2!=-1)&&prcd(operst[top1]->data)>=prcd(symbol))
  91. {
  92. t3=operst[top1--];
  93. t1=treest[top2--];
  94. t2=treest[top2--];
  95. t3->right=t1;
  96. t3->left=t2;
  97. treest[++top2]=t3;
  98. }
  99. operst[++top1]=temp;
  100. }
  101. }
  102. } //end of for
  103. while(top1 !=-1)
  104. {
  105. t1=treest[top2--];
  106. t2=treest[top2--];
  107. temp=operst[top1--];
  108. temp->right=t1;
  109. temp->left=t2;
  110. treest[++top2]=temp;
  111. }
  112. return treest[top2];
  113. }
  114. void inorder(struct exptree *root)
  115. {
  116. if(root!=NULL)
  117. {
  118. inorder(root->left);
  119. printf("%c",root->data);
  120. inorder(root->right);
  121. }
  122. }
  123. void preorder(struct exptree *root)
  124. {
  125. if(root!=NULL)
  126. {
  127. printf("%c",root->data);
  128. preorder(root->left);
  129. preorder(root->right);
  130. }
  131. }
  132. void postorder(struct exptree *root)
  133. {
  134. if(root!=NULL)
  135. {
  136. postorder(root->left);
  137. postorder(root->right);
  138. printf("%c",root->data);
  139. }
  140. }
Add Comment
Please, Sign In to add comment