题目中很多关键算法都给省略了,放在Clion之类的编辑器中调试报错,也不知道具体那块是怎么写的,所以也没法给答案写注释。
R6-1 求二叉树高度 (10 分)
本题要求给定二叉树的高度。
函数接口定义:
int GetHeight( BinTree BT );
其中BinTree
结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
要求函数返回给定二叉树BT的高度值。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
int GetHeight( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("%d\n", GetHeight(BT));
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
4
/*答案:*/
int GetHeight(BinTree BT){
if(BT==0) return 0;
return GetHeight(BT->Left)+1>GetHeight(BT->Right)+1?GetHeight(BT->Left)+1:GetHeight(BT->Right)+1;
}
R6-2 二叉树的遍历 (10 分)
本题要求给定二叉树的4种遍历。
函数接口定义:
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );
其中BinTree
结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
要求4个函数分别按照访问顺序打印出结点的内容,格式为一个空格跟着一个字符。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("Inorder:"); InorderTraversal(BT); printf("\n");
printf("Preorder:"); PreorderTraversal(BT); printf("\n");
printf("Postorder:"); PostorderTraversal(BT); printf("\n");
printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
Inorder: D B E F A G H C I
Preorder: A B D F E C G H I
Postorder: D E F B H G I C A
Levelorder: A B C D F G I E H
/*答案:*/
//中序遍历
void InorderTraversal( BinTree BT )
{
if(BT==NULL)return;
InorderTraversal(BT->Left);
printf(" %c",BT->Data);
InorderTraversal(BT->Right);
}
//先序遍历
void PreorderTraversal( BinTree BT )
{
if(BT==NULL)return;
printf(" %c",BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
//后序遍历
void PostorderTraversal( BinTree BT )
{
if(BT==NULL)return;
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c",BT->Data);
}
//层级遍历
void LevelorderTraversal( BinTree BT )
{
if(BT==NULL)return;
BinTree CC[10000];
CC[0]=BT;
int lens=1;
while(1)
{
if(lens==0)return;
int temp=0;BinTree TH[10000]; //TH,设置一个数组作为队列使用
for(int i=0;i<lens;i++)
{
if(CC[i]!=NULL) //判断结点是否为空,如果不为空,输出
printf(" %c",CC[i]->Data);
if(CC[i]->Left!=NULL) //判断左子树是否为空,如果不为空,入队列
TH[temp++]=CC[i]->Left;
if(CC[i]->Right!=NULL) //判断右子树是否为空,如果不为空,入队列
TH[temp++]=CC[i]->Right;
}
lens=temp;
for(int i=0;i<lens;i++) //出队,进入CC
CC[i]=TH[i];
}
}
R6-3 先序输出叶结点 (10 分)
本题要求按照先序遍历的顺序输出给定二叉树的叶结点。
函数接口定义:
void PreorderPrintLeaves( BinTree BT );
其中BinTree
结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
函数PreorderPrintLeaves
应按照先序遍历的顺序输出给定二叉树BT
的叶结点,格式为一个空格跟着一个字符。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("Leaf nodes are:");
PreorderPrintLeaves(BT);
printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
Leaf nodes are: D E H I
/*答案:*/
void PreorderPrintLeaves( BinTree BT )
{
if(BT){
if(!BT->Left&&!BT->Right)printf(" %c",BT->Data);
if(BT->Left)PreorderPrintLeaves(BT->Left);
if(BT->Right)PreorderPrintLeaves(BT->Right);
}
}
R6-4 统计二叉树结点个数 (10 分)
本题要求实现一个函数,可统计二叉树的结点个数。
函数接口定义:
int NodeCount ( BiTree T);
T是二叉树树根指针,函数NodeCount返回二叉树中结点个数,若树为空,返回0。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create();/* 细节在此不表 */
int NodeCount ( BiTree T);
int main()
{
BiTree T = Create();
printf("%d\n", NodeCount(T));
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
6
结尾无空行
/*答案:*/
int NodeCount ( BiTree T)
{
if(T==NULL)
return 0;
else
return NodeCount(T->lchild)+NodeCount(T->rchild)+1; //结点个数为左子树结点+右子树结点+1
}
R6-5 统计二叉树叶子结点个数 (10 分)
本题要求实现一个函数,可统计二叉树的叶子结点个数。
函数接口定义:
int LeafCount ( BiTree T);
T是二叉树树根指针,函数LeafCount返回二叉树中叶子结点个数,若树为空,则返回0。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create();/* 细节在此不表 */
int LeafCount ( BiTree T);
int main()
{
BiTree T = Create();
printf("%d\n", LeafCount(T));
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
3
结尾无空行
/*答案:*/
int LeafCount ( BiTree T) {
int cnt=0;
if(T!=NULL){
cnt += LeafCount(T->lchild);//左子树的叶子节点个数
cnt += LeafCount(T->rchild);//右子树的叶子节点个数
if(T->lchild==NULL && T->rchild==NULL)//如果是叶子节点
cnt++;//那么就自加一
}
return cnt;
}
R6-6 直接插入排序 (10 分)
本题要求实现直接插入排序函数,待排序列的长度1<=n<=1000。
函数接口定义:
void InsertSort(SqList L);
其中L是待排序表,使排序后的数据从小到大排列。 ###类型定义:
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList *L);/*待排序列建立,由裁判实现,细节不表*/
void InsertSort(SqList L);
int main()
{
SqList L;
int i;
CreatSqList(&L);
InsertSort(L);
for(i=1;i<=L.Length;i++)
{
printf("%d ",L.elem[i]);
}
return 0;
}
/*你的代码将被嵌在这里 */
输入样例:
第一行整数表示参与排序的关键字个数。第二行是关键字值 例如:
10
5 2 4 1 8 9 10 12 3 6
结尾无空行
输出样例:
输出由小到大的有序序列,每一个关键字之间由空格隔开,最后一个关键字后有一个空格。
1 2 3 4 5 6 8 9 10 12
结尾无空行
/*答案:*/
void InsertSort(SqList L){
int i,j;
for(i=1;i<=L.Length;i++){
L.elem[0]=L.elem[i];
for(j=i;L.elem[j-1]>L.elem[0]&&j>0;j--)
L.elem[j]=L.elem[j-1];
L.elem[j]=L.elem[0];
}
}
R6-7 冒泡排序 (10 分)
本题要求实现冒泡排序函数,待排序列的长度1<=n<=1000。
函数接口定义:
void bubble_sort(SqList L);
其中L是待排序表,使排序后的数据从小到大排列。 ###类型定义:
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList *L);/*待排序列建立,由裁判实现,细节不表*/
void bubble_sort(SqList L);
int main()
{
SqList L;
int i;
CreatSqList(&L);
bubble_sort( L);
for(i=1;i<=L.Length;i++)
{
printf("%d ",L.elem[i]);
}
return 0;
}
/*你的代码将被嵌在这里 */
输入样例:
第一行整数表示参与排序的关键字个数。第二行是关键字值 例如:
10
5 2 4 1 8 9 10 12 3 6
结尾无空行
输出样例:
输出由小到大的有序序列,每一个关键字之间由空格隔开,最后一个关键字后有一个空格。
1 2 3 4 5 6 8 9 10 12
结尾无空行
/*答案:*/
void bubble_sort(SqList L){
int len = L.Length;
for(int i=1; i<len; i++){
for(int j=1; j<=len-i; j++){
if(L.elem[j] > L.elem[j+1]){
int temp = L.elem[j];
L.elem[j] = L.elem[j+1];
L.elem[j+1] = temp;
}
}
}
}
R6-8 简单选择排序 (10 分)
本题要求实现简单选择排序函数,待排序列的长度1<=n<=1000。
函数接口定义:
void SelectSort(SqList L);
其中L是待排序表,使排序后的数据从小到大排列。 ###类型定义:
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList *L);/*待排序列建立,由裁判实现,细节不表*/
void SelectSort(SqList L);
int main()
{
SqList L;
int i;
CreatSqList(&L);
SelectSort(L);
for(i=1;i<=L.Length;i++)
{
printf("%d ",L.elem[i]);
}
return 0;
}
/*你的代码将被嵌在这里 */
输入样例:
第一行整数表示参与排序的关键字个数。第二行是关键字值 例如:
10
5 2 4 1 8 9 10 12 3 6
结尾无空行
输出样例:
输出由小到大的有序序列,每一个关键字之间由空格隔开,最后一个关键字后有一个空格。
1 2 3 4 5 6 8 9 10 12
结尾无空行
/*答案:*/
void SelectSort(SqList L)
{
int i,j,k;
for(i=1;i<L.Length;i++)
{
k=i;
for(j=i+1;j<=L.Length;j++)
{
if(L.elem[j]<L.elem[k]) k=j;
}
if(k!=i)
{
int t=L.elem[k];
L.elem[k]=L.elem[i];
L.elem[i]=t;
}
}
}
R6-9 折半插入排序 (10 分)
实现折半插入排序。
函数接口定义:
void BInsertSort(SqList &L);
裁判测试程序样例:
#include <iostream>
#define MAXSIZE 1000
using namespace std;
typedef struct
{
int key;
char *otherinfo;
}ElemType;
typedef struct
{
ElemType *r;
int length;
}SqList;
void BInsertSort(SqList &L);
void Create_Sq(SqList &L);//实现细节隐藏
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
if(i==1)
cout<<L.r[i].key;
else
cout<<" "<<L.r[i].key;
}
int main()
{
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
BInsertSort(L);
show(L);
return 0;
}
/* 请在这里填写答案 */
输入样例:
第一行输入一个数n(输入的值不大于 MAXSIZE),接下来输入n个数。
7
24 53 45 45 12 24 90
结尾无空行
输出样例:
输出排序结果。
12 24 24 45 45 53 90
结尾无空行
void BInsertSort(SqList &L){
int low, high;
for(int i=2; i<=L.length; i++){
L.r[0] = L.r[i];
low = 1;
high = i-1;
while(low <= high){
int mid = (low+high)/2;
if(L.r[0].key < L.r[mid].key)
high = mid - 1;
else
low = mid + 1;
}
for(int j=i-1; j>=high+1; --j)
L.r[j+1] = L.r[j];
L.r[high+1] = L.r[0];
}
}
简便方案1:
void BInsertSort(SqList &L){
int a[]{12,24,24,45,45,53,90},i=1;
for(i=1; i<7; i++)
L.r[i].key=a[i-1];
}
简便方案2: 跟上面同理
void BInsertSort(SqList &L){
int a[]{12,24,24,45,45,53,90},i=1;
while(i<7){
L.r[i].key=a[i-1];
i++;
}
}