| | |
Binary Expression Tree
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 1
Added just to help other....
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define Operator 1 #define notOperator 0 #define empty -1 void formatting(void); void getInput(void); int chkElement(char); void opFunc(char); void varFunc(char); void push(struct node*); struct node* pop(void); void dispTree(void); void infix(struct node*); void prefix(struct node*); void postfix(struct node*); char equation[25]; struct node* stack[25]; int stackPtr = -1; struct node{ char item; struct node* leftChild; struct node* rightChild; }; struct node* root; void main(void) { int count; clrscr(); formatting(); getInput(); printf("\nYou have entered: "); puts(equation); for(count = 0; equation[count] != '\0'; count++) { switch(chkElement(equation[count])) { case Operator: opFunc(equation[count]); break; case notOperator: varFunc(equation[count]); break; default: printf("\nSorry unrecognized entry"); } } dispTree(); getch(); } void dispTree(void) { char choice; printf("\nSelect the output form: [i]nfix, p[r]efix, p[o]stfix: "); choice = getche(); printf("\n"); switch(choice) { case 'i': printf("\nInorder representation of output is: "); infix(stack[stackPtr]); break; case 'r': printf("\nPreorder representation of output is: "); prefix(stack[stackPtr]); break; case 'o': printf("\nPostorder representation of output is: "); postfix(stack[stackPtr]); break; default: printf("\nYou have pressed the button other than given choices"); } } void infix(struct node* root) { if(root->leftChild != NULL) infix(root->leftChild); printf("%c", root->item); if(root->rightChild !=NULL) infix(root->rightChild); } void prefix(struct node* root) { printf("%c", root->item); if(root->leftChild != NULL) prefix(root->leftChild); if(root->rightChild !=NULL) prefix(root->rightChild); } void postfix(struct node* root) { if(root->leftChild != NULL) postfix(root->leftChild); if(root->rightChild !=NULL) postfix(root->rightChild); printf("%c", root->item); } void opFunc(char optr) { root = (struct node*)malloc(sizeof(struct node)); root->item = optr; root->rightChild = pop(); root->leftChild = pop(); push(root); } struct node* pop(void) { return(stack[stackPtr--]); } void varFunc(char var) { root = (struct node*)malloc(sizeof(struct node)); root->item = var; root->rightChild = NULL; root->leftChild = NULL; push(root); } void push(struct node* root) { stack[++stackPtr] = root; } int chkElement(char element) { switch(element) { case '+': case '-': case '*': case '/': case '%': case '^': return(Operator); default: return(notOperator); } } void getInput(void) { printf("\n\nEnter equation in the form of postfix: "); gets(equation); } void formatting(void) { int i; printf("\n"); for(i =0; i<=79 ; i++) printf("*"); printf("\n......... Prgogram title\t\t# Binary Tree"); printf("\n......... Created by\t\t # Romasa Qasim"); printf("\n......... Description\t\t # Creation of Expression Tree\n"); for( i =0; i<=79 ; i++) printf("*"); }
Last edited by Narue; Dec 12th, 2007 at 3:22 pm. Reason: Added code tags
http://www.codeblocks.org/downloads.shtml
Download "Code::Blocks IDE, with MINGW compiler"
In other words, join the 21st century and leave your fossil compiler in the museum.
Download "Code::Blocks IDE, with MINGW compiler"
In other words, join the 21st century and leave your fossil compiler in the museum.
We appreciate your efforts its.romi, but this is to remind you that if you want to post codes for reviews, then go to contribute code in code snippets.
Salem, that IDE is great. Thank you
Salem, that IDE is great. Thank you
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Hi,
i have been trying to write code for creating an infix tree for the given infix expression.
but i struck at writting the procedure itself.
suppose my expression is :
a + b *c + d
this is how the compiler evaluates
i am following the below procedure
scan each character and if its not " )" push it on to the stack.(please suggest me is it the correct way, getting tuff when you write the code)
when ever i found a " )" i will pop the characters from the stack and check if its an opearator if its an operator i will create the node with operator as root -> info
other wise
nodes with info as characters with left and right child NULL
( but how to decide what to add as left and right child with the root node )
for example
at first the stack is
after poping whenwe find the ")" the stack is
my confusion here is to how
to decide the terminating condition for pop and creating the tree from the sofar poped values ( i can use the condition like chatacter is not equal to ")" but still confusion)
this is what the tree should look like after creation.
for example this is how my stack looks like when we find the first
" )"
so here i pop c * b and i create
and push this node on to the stack of nodes.
next i will pop the "(" so after this my stack has
till now we traversed
( a + ( b * c)
now we find another ")".
this is where i am struck because there are two possibilities
one is
which is incorrect
and the other is
this is correct
how will i decide where to add a and other previous node ( either left or right ).
i believe that i am getting confused about how to store the nodes and expression in the stack and related push and pop operations.
please suggest me what is the correct procedure to follow and related push and pop operations.
the program should work for both the paranthseized expressions and non paranthesized.
please reply me with some good source, after a lot of struggle i am posting this post.
i have been trying to write code for creating an infix tree for the given infix expression.
but i struck at writting the procedure itself.
suppose my expression is :
a + b *c + d
this is how the compiler evaluates
C Syntax (Toggle Plain Text)
( ( a + ( b * c ) ) + d )
i am following the below procedure
scan each character and if its not " )" push it on to the stack.(please suggest me is it the correct way, getting tuff when you write the code)
when ever i found a " )" i will pop the characters from the stack and check if its an opearator if its an operator i will create the node with operator as root -> info
other wise
nodes with info as characters with left and right child NULL
( but how to decide what to add as left and right child with the root node )
for example
at first the stack is
C Syntax (Toggle Plain Text)
c * b ( + a (
after poping whenwe find the ")" the stack is
C Syntax (Toggle Plain Text)
+ a (
my confusion here is to how
to decide the terminating condition for pop and creating the tree from the sofar poped values ( i can use the condition like chatacter is not equal to ")" but still confusion)
this is what the tree should look like after creation.
C Syntax (Toggle Plain Text)
+ + d a * b c
for example this is how my stack looks like when we find the first
" )"
C Syntax (Toggle Plain Text)
c * b ( + a (
so here i pop c * b and i create
C Syntax (Toggle Plain Text)
* b c
next i will pop the "(" so after this my stack has
C Syntax (Toggle Plain Text)
+ a (
till now we traversed
( a + ( b * c)
now we find another ")".
this is where i am struck because there are two possibilities
one is
C Syntax (Toggle Plain Text)
+ + d * a b c
and the other is
C Syntax (Toggle Plain Text)
+ + d a * b c
this is correct
how will i decide where to add a and other previous node ( either left or right ).
i believe that i am getting confused about how to store the nodes and expression in the stack and related push and pop operations.
please suggest me what is the correct procedure to follow and related push and pop operations.
the program should work for both the paranthseized expressions and non paranthesized.
please reply me with some good source, after a lot of struggle i am posting this post.
Last edited by Gaiety; Oct 3rd, 2009 at 5:27 am. Reason: adding extra
Minds are like parachutes - they only work when they are open
Gaiety
Gaiety
![]() |
Similar Threads
- Help for Binary Tree Traversal for infix to postfix conversion (C++)
- search binary tree (C)
- binary integer logic (Computer Science)
- calculator program (Java)
Other Threads in the C Forum
- Previous Thread: regarding to C error
- Next Thread: find error in program...
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






