Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab022
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
DS Lab Group
实验项目模板
Lab022
提交
8366cab2
提交
8366cab2
5月 20, 2019
创建
作者:
宋海霞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify
上级
8ac45b3b
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
60 行增加
和
60 行删除
+60
-60
main.c
main.c
+60
-60
没有找到文件。
main.c
浏览文件 @
8366cab2
#include "BinarySearchTree.h"
#include "BinarySearchTree.h"
BinSearchNodeEntry
nodeArray
[
50
];
//
二叉排序树节点序列
BinSearchNodeEntry
nodeArray
[
50
];
//
Binary sort tree node sequence
int
count
=
0
;
//
节点的数量
int
count
=
0
;
//
Nodal number
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
PBinSearchTree
pTree
;
//
二叉排序树指针
PBinSearchTree
pTree
;
//
Initializes the binary sort tree pointer
//
//
//
初始化关键码数组
//
Initializes the key array
//
//
KeyType
Key
[]
=
{
18
,
73
,
10
,
10
,
5
,
4
,
6
,
99
,
27
,
41
,
51
,
45
,
32
,
25
,
54
};
KeyType
Key
[]
=
{
18
,
73
,
10
,
10
,
5
,
4
,
6
,
99
,
27
,
41
,
51
,
45
,
32
,
25
,
54
};
int
Length
=
sizeof
(
Key
)
/
sizeof
(
Key
[
0
]);
int
Length
=
sizeof
(
Key
)
/
sizeof
(
Key
[
0
]);
//
//
//
构造二叉排序树
//
Construct a binary sort tree
//
//
CreateSearchTree
(
&
pTree
,
Key
,
Length
);
CreateSearchTree
(
&
pTree
,
Key
,
Length
);
//
//
//
输出结果
//
output result
//
//
OutputResult
(
&
pTree
);
OutputResult
(
&
pTree
);
//
//
//
销毁二叉排序树
//
Destroy the binary sort tree
//
//
DeleteTree
(
pTree
);
DeleteTree
(
pTree
);
...
@@ -33,35 +33,35 @@ int main(int argc, char* argv[])
...
@@ -33,35 +33,35 @@ int main(int argc, char* argv[])
}
}
/*
/*
功能:
function:
在二叉排序树中搜索以 Key 为关键码的节点。
Search the binary sort tree for nodes whose Key is the Key.
参数:
parameter:
pTree --
二叉排序树指针的指针。
pTree --
Pointer to binary sort tree pointer.
Key --
关键码。
Key --
The key code.
position --
节点指针的指针类型,用于返回搜索到的节点指针。
position --
Pointer type of the node pointer used to return the searched node pointer.
返回值:
returned value:
搜索到以 Key 为关键码的节点返回 1,否则返回
0
Returns 1 if a node with Key is found; otherwise returns
0
*/
*/
int
Search
(
PBinSearchTree
*
pTree
,
KeyType
Key
,
PBinSearchNode
*
position
)
int
Search
(
PBinSearchTree
*
pTree
,
KeyType
Key
,
PBinSearchNode
*
position
)
{
{
//
//
// TODO:
在此添加代码
// TODO:
Add the code here
//
//
}
}
/*
/*
功能:
function:
创建二叉排序树节点。
Create a binary sort tree node.
参数:
parameter:
Key --
关键码。
Key --
The key code.
返回值:
returned value:
二叉排序树节点指针。
Binary sort tree node pointer.
*/
*/
BinSearchNode
*
CreateNode
(
KeyType
Key
)
BinSearchNode
*
CreateNode
(
KeyType
Key
)
{
{
...
@@ -75,46 +75,46 @@ BinSearchNode* CreateNode(KeyType Key)
...
@@ -75,46 +75,46 @@ BinSearchNode* CreateNode(KeyType Key)
}
}
/*
/*
功能:
function:
构造二叉排序树。
Construct a binary sort tree.
参数:
parameter:
pTree --
二叉排序树指针的指针。
pTree --
Pointer to binary sort tree pointer.
Key --
关键码数组
Key --
Key array
Length --
数组长度。
Length --
The length of the array.
返回值:
returned value:
构造成功返回 1
The construct returns 1 successfully
构造失败返回
0
Construction failure returns
0
*/
*/
int
CreateSearchTree
(
PBinSearchTree
*
pTree
,
KeyType
*
Key
,
int
Length
)
int
CreateSearchTree
(
PBinSearchTree
*
pTree
,
KeyType
*
Key
,
int
Length
)
{
{
int
i
;
//
游标
int
i
;
//
The cursor
*
pTree
=
NULL
;
//
初始化二叉排序树指针
*
pTree
=
NULL
;
//
Initializes the binary sort tree pointer
PBinSearchNode
pNode
,
position
;
//
二叉排序树节点指针
PBinSearchNode
pNode
,
position
;
//
Binary sort tree node pointer
//
//
// TODO:
在此添加代码
// TODO:
Add the code here
//
//
return
0
;
return
0
;
}
}
/*
/*
功能:
function:
销毁二叉排序树。
Destroy the binary sort tree.
参数:
parameter:
pTree --
二叉排序树的指针。
pTree --
Pointer to a binary sort tree.
返回值:
returned value:
无
nothing
*/
*/
void
DeleteTree
(
PBinSearchTree
pTree
)
void
DeleteTree
(
PBinSearchTree
pTree
)
{
{
//
//
//
利用递归实现后序遍历算法
//
The sequential traversal algorithm is implemented by recursion
//
//
if
(
pTree
!=
NULL
)
if
(
pTree
!=
NULL
)
{
{
...
@@ -128,25 +128,25 @@ void DeleteTree(PBinSearchTree pTree)
...
@@ -128,25 +128,25 @@ void DeleteTree(PBinSearchTree pTree)
/*
/*
功能:
function:
获取二叉排序树节点。使用递归实现先序遍历。
Get the binary sort tree node.Use recursion to implement preorder traversal.
参数:
parameter:
pTree --
节点的地址
pTree --
Node address
nLevel --
节点所在的层级。根节点为 1。
nLevel --
The hierarchy of nodes.The root node is 1.
nodeType --
节点类型
nodeType --
The node type
flag --
构造树结构依据的二进制
flag --
The binary on which the tree is constructed
返回值:
returned value:
如果根据地址构造的表达式可以计算成功,返回 1。否则,返回 0。
If the expression constructed from the address evaluates successfully, return 1. Otherwise, return 0.
*/
*/
int
CopyNode
(
PBinSearchTree
*
pTree
,
int
nLevel
,
TreeNodeType
nodeType
,
int
flag
)
int
CopyNode
(
PBinSearchTree
*
pTree
,
int
nLevel
,
TreeNodeType
nodeType
,
int
flag
)
{
{
int
temp1
,
temp2
;
int
temp1
,
temp2
;
int
i
;
int
i
;
//
递归在这里结束
//
The recursion ends here
if
(
NULL
==
*
pTree
)
if
(
NULL
==
*
pTree
)
return
1
;
return
1
;
i
=
count
;
i
=
count
;
...
@@ -168,7 +168,7 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
...
@@ -168,7 +168,7 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
temp1
=
flag
<<
1
;
temp1
=
flag
<<
1
;
temp2
=
temp1
;
temp2
=
temp1
;
//
递归计算左孩子和右孩子
//
Recursively compute left child and right child
if
(
!
CopyNode
(
&
(
nodeArray
[
i
].
node
.
lchild
),
nLevel
+
1
,
TREE_LEFT
,
temp1
))
if
(
!
CopyNode
(
&
(
nodeArray
[
i
].
node
.
lchild
),
nLevel
+
1
,
TREE_LEFT
,
temp1
))
return
0
;
return
0
;
...
@@ -179,14 +179,14 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
...
@@ -179,14 +179,14 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
}
}
/*
/*
功能:
function:
输出结果。
output result.
参数:
parameter:
pTree --
二叉排序树的指针。
pTree --
Pointer to a binary sort tree.
返回值:
returned value:
无
nothing
*/
*/
void
OutputResult
(
PBinSearchTree
*
pTree
)
void
OutputResult
(
PBinSearchTree
*
pTree
)
...
@@ -194,7 +194,7 @@ void OutputResult(PBinSearchTree* pTree)
...
@@ -194,7 +194,7 @@ void OutputResult(PBinSearchTree* pTree)
int
i
,
flag
,
deep
,
j
;
int
i
,
flag
,
deep
,
j
;
if
(
CopyNode
(
pTree
,
1
,
TREE_ROOT
,
0
)
==
0
)
if
(
CopyNode
(
pTree
,
1
,
TREE_ROOT
,
0
)
==
0
)
{
{
printf
(
"
二叉排序树构造失败
\n
"
);
printf
(
"
Binary sort tree construct failed
\n
"
);
return
;
return
;
}
}
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论