#include <iostream>
using namespace std;
#include <stdio.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//由先序序列创建二叉树
BiTree createByPre()
{
char ch;
BiTree T;
scanf("%c",&ch);
if(ch=='#') return NULL;
T = new BiTNode;
T->data = ch;
T->lchild = createByPre();
T->rchild = createByPre();
return T;
}
//先序遍历二叉树
void preTraverse(BiTree T)
{
if(!T) return;
printf("%c ",T->data);
preTraverse(T->lchild);
preTraverse(T->rchild);
}
//中序遍历二叉树
void inorderTraverse(BiTree T)
{
if(!T) return;
inorderTraverse(T->lchild);
printf("%c ",T->data);
inorderTraverse(T->rchild);
}
//后序遍历二叉树
void postTraverse(BiTree T)
{
if(!T) return;
postTraverse(T->lchild);
postTraverse(T->rchild);
printf("%c ",T->data);
}
//层次遍历二叉树
void levelTraverse(BiTree T)
{
BiTree p[100]={NULL};
int front=0,rear=0;
if(T) p[rear++] = T;
while(front!=rear)
{
printf("%c ",p[front]->data);
if(p[front]->lchild) p[rear++] = p[front]->lchild;
if(p[front]->rchild) p[rear++] = p[front]->rchild;
front++;
}
}
//求二叉树的结点数
int getNodes(BiTree T)
{
if(!T) return 0;
return getNodes(T->lchild)+getNodes(T->rchild)+1;
}
//求二叉树的叶子个数
int getLeafs(BiTree T)
{
if(!T) return 0;
if(!T->lchild && !T->rchild) return 1;
return getLeafs(T->lchild)+getLeafs(T->rchild);
}
//求二叉树的深度
int depth(BiTree T)
{
int ld,rd;
if(!T) return 0;
ld = depth(T->lchild);
rd = depth(T->rchild);
return ld>rd?ld+1:rd+1;
}
int main()
{
BiTree T;
T = createByPre();
printf("先序遍历序列:");
preTraverse(T);
printf("\n");
printf("中序遍历序列:");
inorderTraverse(T);
printf("\n");
printf("后序遍历序列:");
postTraverse(T);
printf("\n");
printf("层次遍历序列:");
levelTraverse(T);
printf("\n");
printf("二叉树的结点数=%d,二叉树的叶子数=%d,深度=%d\n",getNodes(T),getLeafs(T),depth(T));
return 0;
}
using namespace std;
#include <stdio.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//由先序序列创建二叉树
BiTree createByPre()
{
char ch;
BiTree T;
scanf("%c",&ch);
if(ch=='#') return NULL;
T = new BiTNode;
T->data = ch;
T->lchild = createByPre();
T->rchild = createByPre();
return T;
}
//先序遍历二叉树
void preTraverse(BiTree T)
{
if(!T) return;
printf("%c ",T->data);
preTraverse(T->lchild);
preTraverse(T->rchild);
}
//中序遍历二叉树
void inorderTraverse(BiTree T)
{
if(!T) return;
inorderTraverse(T->lchild);
printf("%c ",T->data);
inorderTraverse(T->rchild);
}
//后序遍历二叉树
void postTraverse(BiTree T)
{
if(!T) return;
postTraverse(T->lchild);
postTraverse(T->rchild);
printf("%c ",T->data);
}
//层次遍历二叉树
void levelTraverse(BiTree T)
{
BiTree p[100]={NULL};
int front=0,rear=0;
if(T) p[rear++] = T;
while(front!=rear)
{
printf("%c ",p[front]->data);
if(p[front]->lchild) p[rear++] = p[front]->lchild;
if(p[front]->rchild) p[rear++] = p[front]->rchild;
front++;
}
}
//求二叉树的结点数
int getNodes(BiTree T)
{
if(!T) return 0;
return getNodes(T->lchild)+getNodes(T->rchild)+1;
}
//求二叉树的叶子个数
int getLeafs(BiTree T)
{
if(!T) return 0;
if(!T->lchild && !T->rchild) return 1;
return getLeafs(T->lchild)+getLeafs(T->rchild);
}
//求二叉树的深度
int depth(BiTree T)
{
int ld,rd;
if(!T) return 0;
ld = depth(T->lchild);
rd = depth(T->rchild);
return ld>rd?ld+1:rd+1;
}
int main()
{
BiTree T;
T = createByPre();
printf("先序遍历序列:");
preTraverse(T);
printf("\n");
printf("中序遍历序列:");
inorderTraverse(T);
printf("\n");
printf("后序遍历序列:");
postTraverse(T);
printf("\n");
printf("层次遍历序列:");
levelTraverse(T);
printf("\n");
printf("二叉树的结点数=%d,二叉树的叶子数=%d,深度=%d\n",getNodes(T),getLeafs(T),depth(T));
return 0;
}