/
BinTree.c
218 lines (205 loc) · 4.11 KB
/
BinTree.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
#include <assert.h>
#include <stdio.h>
#include <malloc.h>
#include "BinTree.h"
#include "queue.h"
#include "stack.h"
// 构建二叉树的结点
pBTNode BuyBinTreeNode(BTDataType data)
{
pBTNode pNew = (pBTNode)malloc(sizeof(BTNode));
if (NULL == pNew) {
printf("失败");
return NULL;
}
pNew->_data = data;
pNew->_pLeft = pNew->_pRight = NULL;
return pNew;
}
// 创建二叉树
void _CreateBinTree(pBTNode* pRoot, const BTDataType* array, int size, int* index, BTDataType invalid)
{
if (*index < size && array[*index]!= invalid) {
*pRoot = BuyBinTreeNode(array[*index]);
*index += 1;
_CreateBinTree(&(*pRoot)->_pLeft, array, size, index, invalid);
*index += 1;
_CreateBinTree(&(*pRoot)->_pRight, array, size, index, invalid);
}
}
void CreateBinTree(pBTNode* pRoot, const BTDataType* array, int size, BTDataType invalid)
{
assert(pRoot);
assert(array);
int index = 0;
_CreateBinTree(pRoot, array, size, &index, invalid);
}
// 拷贝二叉树
pBTNode CopyBinTree(pBTNode pRoot)
{
pBTNode pNew = NULL;
if (pRoot) {
pNew = BuyBinTreeNode(pRoot->_data);
pNew->_pLeft = CopyBinTree(pRoot->_pLeft);
pNew->_pRight = CopyBinTree(pRoot->_pRight);
}
return pNew;
}
// 销毁二叉树
void DestroyBinTree(pBTNode *pRoot)
{
if (*pRoot) {
DestroyBinTree(&(*pRoot)->_pLeft);
DestroyBinTree(&(*pRoot)->_pRight);
free(*pRoot);
*pRoot = NULL;
}
}
// 前序遍历递归
void PreOrder(pBTNode pRoot)
{
if (pRoot) {
printf("%c ", pRoot->_data);
PreOrder(pRoot->_pLeft);
PreOrder(pRoot->_pRight);
}
}
// 前序遍历非递归一
void PreOrderNor(pBTNode pRoot)
{
Stack s;
init(&s);
pushBack(&s, pRoot);
while (!isEmpty(&s)) {
pBTNode pCur = top(&s);
pop(&s);
printf("%c ", pCur->_data);
if (pCur->_pRight)
pushBack(&s, pCur->_pRight);
if (pCur->_pLeft)
pushBack(&s, pCur->_pLeft);
}
}
// 前序遍历非递归二
void PreOrderNorOP(pBTNode pRoot)
{
Stack s;
init(&s);
pushBack(&s, pRoot);
while (!isEmpty(&s)) {
pBTNode pCur = top(&s);
pop(&s);
while (pCur) {
printf("%c ", pCur->_data);
if (pCur->_pRight) {
pushBack(&s, pCur->_pRight);
}
pCur = pCur->_pLeft;
}
}
}
// 中序遍历递归
void InOrder(pBTNode pRoot)
{
if (pRoot) {
InOrder(pRoot->_pLeft);
printf("%c ", pRoot->_data);
InOrder(pRoot->_pRight);
}
}
// 后续遍历递归
void PostOrder(pBTNode pRoot)
{
if (pRoot) {
PostOrder(pRoot->_pLeft);
PostOrder(pRoot->_pRight);
printf("%c ", pRoot->_data);
}
}
// 层序遍历
void LevelOrder(pBTNode pRoot)
{
LQueue q;
InitQueue(&q);
PushQueue(&q, pRoot);
while (!QueueEmpyt(&q)) {
pBTNode pCur = QueueFront(&q);
PopQueue(&q);
printf("%c ", pCur->_data);
if(pCur->_pLeft)
PushQueue(&q, pCur->_pLeft);
if (pCur->_pRight)
PushQueue(&q, pCur->_pRight);
}
}
void swap(pBTNode *left, pBTNode *right)
{
pBTNode tmp = *left;
*left = *right;
*right = tmp;
}
// 二叉树的镜像递归
void MirrorBinTree(pBTNode pRoot)
{
if (pRoot) {
swap(&pRoot->_pLeft, &pRoot->_pRight);
MirrorBinTree(pRoot->_pLeft);
MirrorBinTree(pRoot->_pRight);
}
}
// 二叉树的镜像非递归
void MirrorBinTreeNor(pBTNode pRoot)
{
LQueue q;
InitQueue(&q);
PushQueue(&q, pRoot);
while (!QueueEmpyt(&q)) {
pBTNode pCur = QueueFront(&q);
PopQueue(&q);
if (pCur->_pLeft)
PushQueue(&q, pCur->_pLeft);
if (pCur->_pRight)
PushQueue(&q, pCur->_pRight);
swap(&pCur->_pLeft, &pCur->_pRight);
}
}
// 求二叉树中结点的个数
int BinTreeSize(pBTNode pRoot)
{
if (NULL == pRoot) {
return 0;
}
return BinTreeSize(pRoot->_pLeft) + BinTreeSize(pRoot->_pRight) + 1;
}
// 获取二叉树中叶子结点的个数
int GetLeafCount(pBTNode pRoot)
{
if (NULL == pRoot) {
return 0;
}
if (NULL == pRoot->_pLeft && NULL == pRoot->_pRight) {
return 1;
}
return GetLeafCount(pRoot->_pLeft) + GetLeafCount(pRoot->_pRight);
}
// 求二叉树中K层结点的个数
int GetKLevelNode(pBTNode pRoot, int K)
{
if (NULL == pRoot || K < 0) {
return 0;
}
if (K == 1) {
return 1;
}
return GetKLevelNode(pRoot->_pLeft, K - 1) + GetKLevelNode(pRoot->_pRight, K - 1);
}
// 求二叉树的高度
int Height(pBTNode pRoot)
{
if (NULL == pRoot) {
return 0;
}
int left = Height(pRoot->_pLeft);
int right = Height(pRoot->_pRight);
return left > right ? left + 1 : right + 1;
}