#include <stdio.
h>
#include <stdlib.h>
typedef struct nodeStructure
{
char data;
struct nodeStructure *left, *right;
} node;
node *root;
node *stack[10];
int top;
void operatorNode( char );
void operandNode( char );
node *pop();
void push( node * );
void postOrder( node * );
void preOrder( node * );
void inOrder( node * );
int main(void)
{
root = NULL;
top = -1;
char postfix[10] = "ABC*+D+";
int i = 0;
char ch;
while( (ch = postfix[i]) != '\0' )
{
if ( ch == '/' || ch == '*' || ch == '-'|| ch == '+' )
{
operatorNode( ch );
}
else
{
operandNode( ch );
}
i++;
}
root = pop();
printf( "PREORDER : " );
preOrder( root );
printf( "\n" );
printf( "INORDER : " );
inOrder( root );
printf( "\n" );
printf( "POSTORDER: " );
postOrder( root );
getch();
return 0;
}
void push( node *ptr )
{
top++;
stack[top] = ptr;
return;
}
node *pop()
{
node *del;
del = stack[top];
top--;
return del;
}
void operandNode( char ch )
{
node *newNode;
newNode = (node *)malloc( 1 * sizeof(node) );
newNode->data = ch;
newNode->left = NULL;
newNode->right = NULL;
push( newNode );
}
void operatorNode( char ch )
{
node *newNode, *pl, *pr;
newNode = (node *)malloc( 1 * sizeof(node) );
newNode->data = ch;
pr = pop();
pl = pop();
newNode->left = pl;
newNode->right = pr;
push( newNode );
}
void preOrder( node *ptr )
{
printf( "%c ", ptr->data );
if ( ptr->left != NULL )
{
preOrder( ptr->left );
}
if ( ptr->right != NULL )
{
preOrder( ptr->right );
}
return;
}
void inOrder( node *ptr )
{
if ( ptr->left != NULL )
{
inOrder( ptr->left );
}
printf( "%c ", ptr->data );
if ( ptr->right != NULL )
{
inOrder( ptr->right );
}
return;
}
void postOrder( node *ptr )
{
if ( ptr->left != NULL )
{
postOrder( ptr->left );
}
if ( ptr->right != NULL )
{
postOrder( ptr->right );
}
printf( "%c ", ptr->data );
return;
}