#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>

#define MAX 20

typedef struct cvor
{
	int br;
	char znak;
	int is_no;
	struct cvor* levi;
	struct cvor* desni;
} CVOR;

CVOR *napravi_cvor()
{
	CVOR *novi;
	if((novi = (CVOR*)malloc(sizeof(CVOR)))==NULL)
	{
		fprintf(stderr,"Greska pri alokaciji memorije\n");
		exit(1);
	}
	novi->levi = NULL;
	novi->desni = NULL;
}

pretvori_u_stablo(char** s,CVOR **drvo)
{
	char c;
	int i,n=0;

	CVOR *novi=napravi_cvor();
	while((*s)[i]==' ')
		i++;
	
	c=(*s)[i++];
        (*drvo)->is_no = 0;
        (*drvo)->znak = c;
        
        while((*s)[i]==' ')
		i++;
		
        c=(*s)[i];
        if(!isdigit(c))
       	{
       		(*drvo)->levi = novi;
       		pretvori_u_stablo((*s)+i,&novi);
	}

       	else
       	{
       		CVOR* novi = napravi_cvor();
       		(*drvo)->levi = novi;
       		
       		while(isdigit(c))
       		{
       			n = (n*10)+(c-'0');
       			c = (*s)[++i];
       		}
       		novi->is_no=1;
       		novi->br = n;
       	}
        
        while((*s)[i]==' ')
		i++;
	
       	if(!isdigit(c))
       	{
       		(*drvo)->desni = novi;
       		pretvori_u_stablo((*s)+i,&novi);
	}

	else
       	{
       		int n=0;
       		CVOR* novi = napravi_cvor();
       		(*drvo)->desni = novi;
       		
       		while(isdigit(c))
       		{
       			n = (n*10)+(c-'0');
       			c = (*s)[++i];
       		}
       		novi->is_no=1;
       		novi->br = n;
       	}
}
int main(int argc, char *argv[])
{
	char izraz[MAX];
	CVOR *drvo = NULL;
	int i;


	printf("Unesite izraz u poljskoj notaciji:");
	if(fgets(izraz,20,stdin)==NULL)
	{
		fprintf(stderr,"greska");
		exit(1);
	}

	while(izraz[i]!='\n') 
		i++;
	izraz[i]=izraz[i+1];

	pretvori_u_stablo(&izraz,&drvo);

}

