954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Forming an expression tree from postfix expression

//To convert postfix expression into an expression tree
 
#include"stdio.h"
#include"conio.h"
#include"process.h"
#include"string.h"
#include"stdlib.h"
 
struct node{
char item;
struct node *lnode;
struct node *rnode;
}*stc[30],*temp,*root;

typedef struct node *NODEPTR;
char expression[50];
char c;
int top=0;
NODEPTR pop();
void push(NODEPTR);
void getinput();
void inorder(NODEPTR p);
     
int main (){
    getinput();
     int i,j;
     for(i=0;i<strlen(expression);i++) 
     {
                                        printf("\nstrlength=%d--------for i=%d\n",strlen(expression),i);
                                        c=expression[i];
                                        switch(c)
                                        {
                                                 case '+':
                                                 case '-':
                                                 case '*':
                                                 case '/':
                                                 case '^':
                                                   {   temp=(NODEPTR)malloc(sizeof(struct node));
                                                      temp->item=c;
                                                      temp->rnode=pop();
                                                      temp->lnode=pop();
                                                      push(root);
                                                   }
                                                      default :
                                                    {  temp=(NODEPTR)malloc(sizeof(struct node));
                                                      temp->item=c;
                                                      temp->lnode=NULL;
                                                      temp->rnode=NULL;
                                                      push(temp);
                                                     } void free(void *ptr);
                                                      break;
                                         }
     }
     root=pop();
     inorder(root);  
     getch();           
                                         } 
                                                                     
NODEPTR pop(NODEPTR p) {
      if(top==0)
      printf("Underflow condition !!");
      else {
          // printf("\npopped  \n");
           return(stc[top--]);
           }
           } 
void push(NODEPTR p) {
     if(top==29)
     printf("Overflow condition !!");
     else {
          //printf("pushed ");
          stc[++top]=p;
          }
          }

void getinput() {
     printf("Enter a valid postfix string : ");
     gets(expression);
     }

    
void inorder(NODEPTR p)
	{if(p!=NULL)
		{
		inorder(p->lnode);
		printf("Data :%c",p->item);
		inorder(p->rnode);
		}
	else
	{printf("\nNull Tree");
		return;}
  }


Can anyone explain what mistake i made in the program ??.....pls its urgent and if possible please provide solutions..

sneha_
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

In line# 41
push(root);

Are you trying to push root. You should have done:
push(temp);

In line#50
void free(void *ptr);

What you are trying to do here. Are you trying to free the memory or you want to redefine the function free() ?

Better read about free().


Also, review your push() and pop() operation, test them independantly before using them with your logic.

dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: