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

modify

上级 92f6e9d5
......@@ -3,16 +3,16 @@
int main(int argc, char* argv[])
{
BiTree pTree; // 二叉树指针
BiTree pTree; // pointer to a binary tree
int i;
//
// 创建二叉树
// CreatBiTree
//
pTree = InitTree();
//
// 先序遍历二叉树
// First order traversing binary tree
//
PreOrder(pTree);
......@@ -23,7 +23,7 @@ int main(int argc, char* argv[])
printf("\n");
//
// 销毁二叉树
// Destroy the binary tree
//
DeleteTree(pTree);
......@@ -31,41 +31,41 @@ int main(int argc, char* argv[])
}
/*
功能:
先序遍历二叉树。利用栈实现非递归算法。
function:
First order traversing binary tree.Stack implementation of non - recursive algorithm.
参数:
BiTree -- 二叉树指针
parameter:
BiTree -- pointer to a binary tree
返回值:
如果遍历成功返回 1
如果遍历失败返回 0
returned value:
Returns 1 if the traversal succeeds
Returns 0 if the traversal fails
*/
char g_string[MAX_STRING_SIZE]; // 字符串。用于在遍历过程中保存二叉树的先序序列
int g_length = 0; // 字符串长度。0 表示空字符串
char g_string[MAX_STRING_SIZE]; // String. Used to save the preordered sequence of a binary tree during traversal
int g_length = 0; // The string length.0 indicates an empty string
int PreOrder(BiTree pTree)
{
BiTNode* Stack[MAX_STACK_SIZE]; // 栈。用于存储右孩子
int top = 0; // 栈顶。0 表示空栈
BiTNode* Stack[MAX_STACK_SIZE]; // Stack. Used to store right child
int top = 0; // The top of the stack.0 means empty stack
BiTNode* pNode; // 二叉树节点指针
BiTNode* pNode; // Binary tree node pointer
//
// TODO: 在此添加代码
// TODO: Add the code here
//
return 0;
}
/*
功能:
创建二叉树的一个节点。
function:
Create a node in the binary tree.
参数:
data -- 二叉树节点保存的数据
parameter:
data -- Binary tree nodes store data
返回值:
返回节点指针
returned value:
Return node pointer
*/
BiTNode* CreateNode(ElemType data)
{
......@@ -79,37 +79,37 @@ BiTNode* CreateNode(ElemType data)
}
/*
功能:
利用二叉树的先序序列创建一个二叉树。
function:
Create a binary tree from the preordered sequence of the binary tree.
返回值:
返回二叉树指针
returned value:
Return pointer to a binary tree
*/
static const char* data = "-*a -b c /d e"; // 二叉树的先序序列字符串。
// 注意:只使用先序序列并不能确定唯一的二叉树。
// 所以,在叶子节点后面要紧跟两个空格,
// 并且,以字符串末尾的字符 '\0' 表示序列结束。
// 这样,先序序列就可以确定唯一的二叉树了。
static int nIndex = 0; // 二叉树先序序列的下标
static const char* data = "-*a -b c /d e"; // A binary tree preordered sequence string.
// notice:Using only preordered sequences does not determine a unique binary tree.
// So, two Spaces after the leaf node,
// Also, the sequence ends with the character '\0' at the end of the string.
// In this way, the preordering sequence determines the unique binary tree.
static int nIndex = 0; // The index of the binary tree first order sequence
BiTree InitTree()
{
BiTNode* pRootNode;
if('\0' == data[nIndex]) // 二叉树的先序序列字符串结束
if('\0' == data[nIndex]) // End of preordering sequence string of binary tree
pRootNode = NULL;
else
{
//
// 创建父节点
// Create parent node
//
pRootNode = (' ' == data[nIndex] ? NULL : CreateNode(data[nIndex])); // 必须忽略空节点
pRootNode = (' ' == data[nIndex] ? NULL : CreateNode(data[nIndex])); // Empty nodes must be ignored
nIndex++;
}
if(pRootNode != NULL)
{
//
// 利用递归实现先序遍历算法
// Recursion is used to implement the first order traversal algorithm
//
pRootNode->lchild = InitTree();
pRootNode->rchild = InitTree();
......@@ -119,19 +119,19 @@ BiTree InitTree()
}
/*
功能:
销毁二叉树。
function:
Destroy the binary tree.
参数:
pTree -- 二叉树指针。
parameter:
pTree -- pointer to a binary tree.
返回值:
returned value:
nothing
*/
void DeleteTree(BiTree pTree)
{
//
// 利用递归实现后序遍历算法
// The sequential traversal algorithm is implemented by recursion
//
if(pTree != NULL)
{
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论