| | |
Help in Postfix to Binary Tree Representation
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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.
Views: 854 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





