我们这里以 A
B C
# E # E
# # # #
为例
1.二叉树的创建
BTNode * CreatTree()
{
BTNode *T;
char a;
scanf("%c",&a); //注意输入顺序为 AB#E##C#E## 也就是现存的是A的左子树,再存A的右子树(#表示没有存数据)
if(a=='#')
T=NULL;
else
{
T=(BTNode *)malloc(sizeof(BTNode));
T->data=a;
T->Lchild=CreatTree();//先存左子树
T->Rchild=CreatTree();
}
return T;
}
2.二叉树的三种遍历
//先序遍历
void Pre_print(BTNode *p)
{
if(p==NULL)
return ;
else
{
printf("%c ",p->data);
Pre_print(p->Lchild);
Pre_print(p->Rchild);
}
}
//中序遍历
void Mid_print(BTNode *p)
{
if(p==NULL)
return;
else
{
Mid_print(p->Lchild);
printf("%c ",p->data);
Mid_print(p->Rchild);
}
}
//后序遍历
void Post_print(BTNode *p)
{
if(p==NULL)
return ;
else
{
Post_print(p->Lchild);
Post_print(p->Rchild);
printf("%c ",p->data);
}
3.二叉树的层序遍历
//层序输出
void Level_print(BTNode *T,int level)
{
//空树或层级不合理
if(T==NULL||level<1)
return ;
if(level==1)
printf("%c ",T->data);
//左子树level-1
Level_print(T->Lchild,level-1);
//右子树层级-1
Level_print(T->Rchild,level-1);
}
//层序遍历
void Level_Traveral(BTNode *T)
{
if(T==NULL)
return ;
int depth=Depth(T);
for(int i=1;i<=depth;i++)
{
Level_print(T,i);
printf("\n");
}
}
4.求二叉树的深度(高度)
int Depth(BTNode *T)
{
int Reft_depth,Left_depth;
if(T==NULL)
return 0;
else
{
Reft_depth=Depth(T->Lchild);
Left_depth=Depth(T->Rchild);
}
return (Reft_depth>Left_depth?Reft_depth:Left_depth)+1;
}
5.求二叉树的节点数
int NodeCount(BTNode *T)
{
int num1,num2;
if(T==NULL)
return 0;
else
{
num1=NodeCount(T->Lchild);
num2=NodeCount(T->Rchild);
return (num1+num2)+1;
}
}
6.求二叉树的叶子节点数目
int LeafNode(BTNode *T)
{
int num1,num2;
if(T==NULL)
return 0;
else if(T->Lchild==NULL&&T->Rchild==NULL)
return 1;
else
{
num1=LeafNode(T->Lchild);
num2=LeafNode(T->Rchild);
return (num1+num2);
}
}
下面为全部代码
#include<stdio.h>
#include<stdlib.h>
/** 创建
1 深度
1 三种遍历
1 层序遍历
1 叶子节点个数
1 节点个数
*/
typedef struct node{
char data;
node *Rchild,*Lchild;
}BTNode;
//创建二叉树
/**
*注意输入时 先输入左子树全部数据,再输入右子树全部数据
*/
BTNode * CreatTree()
{
BTNode *T;
char a;
scanf("%c",&a);
if(a=='#')
T=NULL;
else
{
T=(BTNode *)malloc(sizeof(BTNode));
T->data=a;
T->Lchild=CreatTree();
T->Rchild=CreatTree();
}
return T;
}
//先序遍历
void Pre_print(BTNode *p)
{
if(p==NULL)
return ;
else
{
printf("%c ",p->data);
Pre_print(p->Lchild);
Pre_print(p->Rchild);
}
}
//中序遍历
void Mid_print(BTNode *p)
{
if(p==NULL)
return;
else
{
Mid_print(p->Lchild);
printf("%c ",p->data);
Mid_print(p->Rchild);
}
}
//后序遍历
void Post_print(BTNode *p)
{
if(p==NULL)
return ;
else
{
Post_print(p->Lchild);
Post_print(p->Rchild);
printf("%c ",p->data);
}
}
//求树的深度(高度)
int Depth(BTNode *T)
{
int Reft_depth,Left_depth;
if(T==NULL)
return 0;
else
{
Reft_depth=Depth(T->Lchild);
Left_depth=Depth(T->Rchild);
}
return (Reft_depth>Left_depth?Reft_depth:Left_depth)+1;
}
//层序输出
void Level_print(BTNode *T,int level)
{
//空树或层级不合理
if(T==NULL||level<1)
return ;
if(level==1)
printf("%c ",T->data);
//左子树level-1
Level_print(T->Lchild,level-1);
//右子树层级-1
Level_print(T->Rchild,level-1);
}
//层序遍历
void Level_Traveral(BTNode *T)
{
if(T==NULL)
return ;
int depth=Depth(T);
for(int i=1;i<=depth;i++)
{
Level_print(T,i);
printf("\n");
}
}
//求节点个数
int NodeCount(BTNode *T)
{
int num1,num2;
if(T==NULL)
return 0;
else
{
num1=NodeCount(T->Lchild);
num2=NodeCount(T->Rchild);
return (num1+num2)+1;
}
}
//求叶子节点数
int LeafNode(BTNode *T)
{
int num1,num2;
if(T==NULL)
return 0;
else if(T->Lchild==NULL&&T->Rchild==NULL)
return 1;
else
{
num1=LeafNode(T->Lchild);
num2=LeafNode(T->Rchild);
return (num1+num2);
}
}
int main()
{
BTNode *T;
T=CreatTree();
printf("先序:");
Pre_print(T);
printf("\n");
printf("中序:");
Mid_print(T);
printf("\n");
printf("后序:");
Post_print(T);
printf("\n");
printf("树的深度:%d\n",Depth(T));
printf("树的节点个数:%d\n",NodeCount(T));
printf("叶子节点个数:%d\n",LeafNode(T));printf("层序遍历\n");
Level_Traveral(T);
return 0;
}
运行结果: