Help in Postfix to Binary Tree Representation

Reply

Join Date: Mar 2008
Posts: 30
Reputation: trick is an unknown quantity at this point 
Solved Threads: 0
trick trick is offline Offline
Light Poster

Help in Postfix to Binary Tree Representation

 
0
  #1
Sep 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Help in Postfix to Binary Tree Representation

 
0
  #2
Sep 6th, 2008
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC