Binary Expression Tree

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 17
Reputation: its.romi is an unknown quantity at this point 
Solved Threads: 1
its.romi its.romi is offline Offline
Newbie Poster

Binary Expression Tree

 
-2
  #1
Dec 12th, 2007
Added just to help other....
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. #define Operator 1
  6. #define notOperator 0
  7. #define empty -1
  8.  
  9. void formatting(void);
  10. void getInput(void);
  11. int chkElement(char);
  12. void opFunc(char);
  13. void varFunc(char);
  14. void push(struct node*);
  15. struct node* pop(void);
  16. void dispTree(void);
  17. void infix(struct node*);
  18. void prefix(struct node*);
  19. void postfix(struct node*);
  20.  
  21. char equation[25];
  22. struct node* stack[25];
  23. int stackPtr = -1;
  24.  
  25. struct node{
  26. char item;
  27. struct node* leftChild;
  28. struct node* rightChild;
  29. };
  30. struct node* root;
  31. void main(void)
  32. {
  33. int count;
  34. clrscr();
  35. formatting();
  36. getInput();
  37. printf("\nYou have entered: ");
  38. puts(equation);
  39. for(count = 0; equation[count] != '\0'; count++)
  40. {
  41. switch(chkElement(equation[count]))
  42. {
  43. case Operator:
  44. opFunc(equation[count]);
  45. break;
  46. case notOperator:
  47. varFunc(equation[count]);
  48. break;
  49. default:
  50. printf("\nSorry unrecognized entry");
  51. }
  52. }
  53.  
  54. dispTree();
  55. getch();
  56. }
  57.  
  58. void dispTree(void)
  59. {
  60. char choice;
  61. printf("\nSelect the output form: [i]nfix, p[r]efix, p[o]stfix: ");
  62. choice = getche();
  63. printf("\n");
  64. switch(choice)
  65. {
  66. case 'i':
  67. printf("\nInorder representation of output is: ");
  68. infix(stack[stackPtr]);
  69. break;
  70.  
  71. case 'r':
  72. printf("\nPreorder representation of output is: ");
  73. prefix(stack[stackPtr]);
  74. break;
  75.  
  76. case 'o':
  77. printf("\nPostorder representation of output is: ");
  78. postfix(stack[stackPtr]);
  79. break;
  80.  
  81. default:
  82. printf("\nYou have pressed the button other than given choices");
  83. }
  84. }
  85.  
  86. void infix(struct node* root)
  87. {
  88. if(root->leftChild != NULL) infix(root->leftChild);
  89. printf("%c", root->item);
  90. if(root->rightChild !=NULL) infix(root->rightChild);
  91. }
  92.  
  93. void prefix(struct node* root)
  94. {
  95. printf("%c", root->item);
  96. if(root->leftChild != NULL) prefix(root->leftChild);
  97. if(root->rightChild !=NULL) prefix(root->rightChild);
  98. }
  99.  
  100. void postfix(struct node* root)
  101. {
  102. if(root->leftChild != NULL) postfix(root->leftChild);
  103. if(root->rightChild !=NULL) postfix(root->rightChild);
  104. printf("%c", root->item);
  105. }
  106.  
  107. void opFunc(char optr)
  108. {
  109. root = (struct node*)malloc(sizeof(struct node));
  110. root->item = optr;
  111. root->rightChild = pop();
  112. root->leftChild = pop();
  113. push(root);
  114. }
  115.  
  116. struct node* pop(void)
  117. {
  118. return(stack[stackPtr--]);
  119. }
  120. void varFunc(char var)
  121. {
  122. root = (struct node*)malloc(sizeof(struct node));
  123. root->item = var;
  124. root->rightChild = NULL;
  125. root->leftChild = NULL;
  126. push(root);
  127. }
  128.  
  129. void push(struct node* root)
  130. {
  131. stack[++stackPtr] = root;
  132. }
  133.  
  134. int chkElement(char element)
  135. {
  136. switch(element)
  137. {
  138. case '+':
  139. case '-':
  140. case '*':
  141. case '/':
  142. case '%':
  143. case '^':
  144. return(Operator);
  145.  
  146. default:
  147. return(notOperator);
  148. }
  149. }
  150.  
  151. void getInput(void)
  152. {
  153. printf("\n\nEnter equation in the form of postfix: ");
  154. gets(equation);
  155. }
  156.  
  157. void formatting(void)
  158. {
  159. int i;
  160. printf("\n");
  161. for(i =0; i<=79 ; i++)
  162. printf("*");
  163.  
  164. printf("\n......... Prgogram title\t\t# Binary Tree");
  165. printf("\n......... Created by\t\t # Romasa Qasim");
  166. printf("\n......... Description\t\t # Creation of Expression Tree\n");
  167.  
  168. for( i =0; i<=79 ; i++)
  169. printf("*");
  170. }
Last edited by Narue; Dec 12th, 2007 at 3:22 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Binary Expression Tree

 
0
  #2
Dec 18th, 2007
I can write a big list of comments on your code. But i dont have time for that. But just a quick review you are using quite a lot of non standard function and libraries.

What compiler are u using?

But over all good code.

ssharish
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: its.romi is an unknown quantity at this point 
Solved Threads: 1
its.romi its.romi is offline Offline
Newbie Poster

Re: Binary Expression Tree

 
0
  #3
Dec 18th, 2007
im using Turbo C 3.0
can u plz tell me those errors, so that i can do some better work next time
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Binary Expression Tree

 
1
  #4
Dec 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Binary Expression Tree

 
1
  #5
Dec 19th, 2007
Use Dev-C++, thats good as well. Salem Thanks for that Code::Blocks that looks pretty cool.

ssharish
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Binary Expression Tree

 
0
  #6
Dec 19th, 2007
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
"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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 76
Reputation: Gaiety is an unknown quantity at this point 
Solved Threads: 2
Gaiety's Avatar
Gaiety Gaiety is offline Offline
Junior Poster in Training

Re: Binary Expression Tree

 
-1
  #7
Oct 3rd, 2009
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

  1. ( ( 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

  1.  
  2. c
  3. *
  4. b
  5. (
  6. +
  7. a
  8. (

after poping whenwe find the ")" the stack is

  1.  
  2. +
  3. a
  4. (

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.

  1. +
  2. + d
  3. a *
  4. b c

for example this is how my stack looks like when we find the first
" )"
  1.  
  2. c
  3. *
  4. b
  5. (
  6. +
  7. a
  8. (

so here i pop c * b and i create

  1. *
  2. b c
and push this node on to the stack of nodes.
next i will pop the "(" so after this my stack has

  1.  
  2.  
  3. +
  4. a
  5. (

till now we traversed
( a + ( b * c)
now we find another ")".

this is where i am struck because there are two possibilities
one is

  1. +
  2. + d
  3. * a
  4. b c
which is incorrect

and the other is

  1. +
  2. + d
  3. a *
  4. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: chat2fanna is an unknown quantity at this point 
Solved Threads: 0
chat2fanna chat2fanna is offline Offline
Newbie Poster
 
0
  #8
Oct 8th, 2009
any other program simpler than this....!?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC