Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab014
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
DS Lab Group
实验项目模板
Lab014
提交
b317761f
提交
b317761f
8月 15, 2019
创建
作者:
宋海霞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify
上级
85ce66dc
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
47 行增加
和
34 行删除
+47
-34
CSTree.h
CSTree.h
+2
-3
Stack.c
Stack.c
+6
-2
Stack.h
Stack.h
+4
-13
main.c
main.c
+35
-16
没有找到文件。
CSTree.h
浏览文件 @
b317761f
#ifndef CSTREE_H_
#define CSTREE_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
...
...
@@ -21,7 +19,8 @@
#define MAX_NUMBER 50
typedef
struct
CSNode
{
typedef
struct
CSNode
{
char
data
;
// Node data
struct
CSNode
*
firstchild
;
// Child Node
struct
CSNode
*
nextsibling
;
// Brother Node
...
...
Stack.c
浏览文件 @
b317761f
...
...
@@ -30,8 +30,10 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem)
//
// Stack full, push failed.
//
if
(
MAX_STACK_LENGTH
<
pS
->
top
)
if
(
MAX_STACK_LENGTH
<
pS
->
top
)
{
return
0
;
}
pS
->
top
++
;
pS
->
buffer
[
pS
->
top
]
=
Elem
;
// Insert the element at the top of the stack
...
...
@@ -56,8 +58,10 @@ struct CSNode* Pop(Stack* pS)
//
// The stack is empty and the pop fails
//
if
(
StackEmpty
(
pS
))
if
(
StackEmpty
(
pS
))
{
return
0
;
}
Elem
=
pS
->
buffer
[
pS
->
top
];
pS
->
top
--
;
...
...
Stack.h
浏览文件 @
b317761f
#ifndef STACK_H_
#define STACK_H_
//
// Include the C standard library header file here
//
//
// Other header files are included here
//
//
// Define the data structure here
//
#define MAX_STACK_LENGTH 64 // Maximum length of stack
// Stack
struct
CSNode
;
typedef
struct
Stack
{
typedef
struct
Stack
{
struct
CSNode
*
buffer
[
MAX_STACK_LENGTH
];
// Stack buffer
int
top
;
// Indicates the position at the top of the stack, not the number of elements in the stack
// Indicates the position at the top of the stack, not the number of elements in the stack
int
top
;
}
Stack
;
//
// Declare the function here
//
...
...
@@ -38,12 +33,8 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem);
struct
CSNode
*
Pop
(
Stack
*
pS
);
int
StackEmpty
(
Stack
*
pS
);
//
// Declare global variables here
//
#endif
/* STACK_H_ */
main.c
浏览文件 @
b317761f
#include "CSTree.h"
#include <stdlib.h>
Stack
stack
;
// Stack. Used to store nodes
...
...
@@ -45,7 +46,9 @@ returned value:
Returns 1 if the traversal succeeds
Returns 0 if the traversal fails
*/
char
g_string
[
MAX_NUMBER
];
// String. Used to save the preordered sequence of a tree during traversal
// String. Used to save the preordered sequence of a tree during traversal
char
g_string
[
MAX_NUMBER
];
int
g_length
=
0
;
// The string length.0 indicates an empty string
int
PreOrder
(
CSTree
pTree
)
{
...
...
@@ -98,7 +101,7 @@ void CreateSubTree(char* data, CSTree pRootNode)
pRootNode
->
firstchild
=
CreateNode
(
data
[
1
]);
pFirstChild
=
pRootNode
->
firstchild
;
for
(
i
=
2
;
data
[
i
]
!=
'\0'
;
i
++
)
for
(
i
=
2
;
data
[
i
]
!=
'\0'
;
i
++
)
{
pFirstChild
->
nextsibling
=
CreateNode
(
data
[
i
]);
pFirstChild
=
pFirstChild
->
nextsibling
;
...
...
@@ -119,22 +122,45 @@ CSNode* PreOrderCreate(CSTree pTree, char Key)
{
CSNode
*
pNode
=
NULL
;
if
(
pTree
!=
NULL
)
if
(
pTree
!=
NULL
)
{
if
(
pTree
->
data
==
Key
)
if
(
pTree
->
data
==
Key
)
{
return
pTree
;
}
pNode
=
PreOrderCreate
(
pTree
->
firstchild
,
Key
);
if
(
pNode
!=
NULL
)
if
(
pNode
!=
NULL
)
{
return
pNode
;
}
pNode
=
PreOrderCreate
(
pTree
->
nextsibling
,
Key
);
if
(
pNode
!=
NULL
)
if
(
pNode
!=
NULL
)
{
return
pNode
;
}
}
return
NULL
;
}
// Two dimensional array for initializing the tree,
// one subtree for each row of the two dimensional array,
// The first character initializes the root node of the subtree,
// The second character initializes the child node,
// The rest of the characters are used to initialize sibling nodes.notice:
// The first character of the first line is used to
// construct the root node of the entire tree,
// The first character of the remaining lines should be
// the child or sibling of the previous line.
const
char
data
[
MAX_NUMBER
][
MAX_NUMBER
]
=
{
{
'R'
,
'A'
,
'B'
,
'C'
},
{
'A'
,
'D'
,
'E'
},
{
'C'
,
'F'
},
{
'F'
,
'G'
,
'H'
,
'K'
}
};
/*
function:
Initialize the tree with a two-dimensional array.
...
...
@@ -142,20 +168,13 @@ function:
returned value:
Return tree pointer
*/
const
char
data
[
MAX_NUMBER
][
MAX_NUMBER
]
=
{
{
'R'
,
'A'
,
'B'
,
'C'
},
// Two dimensional array for initializing the tree, one subtree for each row of the two dimensional array,
{
'A'
,
'D'
,
'E'
},
// The first character initializes the root node of the subtree,The second character initializes the child node,
{
'C'
,
'F'
},
// The rest of the characters are used to initialize sibling nodes.notice:
{
'F'
,
'G'
,
'H'
,
'K'
}
};
// The first character of the first line is used to construct the root node of the entire tree,
// The first character of the remaining lines should be the child or sibling of the previous line.
CSTree
InitTree
()
{
int
i
=
0
;
CSNode
*
pNode
=
NULL
;
CSTree
pRootNode
=
CreateNode
(
data
[
0
][
0
]);
for
(
i
=
0
;
data
[
i
][
0
]
!=
0
;
i
++
)
for
(
i
=
0
;
data
[
i
][
0
]
!=
0
;
i
++
)
{
pNode
=
PreOrderCreate
(
pRootNode
,
data
[
i
][
0
]);
CreateSubTree
((
char
*
)
data
[
i
],
pNode
);
...
...
@@ -179,7 +198,7 @@ void DeleteTree(CSTree pTree)
//
// The sequential traversal algorithm is implemented by recursion
//
if
(
pTree
!=
NULL
)
if
(
pTree
!=
NULL
)
{
DeleteTree
(
pTree
->
firstchild
);
DeleteTree
(
pTree
->
nextsibling
);
...
...
@@ -201,7 +220,7 @@ returned value:
void
OutputResult
()
{
int
i
;
for
(
i
=
0
;
i
<
g_length
-
1
;
i
++
)
for
(
i
=
0
;
i
<
g_length
-
1
;
i
++
)
{
printf
(
"%c"
,
g_string
[
i
]);
}
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论