第1关:基于二叉链表的二叉树的遍历
任务描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写三个递归算法分别实现二叉树的先序、中序和后序遍历。
编程要求
输入
多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出三行,为二叉树的先序、中序和后序序列。
测试说明
平台会对你编写的代码进行测试:
测试输入:
abcd00e00f00ig00h00
abd00e00cf00g00
0
预期输出:
abcdefigh
dcebfagih
decfbghia
abdecfg
dbeafcg
debfgca
上答案:
#include<iostream>
#include<string.h>
using namespace std;
int flag;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{//先序建立二叉树
if(S[i]=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=S[i];
CreateBiTree(T->lchild,S,++i);
CreateBiTree(T->rchild,S,++i);
}
}
void PreOrderTraverse(BiTree T)
{//二叉树的先序遍历
/**************begin************/
if(T==NULL)
{
return ;
}
printf("%c",T->data);
if(T->lchild!=NULL)
{
PreOrderTraverse(T->lchild);
}
if(T->rchild!=NULL)
{
PreOrderTraverse(T->rchild);
}
/**************end************/
}
void InOrderTraverse(BiTree T)
{//二叉树的中序遍历
/**************begin************/
if(T!=NULL){
InOrderTraverse(T->lchild);
printf("%c",T->data);
InOrderTraverse(T->rchild);
}
/**************end************/
}
void PostOrderTraverse(BiTree T)
{//二叉树的后序遍历
/**************begin************/
if(T==NULL)
{
return ;
}
if(T->lchild!=NULL)
{
PostOrderTraverse(T->lchild);
}
if(T->rchild!=NULL)
{
PostOrderTraverse(T->rchild);
}
printf("%c",T->data);
/**************end************/
}
int main()
{
char S[100];
while(cin>>S)
{
if(strcmp(S,"0")==0) break;
int i=-1;
BiTree T;
CreateBiTree(T,S,++i);
PreOrderTraverse(T);
cout<<endl;
InOrderTraverse(T);
cout<<endl;
PostOrderTraverse(T);
cout<<endl;
}
return 0;
}
第2关:基于二叉链表的二叉树结点个数的统计
任务描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写三个递归算法对二叉树的结点(度为0、1、2)个数进行统计。
编程要求
输入
多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据输出一行,每行三个数分别为二叉树的度为0、1、2的结点个数。每两个数用空格分隔。
测试说明
平台会对你编写的代码进行测试:
测试输入:
abcd00e00f00ig00h00
abd00e00cf00g00
0
预期输出:
5 0 4
4 0 3
上答案:
#include<iostream>
#include<string.h>
using namespace std;
int a,b,c;//a、b、c分别表示度为0、1、2的结点个数
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{//先序建立二叉树
if(S[i]=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=S[i];
CreateBiTree(T->lchild,S,++i);
CreateBiTree(T->rchild,S,++i);
}
}
void Count(BiTree T)
{//二叉树结点个数的统计
/**************begin************/
if(T==NULL)
{
return;
}
else
{
if(T->lchild&&T->rchild)
c++;
else if(T->lchild||T->rchild)
{b++;}
else {a++;}
Count(T->lchild);
Count(T->rchild);
}
/**************end************/
}
int main()
{
char S[100];
while(cin>>S)
{
if(strcmp(S,"0")==0) break;
a=b=c=0;
int i=-1;
BiTree T;
CreateBiTree(T,S,++i);
Count(T);
cout<<a<<" "<<b<<" "<<c<<endl;
}
return 0;
}
第3关:基于二叉链表的二叉树高度的计算
任务描述
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,,编写递归算法计算二叉树的高度。
编程要求
输入
多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。
输出
每组数据分别输出一行,为二叉树的高度。
测试说明
平台会对你编写的代码进行测试:
测试输入:
abcd00e00f00ig00h00
abd00e00cf00g00
0
预期输出:
4
3
上答案:
#include<iostream>
#include <string.h>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{//先序建立二叉树
if(S[i]=='0')
T=NULL;
else
{
T=new BiTNode;
T->data=S[i];
CreateBiTree(T->lchild,S,++i);
CreateBiTree(T->rchild,S,++i);
}
}
int Depth(BiTree T)
{//二叉树高度的计算
/**************begin************/
int m,n;
if(T==NULL)
{
return 0;
}
else
{
m=Depth(T->lchild);
n=Depth(T->rchild);
if(m>n)
{
return m+1;
}
else return n+1;
}
/**************end************/
}
int main()
{
char S[100];
while(cin>>S)
{
if(strcmp(S,"0")==0) break;
int i=-1;
BiTree T;
CreateBiTree(T,S,++i);
cout<<Depth(T)<<endl;
}
return 0;
}
第4关:二叉树的WPL计算
任务描述
二叉树的带权路径长度(WPL)是二叉树中所有叶结点的带权路径长度之和。给定一棵二叉树T, 采用二叉链表存储,结点结构为:left weight right,其中叶结点的weight域保存该结点的非负权值。设root为指向T的根结点的指针,请设计求T的WPL的算法。
编程要求
输入
多组数据,每组数据一行,为一个二叉树的先序序列(序列中元素为0时,表示该结点为空,每两个元素之间用空格隔开)。当输入只有一个0时,输入结束。
输出
每组数据输出一行,为该二叉树的WPL。
测试说明
平台会对你编写的代码进行测试:
测试输入:
1 1 0 0 1 0 0
1 2 1 0 0 0 0
0
预期输出:
2
2
上答案:
#include<iostream>
using namespace std;
typedef struct BiTNode
{
int weight;
struct BiTNode *left,*right;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{//先序建立二叉树
int x;
cin>>x;
if(x==0) T=NULL;
else
{
T=new BiTNode;
T->weight=x;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
}
int WPL(BiTree &T,int d)
{//求二叉树T的带权路径长度
/**************begin************/
int wpl=0;
if(T!=NULL)
{
if(T->left==NULL&&T->right==NULL)
wpl+=d*T->weight;
wpl+=WPL(T->left,d+1);
wpl+=WPL(T->right,d+1);
}
return wpl;
/**************end************/
}
int main()
{
while(1)
{
BiTree T;
CreateBiTree(T);
if(!T) break;
int d=0; //调用时T指向二叉树的根结点,d为0
cout<<WPL(T,d)<<endl;
}
return 0;
}