33,007
社区成员
发帖
与我相关
我的任务
分享
# include <iostream.h>
# include <string.h>
# include <malloc.h>
# include <stdio.h>
//void exit(int );
# define maxsize 100
typedef struct node
{
char a;
struct node *left,*right;
}treenode;
typedef treenode * tree;
tree maketree(char a[],int s,int e,tree root)
{
// int i;
if(s>e)
root=NULL;
else
{
root=(tree)malloc(sizeof(treenode));
root->a=a[s];
if(a[s]=='*')
{
root->right=root->left=NULL;
}
else
{
root->left=maketree(a,s+1,s+1+(e-1-s)/2,root);
root->right=maketree(a,s+2+(e-2-s)/2,e,root);
// break;
}
}
return root;
}
void postorder(tree root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
cout <<root->a;
}
}
void perorder(tree root)
{
if(root)
{
cout <<root->a;
perorder(root->left);
perorder(root->right);
}
}
void inorder(tree root)
{
if(root)
{
inorder(root->left);
cout <<root->a;
inorder(root->right);
}
}
int leaf(tree root)
{
if(root==NULL)
return 0;
else
if(root->left==NULL && root ->right==NULL)
return 1;
else
return leaf(root->left)+leaf (root->right);
}
void main()
{
tree root=NULL;
char a[]="63154*97*8*";
//char post[maxsize]="14538796";
root=maketree(a ,0,strlen(a)-1,root);
cout <<"the perorder is as follow:";
perorder(root);
cout<<endl;
cout <<"the inorder is as follow:";
inorder(root);
cout<<endl;
cout <<"the poseorder is as follow:";
postorder(root);
cout<<endl;
//int num=leaf(root);
// cout <<"the tree num is ";
// cout <<num;
}
a
/
b
\
d
/
f
\
c
\
e
\
g
a
/ \
b c
\ \
d e
/ \
f g