#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
};
node *tree=NULL;
node *root=NULL;
node *tp[50];
node *insert(node *tree,int ele);
node *insert1(node *tree,int ele);
node *x=NULL;
node *p=NULL;
int disp[50];
int k=0;
void preorder(node *tree);
void inorder(node *tree);
void postorder(node *tree);
int count=0;
/* node *insert(node *tree,int ele)
{
count++;
if(tree==NULL)
{
tree=new node;
root=tree;
tree->left=tree->right=NULL;
tree->data=ele;
}
else
{
char *ptr;
char val2='f';
ptr=strchr(ele,val2);
if(ptr)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
}
return(tree);
}*/
node *insert1(node *tree,int ele,char *ele1)
{
if(tp[0]->data==0 && root==NULL)
root=tree;
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
}
else
{
char *ptr;
char val2='f';
ptr=strchr(ele1,val2);
if(ptr)
{
tp[count]=tree;
tree=insert1(tree->left,ele,ele1);
tp[count]->left=tree;
count++;
}
else
{
tree=insert1(tp[count-1]->right,ele,ele1);
tp[count-1]->right=tree;
count--;
}
}
return(tree);
}
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data;
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<endl;
inorder(tree->right);
}
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data;
}
}
void display(node *tree)
{
// cout<<tree->data;
disp[k]=tree->data;
k++;
if(tree->right->right==NULL && tree->right->left==NULL)
{
for(int i=0;i<k;i++)
cout<<disp[i]<<"->";
cout<<tree->right->data;
cout<<endl;
}
else
{
display(tree->right);
cout<<endl;
}
if(tree->left->left==NULL && tree->left->right==NULL)
{
for(int i=0;i<k;i++)
cout<<disp[i]<<"->";
cout<<tree->left->data;
cout<<endl;
}
else
{
display(tree->left);
cout<<endl;
}
if(k!=1)
k--;
return;
}
int main()
{
//clrscr();
// char val[20];
char *val,*ptr;
char val2='i';
char val1;
char val3='e';
int c=0;
FILE *fp;
char *mode="r";
//char *root="X";
char s1[20] ="f";
char s2[]=".@";
char s3[20]="e";
int k=0;
tree=insert1(tree,0,mode);
fp=fopen("sample.txt",mode);
while(fscanf(fp,"%s",val)!=EOF)
{
k++;
ptr=strchr(val,val2);
if(ptr!=NULL)
{
c++;
strcat(s1,s2);
//printf("%s",s1);
tree=insert1(tree,k,s1);
}
else
{
ptr=strchr(val,val3);
if(ptr!=NULL)
{
strcat(s3,s2);
tree=insert1(tree,k,s3);
}
else
{}
}
}
printf("The number of if's are %d\n",c);
double p=c-1;
double pt=pow(2,p);
printf("The number of paths's are %f",pt+1);
cout<<endl;
//cout<<"Inorer:"<<endl;
//inorder(tp[0]);
cout<<endl;
/*
cout<<"preorder"<<endl;
preorder(tp[0]);
cout<<endl;
cout<<"Postorder"<<endl;
postorder(tp[0]);
cout<<endl;
*/
display(root);
inorder(root);
//printf("%d",count);
cout<<endl;
getch();
}