-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbinaryTree.c
More file actions
190 lines (147 loc) · 3.43 KB
/
binaryTree.c
File metadata and controls
190 lines (147 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define MAXSIZE 100
/**************用于构造二叉树*/
// 全局变量
int indexx = 1;
typedef char String[24];
String str;
// 🔑添加字符串
int StrAssign(String T,char *chars){
if (strlen(chars) > MAXSIZE)
return -1;
else {
T[0] = strlen(chars);
for (int i = 1; i <= T[0]; i++)
{
T[i] = *(chars+i-1);
}
return 0;
}
}
/**************************/
typedef char TElemType;
TElemType Nil = ' '; /*字符型以空格为空*/
// 🔨节点结构
typedef struct BiTNode{
TElemType data; // data
struct BiTNode *lchild, *rchild; // leftchild and right child
}BiTNode, *BiTree;
/********stack************/
typedef struct stack
{
BiTNode *elements[50];
int top;
}seqstack;
seqstack s;
void init(){
s.top = 0;
}
void Push(BiTNode *temp){
s.top ++;
s.elements[s.top] = temp;
}
BiTNode *Pop(){
BiTNode *temp = s.elements[s.top];
s.top--;
return temp;
}
/********************/
// 🔑 访问节点
int visit(TElemType e){
printf("%c",e);
return 0;
}
// 🔑 构造空二叉树
int InitBiTree(BiTree *T){
*T = NULL;
return 0;
}
// 🔑 条件:二叉树存在 功能:销毁二叉树
int DestroyBiTree(BiTree *T){
if(*T){
if ( (*T)->lchild ) // leftchild
DestroyBiTree(&(*T)->lchild);
if ( (*T)->rchild )
DestroyBiTree(&(*T)->rchild);
free(*T);
*T = NULL;
}
return 0;
}
// 🔑创建二叉树,按照先根顺序输入节点的值
// #->表示空树,构建二叉链表表示二叉树 T
int CreateBiTree(BiTree *T){
TElemType ch;
ch = str[indexx++];
if (ch == '#')
*T = NULL;
else{
// 分配内存
*T = (BiTree)malloc(sizeof(BiTNode));
// 异常情况
if(!*T)
exit(OVERFLOW);
// 构建新节点
(*T) -> data = ch;
CreateBiTree(&(*T) -> lchild); // 构建左子树
CreateBiTree(&(*T) -> rchild); // 构建右子树
}
return 0;
}
int PreOrder(BiTree T,int location){
BiTree temp = T;
int visit = 0;
while(temp != NULL || s.top != 0 ){
while(temp != NULL)// 先遍历左子树;
{
if(visit == location) {
return 0;
}
//printf("序号:%d,内容:%c \n",visit, temp -> data);
printf("%c ", temp -> data);
visit++;
Push(temp);
temp = temp ->lchild;
}
if (s.top != 0){
temp = Pop();
temp = temp ->rchild;
}
}
// printf("%c ", temp -> data);
// Push(temp);
// //temp = temp ->lchild;
// printf("%c ", temp -> data);
// temp = Pop();
// temp = temp ->rchild;
// printf("%c ", temp -> data);
return 0;
}
int PreOrderVisit(BiTree T,int location){
printf("路径: ");
PreOrder(T,location);
printf(" 长度:%d",location);
printf("\n");
return 0;
}
/************************************************/
int main (){
BiTree T;
InitBiTree(&T);
init();
// test data1
// StrAssign(str,"1246###5##37###"); //先根遍历输入
// int num = 7;
// test data2
StrAssign(str,"124##5##3#67###"); //先根遍历输入
int num = 7;
CreateBiTree(&T);
for (int i = 1; i <=num ; ++i) {
PreOrderVisit(T,i);
}
return 0;
}