-
Notifications
You must be signed in to change notification settings - Fork 378
/
CBT.c
340 lines (308 loc) · 7.03 KB
/
CBT.c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
*
* 完全二叉树结构的基本特征
* (1)在一个树结构中,有且仅有一个节点没有直接前驱,这个节点就是树的根节点;
* (2)除了根节点外,其余结个节点有且仅有一个直接前驱;
* (3)每个结点都可以有任意多个直接后继;
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXLEN 20
typedef char DATA;
typedef struct CBT
{
DATA data;
struct CBT *left;
struct CBT *right;
}CBTType;
CBTType *initTree()
{
CBTType *node;
if (node=(CBTType *)malloc(sizeof(CBTType)))
{
printf("please input the root! \n");
scanf("%s",&node->data);
node->left=NULL;
node->right=NULL;
if (node!=NULL)
{
return node;
}
else
return NULL;
}
return NULL;
}
//查询节点是否存在
CBTType *Treefindnode(CBTType *treeNode,DATA data)
{
if (treeNode==NULL)
{
return NULL;
}
else
{
if (treeNode->data==data)
{
return treeNode ;
}
else
{
if (treeNode=Treefindnode(treeNode->left,data))
{
return treeNode;
}
else if (treeNode=Treefindnode(treeNode->right,data))
{
return treeNode;
}
else
return NULL;
}
}
}
//增加结点
void AddTreeNode(CBTType *treeNode)
{
CBTType *pnode,*parent;
DATA data;
char flag;
if (pnode=(CBTType*)malloc(sizeof(CBTType)))
{
printf("input the node data \n");
fflush(stdin);
scanf("%s",&pnode->data);
pnode->left=NULL;
pnode->right=NULL;
printf("input the node's parent node\n");
fflush(stdin);
scanf("%s",&data);
parent=Treefindnode(treeNode,data);
if (!parent)
{
printf("can't find the parent node!\n");
free(pnode);
return ;
}
printf("1.add to the left node!\n2.add to the right node!");
do
{
flag=getch();
flag-='0';
if (flag==1||flag==2)
{
if (parent==NULL)
{
printf("no parent node,please give parent node first!\n");
}
else
{
switch(flag)
{
case 1: //添加到左节点
if (parent->left) //左节点不为空
{
printf("the lefe node is not empty!\n");
}
else
{
parent->left=pnode;
}
break;
case 2:
if (parent->right)
{
printf("the right node is not empty!\n");
}
else
parent->right=pnode;
break;
default:
printf("useless parameter!\n");
}
}
}
}while(flag!=1&&flag!=2);
}
}
//获得左子树
CBTType *Treeleftnode(CBTType *treeNode)
{
if (treeNode)
{
return treeNode->left;
}
else
return NULL;
}
//获得右子树
CBTType *Treerightnode(CBTType *treeNode)
{
if (treeNode)
{
return treeNode->right;
}
else
return NULL;
}
//判断空树
int Treeisempty(CBTType *TreeNode)
{
if(TreeNode)
return 0;
else
return 1;
}
//计算二叉树的深度
int Treedepth(CBTType *treeNode)
{
int depleft,depright;
if (treeNode==NULL)
{
return 0;
}
else
{
depleft=Treedepth(treeNode->left);
depright=Treedepth(treeNode->right);
if (depleft>depright)
{
return depleft+1;
}
else
return depright+1;
}
}
//清空二叉树
void ClearTree(CBTType *treeNode)
{
if (treeNode)
{
ClearTree(treeNode->left);
ClearTree(treeNode->right);
free(treeNode);
treeNode=NULL;
}
}
//显示节点数据
void display(CBTType *p)
{
printf("%c",p->data);
}
//二叉树的遍历
//层次遍历
void levelTree(CBTType *treeNode)
{
CBTType *q[MAXLEN];
int head=0,tail=0;
if (treeNode)
{
tail=(tail+1)%MAXLEN;
q[tail]=treeNode;
}
while(head!=tail)
{
head=(head+1)%MAXLEN;
treeNode=q[head];
if (treeNode->left!=NULL)
{
tail=(tail+1)%MAXLEN;
q[tail]=treeNode->left;
}
if (treeNode->right!=NULL)
{
tail=(tail+1)%MAXLEN;
q[tail]=treeNode->right;
}
}
}
//先序遍历,中序遍历和后序遍历
void dlrTree(CBTType *p)
{
if(p)
{
printf("%c\n", p->data);
dlrTree(p->left);
dlrTree(p->right);
}
}
void ldrTree(CBTType *p)
{
if(p)
{
dlrTree(p->left);
printf("%c\n", p->data);
dlrTree(p->right);
}
}
void lrdTree(CBTType *p)
{
if(p)
{
dlrTree(p->left);
dlrTree(p->right);
printf("%c\n", p->data);
}
}
int main()
{
CBTType *root=NULL;
char flag1;
char flag2;
root =initTree();
do{
printf("please chose option to add node!\n");
printf("0.quit\t");
printf("1.add the bitree node.\n");
flag1=getch();
switch(flag1)
{
case '1':
AddTreeNode(root);
break;
case '0':
break;
default:
;
}
}while(flag1!='0');
do{
printf("please chose the method to travese the tree,input 0 means quit!\n");
printf("1.xian xu bian li\t");
printf("2.zhong xu bian li\n");
printf("3.xian xu bian li\t");
printf("4.ceng ci bian li\n");
flag2=getch();
switch(flag2)
{
case '0':
break;
case '1':
printf("the answer of dlrTree travese:\n");
dlrTree(root);
printf("\n");
break;
case '2':
printf("the answer of ldrTree travese:\n");
ldrTree(root);
printf("\n");
break;
case '3':
printf("the answer of lrdTree travese:\n");
lrdTree(root);
printf("\n");
break;
case '4':
printf("the answer of levelTree travese:\n");
levelTree(root);
printf("\n");
break;
default:
;
}
}while(flag2!='0');
printf("the depth of the tree is:%d\n", Treedepth(root));
ClearTree(root);
root=NULL;
}