Added just to help other....

#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("*");
}

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

im using Turbo C 3.0
can u plz tell me those errors, so that i can do some better work next time

Use Dev-C++, thats good as well. Salem Thanks for that Code::Blocks that looks pretty cool.

ssharish

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 :)

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

( ( 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
                        *
                        b
                        (
                        +
                        a
                        (

after poping whenwe find the ")" the stack is

+
                        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.

+
                             +                 d 
                      a              *
                                 b         c

for example this is how my stack looks like when we find the first
" )"

c
                        *
                        b
                        (
                        +
                        a
                        (

so here i pop c * b and i create

*
           b       c

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

+
                        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

+
                             +                 d 
                       *           a
                 b         c

which is incorrect

and the other is

+
                             +                 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.

any other program simpler than this....!?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.