-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_tree.c
757 lines (725 loc) · 16.8 KB
/
bin_tree.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#include "bin_tree.h"
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "seqstack.h"
#include "seqqueue.h"
TreeNode* CreateTreeNode(TreeNodeType value)
{
TreeNode* newnode = (TreeNode*)malloc(sizeof(TreeNode));
newnode->data = value;
newnode->lchild = NULL;
newnode->rchild = NULL;
return newnode;
}
void TreeNodeInit(TreeNode** root)
{
if(root == NULL)
{
//非法输入
return;
}
*root = NULL;
}
void Destroy(TreeNode* root)
{
free(root);
}
//函数指针
//返回值类型为 TreeNodeType
//参数类型为 结构体指针类型 TreeNode*
//TreeNodeType Print(TreeNode* node)
//{
// printf("%c ",node->data);
// return node->data;
//}
//先序遍历
void PreOrder(TreeNode* root)
{
if(root == NULL)
{
//遇到空节点就返回,递归出口
return;
}
//访问该节点
printf("%c ",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
//中序遍历
void InOrder(TreeNode* root)
{
if(root == NULL)
{
return;
}
InOrder(root->lchild);
printf("%c ",root->data);
InOrder(root->rchild);
}
//后序遍历
void PostOrder(TreeNode* root)
{
if(root == NULL)
{
return;
}
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c ",root->data);
}
//层序遍历
void LevelOrder(TreeNode* root)
{
if(root == NULL)
{
return;
}
//1.先把根节点插入队列
SeqQueue seq;
SeqQueueInit(&seq);
SeqQueuePush(&seq,root);
//2.循环的取队首元素
while(1){
SeqQueueType node;
int ret = SeqQueueGetTop(&seq,&node);
if(ret == 0)
{
return;
}
//3.访问队首元素并出队列
SeqQueuePop(&seq);
if(node != NULL)
{
printf("%c ",node->data);
//4.将队首元素的左子树节点和右子树节点都依次入队列
SeqQueuePush(&seq,node->lchild);
//5.进入下一次循环,直到队列为空,说明遍历完了
SeqQueuePush(&seq,node->rchild);
}
}
}
//@brief 根据先序遍历结果(带有空字符标记)
// 构造一棵树
//@param array[] 先序遍历的结果,加上空字符标记
//@param size 数组的大小
//@param null_node 空字符标记
TreeNode* _TreeCreate(TreeNodeType array[],size_t size,TreeNodeType null_node,size_t* index)
{
if(index == NULL)
{
//非法输入
return NULL;
}
if(*index >= size)
{
//树已经构建完了,index已经到了数组的末尾
return NULL;
}
if(array[*index] == null_node)
{
//前序遍历结果第一个就是空字符,说明是空树
return NULL;
}
TreeNode* root = CreateTreeNode(array[*index]);
(*index)++;
root->lchild = _TreeCreate(array,size,null_node,index);
(*index)++;
root->rchild = _TreeCreate(array,size,null_node,index);
return root;
}
TreeNode* TreeCreate(TreeNodeType array[], size_t size, TreeNodeType null_node)
{
//当前读到了数组的第几个元素
size_t index = 0;
return _TreeCreate(array,size,null_node,&index);
}
//销毁一棵树
TreeNode* TreeDestroy(TreeNode* root)
{
if(root == NULL)
{
//空树
return NULL;
}
TreeDestroy(root->lchild);
TreeDestroy(root->rchild);
Destroy(root);
return NULL;
}
//复制一棵树
TreeNode* TreeClone(TreeNode* root)
{
if(root == NULL)
{
return NULL;
}
TreeNode* newnode = CreateTreeNode(root->data);
newnode->lchild = TreeClone(root->lchild);
newnode->rchild = TreeClone(root->rchild);
return newnode;
}
//求树的节点个数
size_t TreeSize(TreeNode* root)
{
if(root == NULL)
{
return 0;
}
return 1 + TreeSize(root->lchild) + TreeSize(root->rchild);
}
//求树的叶节点个数
size_t LeafTreeSize(TreeNode* root)
{
if(root == NULL)
{
return 0;
}
if(root->lchild == NULL && root->rchild == NULL)
{
return 1;
}
return LeafTreeSize(root->lchild) + LeafTreeSize(root->rchild);
}
//求第K层节点的个数
size_t TreeLevelSize(TreeNode* root,size_t k)
{
//我们规定树从第一层开始
if(root == NULL || k < 1)
{
return 0;
}
if(k == 1)
{
return 1;
}
return TreeLevelSize(root->lchild,k-1) + TreeLevelSize(root->rchild,k-1);
}
//求树的高度
size_t TreeHeight(TreeNode* root)
{
if(root == NULL)
{
return 0;
}
//比较左右两子树的深度,返回深度大的那个
size_t lheight = TreeHeight(root->lchild);
size_t rheight = TreeHeight(root->rchild);
//相当于是调用次数的比较
return lheight >= rheight? lheight+1:rheight+1;
//下面的代码也可以实现相同功能,
//但是函数会被递归调用三次,上一种方式只会被递归调用两次
//所以效率比较低
//return TreeHeight(root->lchild) >= TreeHeight(root->rchild)?TreeHeight(root->lchild):TreeHeight(root->rchild);
}
//在树中查找指定元素
TreeNode* TreeFind(TreeNode* root, TreeNodeType to_find)
{
if(root == NULL)
{
return NULL;
}
if(root->data == to_find)
{
return root;
}
TreeNode* Lresult = TreeFind(root->lchild,to_find);
TreeNode* Rresult = TreeFind(root->rchild,to_find);
//时间复杂度为O(n),最坏的情况是所有元素都遍历了也没找到
//空间复杂度也是O(n)
return Lresult == NULL?Rresult:Lresult;
}
//给一个节点,找它的父节点
TreeNode* Parents(TreeNode* root,TreeNode* node)
{
if(root == NULL)
{
return NULL;
}
if(root->lchild == node || root->rchild == node)
{
return root;
}
TreeNode* Lresult = Parents(root->lchild,node);
TreeNode* Rresult = Parents(root->rchild,node);
return Lresult != NULL? Lresult:Rresult;
}
//非递归遍历二叉树
void PreOrderByLoop(TreeNode* root)
{
if(root == NULL)
{
return;
}
SeqStack stack;
SeqStackInit(&stack);
SeqStackPush(&stack,root);
while(1)
{
//取栈顶元素
SeqStackType cur;
int ret = SeqStackFindTop(&stack,&cur);
//如果栈空的话说明已经遍历完了
if(ret == 0)
{
break;
}
//访问栈顶元素并且出栈
SeqStackPop(&stack);
printf("%c ",cur->data);
//先把每个节点的右孩子节点入栈,
//再把左孩子节点入栈,
//保证每次访问完自身后,再访问左子树
if(cur->rchild != NULL)
{
SeqStackPush(&stack,cur->rchild);
}
if(cur->lchild != NULL)
{
SeqStackPush(&stack,cur->lchild);
}
}
printf("\n");
}
void InOrderByLoop(TreeNode* root)
{
if(root == NULL)
{
return;
}
SeqStack stack;
SeqStackInit(&stack);
SeqStackType cur = root;
while(1)
{
while(cur != NULL)
{
SeqStackPush(&stack,cur);
cur = cur -> lchild;
}
SeqStackType top;
int ret = SeqStackFindTop(&stack,&top);
if(ret == 0)
{
break;
}
printf("%c ",top->data);
SeqStackPop(&stack);
cur = top->rchild;
}
}
void PostOrderByLoop(TreeNode* root)
{
if(root == NULL)
{
return;
}
SeqStack stack;
SeqStackInit(&stack);
TreeNode* cur = root;
//保存上一个访问的元素
TreeNode* pre = NULL;
while(1)
{
//循环的将左子树入栈
while(cur!=NULL)
{
SeqStackPush(&stack,cur);
cur = cur -> lchild;
}
//取出栈顶元素
SeqStackType top;
int ret = SeqStackFindTop(&stack,&top);
if(ret == 0)
{
return;
}
//在访问前要判断
// 1.它的右子树是否为空
// 2.或者判断它的右子树是否刚被访问过
//满足任意一个条件就可以访问当前元素并将其出栈
if(top->rchild == NULL || top->rchild == pre)
{
printf("%c ",top->data);
SeqStackPop(&stack);
pre = top;
}
//否则cur = cur->rchild ,跳到循环开始继续
else{
cur = top->rchild;
}
}
}
//求一个二叉树的镜像
void TreeMirror(TreeNode* root)
{
if(root == NULL)
{
return;
}
TreeNode* tmp = root->lchild;
root->lchild = root->rchild;
root->rchild = tmp;
TreeMirror(root->lchild);
TreeMirror(root->rchild);
}
//非递归版
void TreeMirrorByLoop(TreeNode* root)
{
if(root == NULL)
{
return;
}
SeqStack stack;
SeqStackInit(&stack);
SeqStackPush(&stack,root);
while(1)
{
SeqStackType top;
int ret = SeqStackFindTop(&stack,&top);
if(ret == 0)
{
return;
}
TreeNode* tmp = top->lchild;
top->lchild = top->rchild;
top->rchild = tmp;
SeqStackPop(&stack);
if(top -> lchild != NULL)
{
SeqStackPush(&stack,top->lchild);
}
if(top -> rchild != NULL)
{
SeqStackPush(&stack,top->rchild);
}
}
}
//判断一个树是不是完全二叉树
//前提是进行层序遍历
int IsCompleteTree(TreeNode* root)
{
if(root == NULL)
{
return 1;
}
SeqQueue seq;
SeqQueueInit(&seq);
SeqQueuePush(&seq,root);
int com_l_r_null_flag = 0;
while(1)
{
//进行层序遍历
SeqQueueType top;
SeqQueueGetTop(&seq,&top);
if(top == NULL)
{
break;
}
//访问队首元素
if(com_l_r_null_flag == 0){
if(top->lchild == NULL && top->rchild == NULL)
{
return 1;
}
else if(top->lchild == NULL && top->rchild != NULL)
{
return 0;
}
// else if(top->lchild != NULL && top->rchild != NULL)
// {
// com_l_r_null_flag = 1;
// }
else if(top->lchild != NULL && top->rchild == NULL)
{
com_l_r_null_flag = 1;
}
}else
{
if(top->lchild == NULL && top->rchild == NULL);
else
{
return 0;
}
}
SeqQueuePop(&seq);
//取队首元素
if(top ->lchild != NULL)
{
SeqQueuePush(&seq,top->lchild);
}
if(top ->rchild != NULL)
{
SeqQueuePush(&seq,top->rchild);
}
}
return 1;
}
////////////////////////////////////////////////
////以下为测试代码
////////////////////////////////////////////////
void TestInit()
{
TreeNode* root;
TreeNodeInit(&root);
}
void TestPreOrder()
{
TESTHEAD;
TreeNode* a = CreateTreeNode('a');
TreeNode* b = CreateTreeNode('b');
TreeNode* c = CreateTreeNode('c');
TreeNode* d = CreateTreeNode('d');
TreeNode* e = CreateTreeNode('e');
TreeNode* f = CreateTreeNode('f');
a->lchild = b;
a->rchild = c;
b->lchild = d;
c->lchild = e;
c->rchild = f;
printf("先序遍历结果:\n");
PreOrder(a);
printf("\n");
}
void TestInOrder()
{
TESTHEAD;
TreeNode* a = CreateTreeNode('a');
TreeNode* b = CreateTreeNode('b');
TreeNode* c = CreateTreeNode('c');
TreeNode* d = CreateTreeNode('d');
TreeNode* e = CreateTreeNode('e');
TreeNode* f = CreateTreeNode('f');
a->lchild = b;
a->rchild = c;
b->lchild = d;
c->lchild = e;
c->rchild = f;
printf("中序遍历结果:\n");
InOrder(a);
printf("\n");
}
void TestPostOrder()
{
TESTHEAD;
TreeNode* a = CreateTreeNode('a');
TreeNode* b = CreateTreeNode('b');
TreeNode* c = CreateTreeNode('c');
TreeNode* d = CreateTreeNode('d');
TreeNode* e = CreateTreeNode('e');
TreeNode* f = CreateTreeNode('f');
a->lchild = b;
a->rchild = c;
b->lchild = d;
c->lchild = e;
c->rchild = f;
printf("后序遍历结果:\n");
PostOrder(a);
printf("\n");
}
void TestLevelOrder()
{
TESTHEAD;
TreeNode* a = CreateTreeNode('a');
TreeNode* b = CreateTreeNode('b');
TreeNode* c = CreateTreeNode('c');
TreeNode* d = CreateTreeNode('d');
TreeNode* e = CreateTreeNode('e');
TreeNode* f = CreateTreeNode('f');
a->lchild = b;
a->rchild = c;
b->lchild = d;
c->lchild = e;
c->rchild = f;
printf("层序遍历结果:\n");
LevelOrder(a);
printf("\n");
}
void TestTreeCreate()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
printf("先序遍历结果:\n");
PreOrder(root);
printf("\n");
printf("中序遍历结果:\n");
InOrder(root);
printf("\n");
printf("后序遍历结果:\n");
PostOrder(root);
printf("\n");
}
void TestDestroy()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
root = TreeDestroy(root);
printf("expect: NULL,actual: %p\n",root);
printf("\n");
}
void TestTreeCLone()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
TreeNode* newroot = TreeClone(root);
printf("root: %p ,newroot: %p \n",root,newroot);
printf("先序遍历结果\n");
PreOrder(newroot);
}
void TestSize()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
size_t ret = TreeSize(root);
printf("except: 6,actual: %lu \n",ret);
printf("\n");
}
void TestLeafTreeSize()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
size_t ret = LeafTreeSize(root);
printf("except: 3,actual: %lu \n",ret);
printf("\n");
}
void TestTreeLevelSize()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
size_t ret = TreeLevelSize(root,3);
printf("except: 3 ,actural: %lu \n",ret);
}
void TestTreeHeight()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
int ret = TreeHeight(root);
printf("except: 3,actural: %d\n",ret);
}
void TestTreeFind()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
TreeNode* find = TreeFind(root,'d');
printf("except: d ,actual: %c \n",find->data);
}
void TestParents()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
TreeNode* par = Parents(root,root->lchild->lchild);
printf("except: %p ,actual: %p \n",root->lchild,par);
}
void TestPreOrderByLoop()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
PreOrderByLoop(root);
}
void TestInOrderByLoop()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
InOrderByLoop(root);
}
void TestTreeMirror()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
TreeMirror(root);
PreOrder(root);
}
void TestIsCompleteTree()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd##e##cf##g##";
root = TreeCreate(array,strlen(array),'#');
int ret = IsCompleteTree(root);
PreOrder(root);
printf("\n");
printf("except: 1 , actull: %d \n",ret);
}
void TestPostOrderByLoop()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
PostOrderByLoop(root);
}
void TestTreeMirrorByLoop()
{
TESTHEAD;
TreeNode* root;
TreeNodeInit(&root);
char array[] = "abd###ce##f##";
root = TreeCreate(array,strlen(array),'#');
TreeMirrorByLoop(root);
PreOrder(root);
}
int main()
{
TestInit();
TestPreOrder();
TestInOrder();
TestPostOrder();
TestLevelOrder();
TestTreeCreate();
TestDestroy();
TestSize();
TestLeafTreeSize();
TestTreeCLone();
TestTreeLevelSize();
TestTreeHeight();
TestTreeFind();
TestParents();
TestPreOrderByLoop();
TestInOrderByLoop();
TestPostOrderByLoop();
TestTreeMirror();
TestIsCompleteTree();
TestTreeMirrorByLoop();
printf("\n");
printf("\n");
printf("\n");
return 0;
}