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

modify

上级 8ac45b3b
#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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论