#include<iostream>
using namespace std;
typedef struct TreeNode
{
int data;
struct TreeNode *lchild;
struct TreeNode *rchild;
}TreeNode,*pTreeNode;
int Data[100];
int DataIndex=0;
int LeafNum;
int NodeNum;
void CreateTree(pTreeNode &tree,int data)
{
TreeNode *pNew;
if(data==0)
{
tree = NULL;
}
else
{
pNew = new TreeNode();
pNew->data = data;
pNew->lchild = NULL;
pNew->rchild = NULL;
tree = pNew;
CreateTree(tree->lchild,Data[++DataIndex]);
CreateTree(tree->rchild,Data[++DataIndex]);
}
}
void PreTree(pTreeNode ptree)
{
if(ptree == NULL)
{
return;
}
else
{
if(NULL == ptree->lchild&&NULL == ptree->rchild)
{
LeafNum++;
cout<<"Leaf:"<<ptree->data<<" "<<endl;
}
else
{
NodeNum++;
cout<<"Node:"<<ptree->data<<" "<<endl;
}
PreTree(ptree->lchild);
PreTree(ptree->rchild);
}
}
int main(void)
{
int data;
int i = 0;
pTreeNode ptree=NULL;
while(1)
{
cout<<"please enter tree num:"<<endl;
while(cin>>data,data>=0)
{
Data[i++] = data;
}
CreateTree(ptree,Data[0]);
PreTree(ptree);
cout<<"Sum of Leaf Num:"<<LeafNum<<endl;
cout<<"Sum of Node Num:"<<NodeNum<<endl;
LeafNum = 0;
NodeNum = 0;
free(ptree);
i = 0;
memset(Data,0,sizeof(Data));
DataIndex = 0;
}
return 0;
}