给你个参考吧
#include
#include
#include
#define M 10 //假设二叉树最多的层数
typedef char ElemType;
#define QueueMaxSize 30
typedef struct BTreeNode
{
ElemType data;
struct BTreeNode* left;
struct BTreeNode* right;
}BTreeNode,*BTree;
/*二叉树的建立*/
void CreatBiTree(BTree *root)
{
char ch;
scanf("\n%c",&ch);
if(ch=='#')*root=NULL;
else{
(*root)=(BTree)malloc(sizeof(BTreeNode));
(*root)->data=ch;
CreatBiTree(&((*root)->left));
CreatBiTree(&((*root)->right));
}
}
/*先序遍历*/
void Preorder(BTree root)
{
if(root!=NULL) {
printf("%c",root->data);
Preorder(root->left);
Preorder(root->right);
}
}
/*中序遍历*/
void Inorder(BTree root)
{
if(root!=NULL) {
Inorder(root->left);
printf("%c",root->data);
Inorder(root->right);
}
}
/*后序遍历*/
void Postorder(BTree root)
{
if(root!=NULL){
Postorder(root->left);
Postorder(root->right);
printf("%c",root->data);
}
}
/*按层遍历*/
void Levelorder(BTree root)
{
BTree p;
BTreeq[QueueMaxSize];
intfront=0,rear=0;
if(root!=NULL){
rear=(rear+1)%QueueMaxSize;
q[rear]=root;
}
while(front!=rear) {
front=(front+1)%QueueMaxSize;
p=q[front];
printf("%c",p->data);
if(p->left!=NULL){
rear=(rear+1)%QueueMaxSize;
q[rear]=p->left;
}
if(p->right!=NULL){
rear=(rear+1)%QueueMaxSize;
q[rear]=p->right;
}
}
}
/*求二叉树的宽度*/
intBTreeWidth(struct BTreeNode* bt)
{
int staticn[M];
int static i=1;
int staticmax=0;
if(bt)
{
if(i==1) //若是访问根结点
{
n[i]++;
i++;
if(bt->left)
n[i]++;
if(bt->right)
n[i]++;
}
else
{ //访问子树结点
i++; //下一层结点数
if(bt->left)
n[i]++;
if(bt->right)
n[i]++;
}
if(max
i--;
BTreeWidth(bt->right);//遍历右子树
}
return max;
}
void main()
{
structBTreeNode *bt;
printf("输入读入的字符:\n");
CreatBiTree(&bt);
printf("前序遍历:\n");
Preorder(bt);
printf("\n中序遍历:\n");
Inorder(bt);
printf("\n后序遍历:\n");
Postorder(bt);
printf("\n层序遍历:\n");
Levelorder(bt);
printf("\n二叉树宽度为:%d\n",BTreeWidth(bt));
}
你给的分再*10就给你写一个。。。