#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef char TElemType;
typedef struct Node
{
TElemType data;
struct Node *lchild, *rchild;
} BiTNode,*BiTree;
int count=0; //叶子节点数
char a[100];//叶子节点
int depth=0;//二叉树的高度
//二叉树链式存储结构的操作函数
void Create(BiTree *root)
{
TElemType ch;
scanf("%c",&ch);
if (ch=='#')
*root = NULL;
else
{
*root = (BiTree)malloc(sizeof(BiTNode));
(*root)->data = ch; // 生成根结点
Create(&(*root)->lchild); // 构造左子树
Create(&(*root)->rchild); // 构造右子树
}
}
void Visit(TElemType ch)
{
printf("%c",ch);
}
//先序遍历
void PreOrder ( BiTree root)
{
if(root==NULL)
return ;
else
{ Visit(root->data); //访问根结点
PreOrder(root->lchild);
PreOrder (root->rchild);
}
}
//中序遍历
void InOrder(BiTree root)
{
if(root==NULL)
return;
InOrder(root->lchild);
Visit(root->data); //输出
InOrder(root->rchild);
}
//中序遍历
void PostOrder(BiTree root)
{
if(root==NULL)
return;
PostOrder(root->lchild);
PostOrder(root->rchild);
Visit(root->data); //输出
}
//先序
void CountLeaf1(BiTree root)
{
if (root!=NULL)
{
if (root->lchild==NULL && root->rchild==NULL)
{
count=count+1; //全局算法--节点数
a[count-1]=root->data;
}
printf("%c",root->data);
CountLeaf1(root->lchild);
CountLeaf1(root->rchild);
}
}
//中序
void CountLeaf2(BiTree root)
{
if (root!=NULL)
{
CountLeaf2(root->lchild);
if (root->lchild==NULL && root->rchild==NULL)
{
count=count+1;
a[count-1]=root->data;
}
printf("%c",root->data);
CountLeaf2(root->rchild);
}
}
//后序
void CountLeaf3(BiTree root)
{
if (root!=NULL)
{
CountLeaf3(root->lchild);
CountLeaf3(root->rchild);
if (root->lchild==NULL && root->rchild==NULL)
{
count=count+1;
a[count-1]=root->data;
}
printf("%c",root->data);
}
}
//统计叶子节点节点数目
int CountLeaf(BiTree root)
{ int count;
if (root==NULL)
count=0;
else
{
if (root->lchild==NULL && root->rchild==NULL)
count=1;
else
{
count=CountLeaf(root->lchild)+CountLeaf(root->rchild);
}
}
return count;
}
//求二叉树的高度
void HighBitree(BiTree root,int h)
{
if (root!=NULL)
{
if(h>depth) depth=h;
HighBitree(root->lchild,h+1);
HighBitree(root->rchild,h+1);
}
}
void swap(BiTree root)
{
BiTree temp;
if(root)
{
swap(root->lchild);//递归交换结点
temp = root->lchild;//交换左右子树
root->lchild = root->rchild;
root->rchild = temp;
swap(root->rchild);//递归交换结点
}
}
void show(BiTree root)
{
int i,b;
printf("1.输出(先、中、后序)\n2.输出叶子节点\n3.叶子节点数目\n4.二叉树的高度\n5.左右子树交换\n输入你要操作的序号:\n");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("\n递归前序遍历输出为:");
PreOrder(root);
printf("\n递归中序遍历输出为:");
InOrder(root);
printf("\n递归后序遍历输出为:");
PostOrder(root);
printf("\n");
printf("\n按任意键继续...\n");
getch();
show(root);
break;
}
case 2:
{
CountLeaf1(root);
printf("\n先序:叶子节点有%d个他们分别是",count);
count=0;
while (a[count]!='\0')
{
printf("%c",a[count]);
count++;
}
printf("\n");
count=0;
CountLeaf2(root);
printf("\n中序:叶子节点有%d个他们分别是",count);
count=0;
while (a[count]!='\0')
{
printf("%c",a[count]);
count++;
}
printf("\n");
count=0;
CountLeaf3(root);
printf("\n后序:叶子节点有%d个他们分别是",count);
count=0;
while (a[count]!='\0')
{
printf("%c",a[count]);
count++;
}
printf("\n");
printf("\n按任意键继续...\n");
getch();
show(root);
break;
}
case 3:
{
b=CountLeaf(root);
printf("\n叶子节点数目一共有%d个\n",b);
printf("\n");
printf("\n按任意键继续...\n");
getch();
show(root);
break;
}
case 4:
{
int h=1;
HighBitree(root,h);
printf("\n二叉树的高度是%d层\n",depth);
printf("\n");
printf("\n按任意键继续...\n");
getch();
show(root);
break;
}
case 5:
{
swap(root);
printf("\n交换后前序遍历输出为:");
PreOrder(root);
printf("\n");
printf("\n按任意键继续...\n");
getch();
show(root);
break;
}
default:
{
printf("\n输入不正确请重新输入\n");
printf("\n按任意键继续...\n");
getch();
show(root);
}
}
}
int main()
{
BiTree root= NULL;
printf("请输入一个字符串,例如:AB#D##C#E##: ");
Create(&root);
show(root);
return 0;
}
| 日期 |
学习方法 |
学习时间 |
新增代码行 |
知识总结 |
| 星期一 |
看慕课视频 |
2h |
0 |
|
| 星期二 |
|
|
|
|
| 星期三 |
|
|
|
|
| 星期四 |
看慕课视频 |
3h |
100 |
|
| 星期五 |
看慕课视频 |
3h |
0 |
|
| 星期六 |
看课堂视频回放 |
2h |
0 |
|
| 星期日 |
看课堂视频回放 |
2h |
150 |
|
| 总计 |
看慕课和课堂视频 |
12h |
250 |
|