Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab011
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
DS Lab Group
实验项目模板
Lab011
提交
ead5a03b
提交
ead5a03b
5月 20, 2019
创建
作者:
宋海霞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify
上级
5adbea7e
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
46 行删除
+46
-46
main.c
main.c
+46
-46
没有找到文件。
main.c
浏览文件 @
ead5a03b
...
...
@@ -3,16 +3,16 @@
int
main
(
int
argc
,
char
*
argv
[])
{
BiTree
pTree
;
//
二叉树指针
BiTree
pTree
;
//
pointer to a binary tree
int
i
;
//
//
创建二叉树
//
CreatBiTree
//
pTree
=
InitTree
();
//
//
后序遍历二叉树
//
After traversing binary tree
//
PostOrder
(
pTree
);
...
...
@@ -23,7 +23,7 @@ int main(int argc, char* argv[])
printf
(
"
\n
"
);
//
//
销毁二叉树
//
Destroy the binary tree
//
DeleteTree
(
pTree
);
...
...
@@ -31,43 +31,43 @@ int main(int argc, char* argv[])
}
/*
功能:
后序遍历二叉树。利用栈实现非递归算法。
function:
After 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 postscript sequence of a binary tree during traversal
int
g_length
=
0
;
//
The string length.0 indicates an empty string
int
PostOrder
(
BiTree
pTree
)
{
BiTNode
*
Stack
[
MAX_STACK_SIZE
];
//
栈。用于存储父节点
enum
PushTimes
TimesStack
[
MAX_STACK_SIZE
];
//
栈。用于存储父节点入栈的次数
int
top
=
0
;
//
两个栈使用相同的栈顶。0 表示空栈
BiTNode
*
Stack
[
MAX_STACK_SIZE
];
//
Stack. Used to store parent nodes
enum
PushTimes
TimesStack
[
MAX_STACK_SIZE
];
//
Stack. Used to store the number of times the parent node is pushed
int
top
=
0
;
//
Both stacks use the same top of the stack.0 means empty stack
BiTNode
*
pNode
;
//
二叉树节点指针
enum
PushTimes
Times
;
//
父节点入栈的次数
BiTNode
*
pNode
;
//
Binary tree node pointer
enum
PushTimes
Times
;
//
Number of parent pushed on the stack
//
// 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
)
{
...
...
@@ -81,37 +81,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
();
...
...
@@ -121,19 +121,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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论