This program is to find the expression tree.

#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "string.h"
#include "stdlib.h"

struct treenode
{
char c;
treenode * rlink;
treenode * llink;
}*stc[30],*temp,*root;


char prefix[20],ch;
int topt=-1,max=50,len; //global declaration

void pusht(struct treenode * p);
struct treenode* popt();
void tredis(struct treenode *ptr,int level);
void exptree();
void post(struct treenode* p);

void main()
{
clrscr();
printf("
Enter a prefix expression :");
scanf("%s",prefix);
exptree();
tredis(root,1);
printf("

The postfix expression is :");
post(root);
getch();
}

void post(struct treenode* p)
{
if(p!=NULL)
{
post(p->llink);
post(p->rlink);
printf("%c",p->c);
}
}
void exptree()
{
len=strlen(prefix);
int i=len-1;
while(i>=0)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
temp=(struct treenode*)malloc(sizeof(struct treenode));
temp->c=prefix[i];
temp->llink=popt();
temp->rlink=popt();
pusht(temp);
break;
default :
temp=(struct treenode*)malloc(sizeof(struct treenode));
temp->c=prefix[i];
temp->rlink=NULL;
temp->llink=NULL;
pusht(temp);
}
i--;
}
root=stc[topt];
}


void pusht(struct treenode * p)
{
if(topt==max)
{
printf("
/**Beyond Capacity**/");
}
else
{
stc[++topt]=p;
}
}


struct treenode* popt()
{
if(topt==-1)

printf("
/**No Expression**/");

else

return(stc[topt--]);

}
void tredis(struct treenode *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
tredis(ptr->rlink, level+1);
printf("
);
for (i = 0; i <>c);
tredis(ptr->llink, level+1);
}
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!