943,737 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1839
  • C RSS
Sep 4th, 2008
0

Help in Postfix to Binary Tree Representation

Expand Post »
Hello everyone, I am trying to devise a Binary Tree Representation of a Postfix Expression. I am sure I am in the right track, but there's just something wrong in my code which I cannot find.

I am just wondering if you guys can spot where I made my mistake. I would really appreciate your help.

I am using a Dev-C++ compiler.

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<conio.h>
  5.  
  6. struct node{
  7. char item;
  8. struct node* leftChild;
  9. struct node* rightChild;
  10. };
  11.  
  12. typedef struct node node;
  13. node* root;
  14.  
  15. static node* stack[25];
  16. static int stackPtr = -1;
  17.  
  18. node* pop(void)
  19. {
  20. return(stack[stackPtr--]);
  21. }
  22.  
  23. void push(node* root)
  24. {
  25. stack[++stackPtr] = root;
  26. }
  27.  
  28. void operator(char optr)
  29. {
  30. root = (node*)malloc(sizeof(node));
  31. root->item = optr;
  32. root->rightChild = pop();
  33. root->leftChild = pop();
  34. push(root);
  35. }
  36.  
  37. void operand(char opnd)
  38. {
  39. root = (node*)malloc(sizeof(node));
  40. root->item = opnd;
  41. root->rightChild = NULL;
  42. root->leftChild = NULL;
  43. push(root);
  44. }
  45.  
  46. void PostTraverse (node* root)
  47. {
  48. if (root->leftChild != NULL) {
  49. PostTraverse (root->leftChild);
  50. }
  51. if (root->rightChild != NULL) {
  52. PostTraverse (root->rightChild);
  53. }
  54. printf("%c ", root->item);
  55. }
  56.  
  57. int main (void)
  58. {
  59. char postfix[] = "1 2 + 4 * 5 7 - +";
  60. char *token;
  61. int i = 0;
  62.  
  63. token = strtok(postfix, " ");
  64. while (token != NULL) {
  65. if(token !='('&& token !=')'&& token !='^'&& token !='*'&&
  66. token !='/'&& token !='+'&& token !='-')
  67. {
  68. operand(token);
  69. } else {
  70. operator(token);
  71. }
  72. token = strtok(NULL, " ");
  73. }
  74. PostTraverse(stack);
  75. printf("\n");
  76. system("pause");
  77. return 0;
  78. }

One problem I found is that the operator() function is like being ignored in the program and I don't know why that was happening. Any idea?

Thank you very much.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
trick is offline Offline
30 posts
since Mar 2008
Sep 6th, 2008
0

Re: Help in Postfix to Binary Tree Representation

Why is root a global variable? As far as I can see, it doesn't need to be, and it's just causing confusion because several functions take a root parameter.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: greatest of three nos
Next Thread in C Forum Timeline: Problem with filling and searching arrays.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC