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); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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<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);
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Randomized Select Algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education