Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab010
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
DS Lab Group
实验项目模板
Lab010
提交
0db9f4af
提交
0db9f4af
8月 15, 2019
创建
作者:
宋海霞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify
上级
d6bd63ef
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
24 行增加
和
23 行删除
+24
-23
BinaryTree.h
BinaryTree.h
+2
-8
main.c
main.c
+22
-15
没有找到文件。
BinaryTree.h
浏览文件 @
0db9f4af
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
//
// Define the data structure here
//
...
...
@@ -25,13 +20,13 @@
typedef
char
ElemType
;
typedef
struct
BiTNode
{
typedef
struct
BiTNode
{
ElemType
data
;
// Binary tree node data
struct
BiTNode
*
lchild
;
// Left child pointer
struct
BiTNode
*
rchild
;
// Right child pointer
}
BiTNode
,
*
BiTree
;
//
// Declare the function here
//
...
...
@@ -41,7 +36,6 @@ BiTNode* CreateNode(ElemType data);
BiTree
InitTree
();
void
DeleteTree
(
BiTree
pTree
);
//
// Declare global variables here
//
...
...
main.c
浏览文件 @
0db9f4af
#include "BinaryTree.h"
#include <stdlib.h>
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
@@ -16,7 +16,7 @@ int main(int argc, char* argv[])
//
InOrder
(
pTree
);
for
(
i
=
0
;
i
<
g_length
-
1
;
i
++
)
for
(
i
=
0
;
i
<
g_length
-
1
;
i
++
)
{
printf
(
"%c"
,
g_string
[
i
]);
}
...
...
@@ -41,8 +41,10 @@ returned value:
Returns 1 if the traversal succeeds
Returns 0 if the traversal fails
*/
char
g_string
[
MAX_STRING_SIZE
];
// String. Used to save the middle order sequence of a binary tree during traversal
int
g_length
=
0
;
// The string length.0 indicates an empty string
// String. Used to save the middle order sequence of a binary tree during traversal
char
g_string
[
MAX_STRING_SIZE
];
int
g_length
=
0
;
// The string length.0 indicates an empty string
int
InOrder
(
BiTree
pTree
)
{
BiTNode
*
Stack
[
MAX_STACK_SIZE
];
// Stack. Used to store parent nodes
...
...
@@ -78,6 +80,15 @@ BiTNode* CreateNode(ElemType data)
return
pNode
;
}
// 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
const
char
*
data
=
"-*a -b c /d e"
;
static
int
nIndex
=
0
;
// The index of the binary tree first order sequence
/*
function:
Create a binary tree from the preordered sequence of the binary tree.
...
...
@@ -85,28 +96,24 @@ function:
returned value:
Return pointer to a binary tree
*/
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
])
// End of preordering sequence string of binary tree
if
(
'\0'
==
data
[
nIndex
])
// End of preordering sequence string of binary tree
{
pRootNode
=
NULL
;
}
else
{
//
// Create parent node
// Create parent node
,Empty nodes must be ignored
//
pRootNode
=
(
' '
==
data
[
nIndex
]
?
NULL
:
CreateNode
(
data
[
nIndex
]));
// Empty nodes must be ignored
pRootNode
=
(
' '
==
data
[
nIndex
]
?
NULL
:
CreateNode
(
data
[
nIndex
]));
nIndex
++
;
}
if
(
pRootNode
!=
NULL
)
if
(
pRootNode
!=
NULL
)
{
//
// Recursion is used to implement the first order traversal algorithm
...
...
@@ -133,7 +140,7 @@ void DeleteTree(BiTree pTree)
//
// The sequential traversal algorithm is implemented by recursion
//
if
(
pTree
!=
NULL
)
if
(
pTree
!=
NULL
)
{
DeleteTree
(
pTree
->
lchild
);
DeleteTree
(
pTree
->
rchild
);
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论