| | |
Help in Postfix to Binary Tree Representation
![]() |
•
•
Join Date: Mar 2008
Posts: 30
Reputation:
Solved Threads: 0
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.
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.
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.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h> struct node{ char item; struct node* leftChild; struct node* rightChild; }; typedef struct node node; node* root; static node* stack[25]; static int stackPtr = -1; node* pop(void) { return(stack[stackPtr--]); } void push(node* root) { stack[++stackPtr] = root; } void operator(char optr) { root = (node*)malloc(sizeof(node)); root->item = optr; root->rightChild = pop(); root->leftChild = pop(); push(root); } void operand(char opnd) { root = (node*)malloc(sizeof(node)); root->item = opnd; root->rightChild = NULL; root->leftChild = NULL; push(root); } void PostTraverse (node* root) { if (root->leftChild != NULL) { PostTraverse (root->leftChild); } if (root->rightChild != NULL) { PostTraverse (root->rightChild); } printf("%c ", root->item); } int main (void) { char postfix[] = "1 2 + 4 * 5 7 - +"; char *token; int i = 0; token = strtok(postfix, " "); while (token != NULL) { if(token !='('&& token !=')'&& token !='^'&& token !='*'&& token !='/'&& token !='+'&& token !='-') { operand(token); } else { operator(token); } token = strtok(NULL, " "); } PostTraverse(stack); printf("\n"); system("pause"); return 0; }
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.
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
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
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: greatest of three nos
- Next Thread: Problem with filling and searching arrays.
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





