提交 8d3d4849 创建 作者: 宋海霞's avatar 宋海霞

template

上级
流水线 #486 已失败 于阶段
/Debug
/Release
*.bak
image: "registry.cn-hangzhou.aliyuncs.com/engintime/ubuntu_16.04_cpp:latest"
stages:
- make
- case1
- teacher-check
variables:
TEMPLATE_REPO: "engintime/ds-lab/Project-Template/Lab022.git"
.codecode-runner: &codecode-runner
tags:
- ubuntu-16.04
- short-job
make:
stage: make
<<: *codecode-runner
script:
- make
- execscore.sh 40
only:
- master
case1:
stage: case1
<<: *codecode-runner
script:
- make
- ./app > user_output.txt
- diff output.txt user_output.txt -b -B -y -i --suppress-common-lines
- execscore.sh 100
only:
- master
teacher-check:
stage: teacher-check
<<: *codecode-runner
script:
- make
- ./app
- git clone ${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEMPLATE_REPO} template
- diff template/.gitlab-ci.yml .gitlab-ci.yml -b -B -y -i --suppress-common-lines
- diff template/output.txt output.txt -b -B -y -i --suppress-common-lines
- fileidentity.sh
only:
- master
when: manual
allow_failure: false
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="BinarySearchTree" SubjectID="cf4dda31-1275-49ef-9b0f-36a6eff372e4" ProjectTemplateID="aaafe4d7-3341-4028-baa9-d8217450f3bd">
<Configurations>
<Configuration Name="Debug">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1" AdditionalOptions="-std=c99"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" AdditionalDependencies="&quot;$(DSLInstallDir)Dump\lib\BinarySearchTree_Demo.o&quot;"/>
<Tool Name="PostBuildEventTool"/>
<VisualContext>
<WatchPoints>
<WatchPoint FunctionName="CreateSearchTree" ObserverID="D00B5F31-F80A-4092-AE57-F0B264FE5EC7">
</WatchPoint>
</WatchPoints>
</VisualContext>
</Configuration>
<Configuration Name="Release">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
<File RelativePath=".\BinarySearchTree.h">
</File>
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\main.c">
</File>
</Filter>
<File RelativePath=".\makefile">
</File>
<File RelativePath=".\output.txt">
</File>
<File RelativePath=".\readme.md">
</File>
</Files>
</OSLProject>
#ifndef _BINARYSEARCHTREE_H_
#define _BINARYSEARCHTREE_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
#include <stdlib.h>
//
// 在此处定义数据结构
//
typedef int KeyType; // 关键码字段类型
typedef enum _TreeNodeType{
TREE_ROOT, // 根节点
TREE_LEFT, // 左孩子
TREE_RIGHT // 右孩子
}TreeNodeType;
typedef struct BinSearchNode{
KeyType Key; // 节点的关键码字段
struct BinSearchNode* lchild; // 左孩子指针
struct BinSearchNode* rchild; // 右孩子指针
}BinSearchNode, *PBinSearchNode;
typedef struct BinSearchNodeExtra{
TreeNodeType enumType; // 节点类型
int flag; // 构造树结构依据的二进制
int nLevel; // 节点在二叉树中的层级。根节点是 1。
}BinSearchNodeExtra, *PBinSearchNodeExtra;
typedef struct BinSearchNodeEntry
{
BinSearchNodeExtra nodeExtra;
BinSearchNode node;
}BinSearchNodeEntry, *PBinSearchNodeEntry;
typedef struct BinSearchNode* PBinSearchTree; // 二叉排序树指针
//
// 在此处声明函数
//
int Search(PBinSearchTree* pTree, KeyType Key, PBinSearchNode* position);
int CreateSearchTree(PBinSearchTree* pTree, KeyType* Key, int Length);
void DeleteTree(PBinSearchTree pTree);
int CopyNode(PBinSearchTree* pTree, int nLevel, TreeNodeType nodeType, int flag);
void OutputResult(PBinSearchTree* pTree);
//
// 在此处声明全局变量
//
#endif /* _BINARYSEARCHTREE_H_ */
#include "BinarySearchTree.h"
BinSearchNodeEntry nodeArray[50]; // 二叉排序树节点序列
int count = 0; // 节点的数量
int main(int argc, char* argv[])
{
PBinSearchTree pTree; // 二叉排序树指针
//
// 初始化关键码数组
//
KeyType Key[] = {18, 73, 10, 10, 5, 4, 6, 99, 27, 41, 51, 45, 32, 25, 54};
int Length = sizeof(Key)/sizeof(Key[0]);
//
// 构造二叉排序树
//
CreateSearchTree(&pTree, Key, Length);
//
// 输出结果
//
OutputResult(&pTree);
//
// 销毁二叉排序树
//
DeleteTree(pTree);
return 0;
}
/*
功能:
在二叉排序树中搜索以 Key 为关键码的节点。
参数:
pTree -- 二叉排序树指针的指针。
Key -- 关键码。
position -- 节点指针的指针类型,用于返回搜索到的节点指针。
返回值:
搜索到以 Key 为关键码的节点返回 1,否则返回 0
*/
int Search(PBinSearchTree* pTree, KeyType Key, PBinSearchNode* position)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
创建二叉排序树节点。
参数:
Key -- 关键码。
返回值:
二叉排序树节点指针。
*/
BinSearchNode* CreateNode(KeyType Key)
{
BinSearchNode* pNode = (PBinSearchNode)malloc(sizeof(BinSearchNode));
pNode->Key = Key;
pNode->lchild = NULL;
pNode->rchild = NULL;
return pNode;
}
/*
功能:
构造二叉排序树。
参数:
pTree -- 二叉排序树指针的指针。
Key -- 关键码数组
Length -- 数组长度。
返回值:
构造成功返回 1
构造失败返回 0
*/
int CreateSearchTree(PBinSearchTree* pTree, KeyType* Key, int Length)
{
int i; // 游标
*pTree = NULL; // 初始化二叉排序树指针
PBinSearchNode pNode, position; // 二叉排序树节点指针
//
// TODO: 在此添加代码
//
return 0;
}
/*
功能:
销毁二叉排序树。
参数:
pTree -- 二叉排序树的指针。
返回值:
*/
void DeleteTree(PBinSearchTree pTree)
{
//
// 利用递归实现后序遍历算法
//
if(pTree != NULL)
{
DeleteTree(pTree->lchild);
DeleteTree(pTree->rchild);
free(pTree);
}
}
/*
功能:
获取二叉排序树节点。使用递归实现先序遍历。
参数:
pTree -- 节点的地址
nLevel -- 节点所在的层级。根节点为 1。
nodeType -- 节点类型
flag -- 构造树结构依据的二进制
返回值:
如果根据地址构造的表达式可以计算成功,返回 1。否则,返回 0。
*/
int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag)
{
int temp1, temp2;
int i;
// 递归在这里结束
if(NULL == *pTree)
return 1;
i = count;
nodeArray[i].node.Key = (*pTree)->Key;
nodeArray[i].node.lchild = (*pTree)->lchild;
nodeArray[i].node.rchild = (*pTree)->rchild;
nodeArray[i].nodeExtra.enumType = nodeType;
nodeArray[i].nodeExtra.nLevel = nLevel;
nodeArray[i].nodeExtra.flag = flag;
++count;
temp2 = temp1 = flag;
if(nodeType == TREE_ROOT)
temp1 = flag;
else if (flag == 0)
temp1 = nodeType == TREE_LEFT ? (flag + 1) << 1 : flag << 1;
else
temp1 = flag << 1;
temp2 = temp1;
// 递归计算左孩子和右孩子
if(!CopyNode(&(nodeArray[i].node.lchild), nLevel+1, TREE_LEFT, temp1))
return 0;
if(!CopyNode(&(nodeArray[i].node.rchild), nLevel+1, TREE_RIGHT, temp2))
return 0;
return 1;
}
/*
功能:
输出结果。
参数:
pTree -- 二叉排序树的指针。
返回值:
*/
void OutputResult(PBinSearchTree* pTree)
{
int i, flag, deep, j;
if(CopyNode(pTree, 1, TREE_ROOT, 0) == 0)
{
printf("二叉排序树构造失败\n");
return;
}
for(i = 0; i < count; i++)
{
flag = nodeArray[i].nodeExtra.flag;
deep = nodeArray[i].nodeExtra.nLevel - 1;
for(j=deep-1; j>0; j--)
{
if(flag & (1 << j))
printf(" | ");
else if(deep > 1)
printf(" ");
}
if(deep > 0)
printf(" +---");
printf("%2d ",nodeArray[i].node.Key);
switch (nodeArray[i].nodeExtra.enumType)
{
case TREE_LEFT:
printf("-L");
break;
case TREE_RIGHT:
printf("-R");
break;
case TREE_ROOT:
default:
;
}
printf("\n");
}
}
app:main.o
gcc -o app main.o
main.o:main.c BinarySearchTree.h
gcc -c -w -O3 -std=c99 -fsigned-char main.c -o main.o
18
+---10 -L
| +--- 5 -L
| +--- 4 -L
| +--- 6 -R
+---73 -R
+---27 -L
| +---25 -L
| +---41 -R
| +---32 -L
| +---51 -R
| +---45 -L
| +---54 -R
+---99 -R
# 阅读实验源代码
**main.c文件**
在main函数中首先定义了二叉排序树的指针,初始化了用于构造二叉排序树的关键码数组,然后调用二叉排序树的构造函数CreateSearchTree构造二叉排序树,最后销毁了二叉排序树。
在main函数的后面,定义了二叉排序树的构造函数CreateSearchTree,此函数的函数体还不完整,留给读者完成。
**BinarySearchTree.h文件**
定义了与二叉排序树相关的数据结构并声明了相关的操作函数。
# 在演示模式下调试项目
**按照下面的步骤调试项目:**
1. 按F7生成项目。
2. 在演示模式下,按F5启动调试项目。程序会在观察点函数的开始位置中断。
3. 重复按F5,直到调试过程结束。
在调试的过程中,每执行“演示流程”窗口中的一行后,仔细观察“可视化数据”窗口内容所发生的变化,理解二叉排序树构造的执行过程。“可视化数据”窗口显示的数据信息(如下图所示),包括:
- 用于构造二叉排序树的关键码数组和游标i。
- 二叉排序树的详细信息。
a) 包括了二叉排序树结点的值和地址,并且标出了结点在构建二叉排序树的序号。
b) 在调试的过程中,游标指向了当前的节点。
![二叉排序树的构造](./img/22.png)
# 编写源代码并通过验证
**按照下面的步骤继续实验:**
1. 为CreateSearchTree函数编写源代码,注意,尽量使用已定义的局部变量。
2. 按F7生成项目。如果生成失败,根据“输出”窗口中的提示信息修改源代码中的语法错误。
3. 按Alt+F5启动验证。如果验证失败,可以使用“输出”窗口中的“比较”功能,或者在“非演示模式”下按F5启动调试后重复按F10单步调试读者编写的源代码,从而定位错误的位置,然后回到步骤1。
# 教师参考答案
访问教师参考答案[domain relative url](engintime/ds-lab/teachers-packet/Lab022.git)
\ No newline at end of file
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论