提交 8366cab2 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 8ac45b3b
#include "BinarySearchTree.h"
BinSearchNodeEntry nodeArray[50]; // 二叉排序树节点序列
int count = 0; // 节点的数量
BinSearchNodeEntry nodeArray[50]; // Binary sort tree node sequence
int count = 0; // Nodal number
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};
int Length = sizeof(Key)/sizeof(Key[0]);
//
// 构造二叉排序树
// Construct a binary sort tree
//
CreateSearchTree(&pTree, Key, Length);
//
// 输出结果
// output result
//
OutputResult(&pTree);
//
// 销毁二叉排序树
// Destroy the binary sort tree
//
DeleteTree(pTree);
......@@ -33,35 +33,35 @@ int main(int argc, char* argv[])
}
/*
功能:
在二叉排序树中搜索以 Key 为关键码的节点。
function:
Search the binary sort tree for nodes whose Key is the Key.
参数:
pTree -- 二叉排序树指针的指针。
Key -- 关键码。
position -- 节点指针的指针类型,用于返回搜索到的节点指针。
parameter:
pTree -- Pointer to binary sort tree pointer.
Key -- The key code.
position -- Pointer type of the node pointer used to return the searched node pointer.
返回值:
搜索到以 Key 为关键码的节点返回 1,否则返回 0
returned value:
Returns 1 if a node with Key is found; otherwise returns 0
*/
int Search(PBinSearchTree* pTree, KeyType Key, PBinSearchNode* position)
{
//
// TODO: 在此添加代码
// TODO: Add the code here
//
}
/*
功能:
创建二叉排序树节点。
function:
Create a binary sort tree node.
参数:
Key -- 关键码。
parameter:
Key -- The key code.
返回值:
二叉排序树节点指针。
returned value:
Binary sort tree node pointer.
*/
BinSearchNode* CreateNode(KeyType Key)
{
......@@ -75,46 +75,46 @@ BinSearchNode* CreateNode(KeyType Key)
}
/*
功能:
构造二叉排序树。
function:
Construct a binary sort tree.
参数:
pTree -- 二叉排序树指针的指针。
Key -- 关键码数组
Length -- 数组长度。
parameter:
pTree -- Pointer to binary sort tree pointer.
Key -- Key array
Length -- The length of the array.
返回值:
构造成功返回 1
构造失败返回 0
returned value:
The construct returns 1 successfully
Construction failure returns 0
*/
int CreateSearchTree(PBinSearchTree* pTree, KeyType* Key, int Length)
{
int i; // 游标
*pTree = NULL; // 初始化二叉排序树指针
PBinSearchNode pNode, position; // 二叉排序树节点指针
int i; // The cursor
*pTree = NULL; // Initializes the binary sort tree pointer
PBinSearchNode pNode, position; // Binary sort tree node pointer
//
// TODO: 在此添加代码
// TODO: Add the code here
//
return 0;
}
/*
功能:
销毁二叉排序树。
function:
Destroy the binary sort tree.
参数:
pTree -- 二叉排序树的指针。
parameter:
pTree -- Pointer to a binary sort tree.
返回值:
returned value:
nothing
*/
void DeleteTree(PBinSearchTree pTree)
{
//
// 利用递归实现后序遍历算法
// The sequential traversal algorithm is implemented by recursion
//
if(pTree != NULL)
{
......@@ -128,25 +128,25 @@ void DeleteTree(PBinSearchTree pTree)
/*
功能:
获取二叉排序树节点。使用递归实现先序遍历。
function:
Get the binary sort tree node.Use recursion to implement preorder traversal.
参数:
parameter:
pTree -- 节点的地址
nLevel -- 节点所在的层级。根节点为 1。
nodeType -- 节点类型
flag -- 构造树结构依据的二进制
pTree -- Node address
nLevel -- The hierarchy of nodes.The root node is 1.
nodeType -- The node type
flag -- The binary on which the tree is constructed
返回值:
如果根据地址构造的表达式可以计算成功,返回 1。否则,返回 0。
returned value:
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 temp1, temp2;
int i;
// 递归在这里结束
// The recursion ends here
if(NULL == *pTree)
return 1;
i = count;
......@@ -168,7 +168,7 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
temp1 = flag << 1;
temp2 = temp1;
// 递归计算左孩子和右孩子
// Recursively compute left child and right child
if(!CopyNode(&(nodeArray[i].node.lchild), nLevel+1, TREE_LEFT, temp1))
return 0;
......@@ -179,14 +179,14 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
}
/*
功能:
输出结果。
function:
output result.
参数:
pTree -- 二叉排序树的指针。
parameter:
pTree -- Pointer to a binary sort tree.
返回值:
returned value:
nothing
*/
void OutputResult(PBinSearchTree* pTree)
......@@ -194,7 +194,7 @@ void OutputResult(PBinSearchTree* pTree)
int i, flag, deep, j;
if(CopyNode(pTree, 1, TREE_ROOT, 0) == 0)
{
printf("二叉排序树构造失败\n");
printf("Binary sort tree construct failed\n");
return;
}
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论