Hello, I got this error message for the following C program: Compilation failed due to following error(s).main.c: In function ‘postorder’: main.c:231:1: error: expected declaration or statement at end of input } ^ QUESTION: Write a C program to create a binary expression tree from a postfix expression, including +, -, * and /. You can get some ideas from the algorithm in the following link: http://www.krivalar.com/data-structures-expression-tree Sample of postfix expression: 5 4 3 * + 10 2 / – (Its corresponding infix expression is 5 + 4 * 3 – 10 / 2) PROGRAM: #include #include #include #include #define size 20 typedef struct node { char data; struct node *left; struct node *right; } btree; /*stack stores the operand nodes of the tree*/ btree *stack[size]; int top; void main() { btree *root; char exp[80]; /*exp stores postfix expression*/ btree *create(char exp[80]); void inorder(btree *root); void preorder(btree *root); void postorder(btree *root); clrscr(); printf("\n enter the postfix expression:\n"); scanf("%s",exp); top=-1; /*Initialize the stack*/ root=create(exp); printf("\n The tree is created.....\n"); printf("\n Inorder traversal: \n\n"); inorder(root); printf("\n Preorder traversal: \n\n"); preorder(root); printf("\n Postorder traversal: \n\n"); postorder(root); getch(); } btree *create(char exp[]) { btree *temp; int pos; char ch; void push(btree*); btree *pop(); pos=0; ch=exp[pos]; while(ch!='\0') { /*create new node*/ temp=((btree*)malloc(sizeof(btree))); temp->left=temp->right=NULL; temp->data=ch; if(isalpha(ch)) push(temp); else if(ch=='+' ||ch=='-' || ch=='*' || ch=='/') { temp->right=pop(); temp->left=pop(); push(temp); } else printf("\n Invalid char Expression\n"); pos++; ch=exp[pos]; } temp=pop(); return(temp); } void push(btree *Node) { if(top+1 >=size) printf("Error:Stack is full\n"); top++; stack[top]=Node; } btree* pop() { btree *Node; if(top==-1) printf("\nerror: stack is empty..\n"); Node =stack[top]; top--; return(Node); } /* functions for tree traversal*/ void inorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { inorder(temp->left); printf("%c",temp->data); inorder(temp->right); }} void preorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { printf("%c",temp->data); preorder(temp->left); preorder(temp->right); }} void postorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { postorder(temp->left); postorder(temp->right); printf("%c",temp->data); }
Hello,
I got this error message for the following C program:
Compilation failed due to following error(s).main.c: In function ‘postorder’:
main.c:231:1: error: expected declaration or statement at end of input
}
^
QUESTION:
Write a C program to create a binary expression tree from a postfix expression, including +, -,
* and /.
You can get some ideas from the
http://www.krivalar.com/data-structures-expression-tree
Sample of postfix expression:
5 4 3 * + 10 2 / – (Its corresponding infix expression is 5 + 4 * 3 – 10 / 2)
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<malloc.h>
#define size 20
typedef struct node
{ char data;
struct node *left;
struct node *right;
}
btree;
/*stack stores the operand nodes of the tree*/
btree *stack[size];
int top;
void main()
{
btree *root;
char exp[80];
/*exp stores postfix expression*/
btree *create(char exp[80]);
void inorder(btree *root);
void preorder(btree *root);
void postorder(btree *root);
clrscr();
printf("\n enter the postfix expression:\n");
scanf("%s",exp);
top=-1; /*Initialize the stack*/
root=create(exp);
printf("\n The tree is created.....\n");
printf("\n Inorder traversal: \n\n");
inorder(root);
printf("\n Preorder traversal: \n\n");
preorder(root);
printf("\n Postorder traversal: \n\n");
postorder(root);
getch();
}
btree *create(char exp[])
{
btree *temp;
int pos;
char ch;
void push(btree*);
btree *pop();
pos=0;
ch=exp[pos];
while(ch!='\0')
{
/*create new node*/
temp=((btree*)malloc(sizeof(btree)));
temp->left=temp->right=NULL;
temp->data=ch;
if(isalpha(ch))
push(temp);
else if(ch=='+' ||ch=='-' || ch=='*' || ch=='/')
{ temp->right=pop();
temp->left=pop();
push(temp);
}
else
printf("\n Invalid char Expression\n");
pos++;
ch=exp[pos];
}
temp=pop();
return(temp);
}
void push(btree *Node)
{
if(top+1
>=size)
printf("Error:Stack is full\n");
top++;
stack[top]=Node;
}
btree* pop()
{
btree *Node;
if(top==-1)
printf("\nerror: stack is empty..\n");
Node =stack[top];
top--;
return(Node);
}
/* functions for tree traversal*/
void inorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{ inorder(temp->left);
printf("%c",temp->data);
inorder(temp->right);
}}
void preorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
printf("%c",temp->data);
preorder(temp->left);
preorder(temp->right);
}}
void postorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%c",temp->data);
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images