Repository navigation
Expand file tree
/
Copy pathbtreeOps.cpp
More file actions
498 lines (409 loc) · 13.4 KB
/
Copy pathbtreeOps.cpp
File metadata and controls
498 lines (409 loc) · 13.4 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
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
//二叉树常用操作
//Thu Jul 17 06:40:24 PDT 2014
#include <iostream>
#include <assert.h>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef struct BTreeNode {
int value;
BTreeNode *lchild;
BTreeNode *rchild;
}*BTree;
//函数:根据所给数据创建二叉排序树
BTree CreateBinaryTree(int *arr, int n);
//向二叉排序树中插入元素-非递归
void InsertBinaryTree(BTree *root, int val);
//函数:二叉树遍历-前序遍历
void PreOrderTraversal(BTree root);
//函数:二叉树遍历-中序遍历
void InOrderTraversal(BTree root);
//函数:二叉树遍历-后序遍历
void PostOrderTraversal(BTree root);
//函数:重建二叉树,依据前序遍历结果和中序遍历结果
BTreeNode* BuildBTreeWithInorderPreOrder(int pre[], int in[], int n);
BTreeNode* Construct(int *preStart, int *preEnd, int *inStart, int *inEnd);
//函数:判断二叉树的后序遍历是否合法
bool IsPostOrderValid(int post[], int n);
//函数:二叉树中和为某值的路径
void FindPathWithSum(BTree root, int expectedSum);
void FindPathCore(BTree root, vector<BTreeNode*> &path, int currentSum, int expectedSum);
//函数:将二叉树转化成双向链表
BTreeNode* ConvertNode(BTree root);
void ConvertNodeCore(BTreeNode* curNode, BTreeNode* &preNode);
//函数:求二叉树深度
int TreeDepth(BTree root);
//函数:判断二叉树是否平衡二叉树
bool IsAVLTree(BTree root);
//函数:层次遍历二叉树
void TraversalByLevel(BTree root);
//求二叉树第K层节点个数
int GetNodeNumKthLevel(BTree root, int k);
//求二叉树中两个节点的最大距离
int GetMaxDistance(BTree root);
//判断一颗二叉树是否为完全二叉树
bool IsCompleteTree(BTree root);
//求一个二叉树中两个节点最低公共祖先节点
BTreeNode* GetLCANode(BTree root, BTreeNode* u, BTreeNode *v);
bool IsNodeExist(BTree root, BTreeNode* node);
int main()
{
//int a[] = {7, 2, 8, 3, 6, 4, 5, 1};
//int n = sizeof(a) / sizeof(a[0]);
//BTree bt = CreateBinaryTree(a, n);
//cout << "前序遍历:";
//PreOrderTraversal(bt);
//cout << endl;
//cout << "中序遍历:";
//InOrderTraversal(bt);
//cout << endl;
//cout << "后序遍历:";
//PostOrderTraversal(bt);
//cout << endl;
//cout << "层次遍历:";
//TraversalByLevel(bt);
//cout << endl;
int preOrder[] = { 7, 2, 1, 3, 6, 4, 5, 8 };
int inOrder[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int n = sizeof(preOrder) / sizeof(int);
BTree bt = BuildBTreeWithInorderPreOrder(preOrder, inOrder, n);
cout << "前序遍历:" ;
PreOrderTraversal(bt);
cout << endl;
cout << "中序遍历:" ;
InOrderTraversal(bt);
cout << endl;
cout << "后序遍历:" ;
PostOrderTraversal(bt);
cout << endl;
int postOrder[] = { 1, 5, 4, 6, 3, 2, 8, 7};
cout << "后续序列合法:" << IsPostOrderValid(postOrder, n)
<< "\t树深度:" << TreeDepth(bt) << endl;
FindPathWithSum(bt, 27);
cout << "AVL:" << IsAVLTree(bt)
<< "\t第3层节点数:" << GetNodeNumKthLevel(bt, 3) << endl;
cout << "是否完全二叉树:" << IsCompleteTree(bt) << endl;
cout << "两个节点最大距离:" << GetMaxDistance(bt) << endl;
BTreeNode* u = bt->lchild->lchild;
BTreeNode* v = bt->lchild->rchild->rchild->lchild;
cout << IsNodeExist(bt, u) << "|" << IsNodeExist(bt, v) << "|" << IsNodeExist(bt, NULL) << endl ;
cout << u->value << " " << v->value << endl;
cout << (GetLCANode(bt, u, v))->value << endl; //有BUG,待完善
//BTreeNode* head = ConvertNode(bt);
//BTreeNode* pos = head;
//cout << "转换成的链表遍历:";
//while (head != NULL)
//{
// cout << head->value << "\t";
// pos = head->rchild;
//}
//cout << endl;
return 0;
}
//函数:根据所给数据创建二叉排序树
BTree CreateBinaryTree(int *arr, int n)
{
assert(arr != NULL && n > 0);
BTree root = NULL;
for (int i=0; i<n; ++i)
InsertBinaryTree(&root, *(arr+i));
//InsertBTreeRecursion(root, *(arr+i));
return root;
}
//向二叉排序树中插入元素-非递归
void InsertBinaryTree(BTree *root, int val)
{
BTreeNode *newNode = new BTreeNode();
newNode->value = val;
newNode->lchild = newNode->rchild = NULL;
BTreeNode *pre, *cur;
pre = NULL;
cur = *root;
while (cur != NULL)
{
pre = cur;
if (val < cur->value)
cur = cur->lchild;
else
cur = cur->rchild;
}
if (NULL == pre)
*root = newNode;
else if (val < pre->value)
pre->lchild = newNode;
else
pre->rchild = newNode;
}
//函数:二叉树遍历-前序遍历
void PreOrderTraversal(BTree root)
{
if (root != NULL)
{
cout << root->value << "\t";
PreOrderTraversal(root->lchild);
PreOrderTraversal(root->rchild);
}
}
//函数:二叉树遍历-中序遍历
void InOrderTraversal(BTree root)
{
if (root != NULL)
{
InOrderTraversal(root->lchild);
cout << root->value << "\t";
InOrderTraversal(root->rchild);
}
}
//函数:二叉树遍历-后序遍历
void PostOrderTraversal(BTree root)
{
if (root != NULL)
{
PostOrderTraversal(root->lchild);
PostOrderTraversal(root->rchild);
cout << root->value << "\t";
}
}
//函数:重建二叉树,依据前序遍历结果和中序遍历结果
//思路:1.从前序系列中取第一个元素x,再在中序序列中找到这个元素x
// 2.以x为界,以x的值新建根节点root,x左边即为左子树(记节点数为ln),x右边即为右子树(记节点数为rn)
// 3.在前序序列中x后,前ln个节点即为左子树,后rn个节点即为右子树
// 4.设置root的左右孩子分别为1、2、3步递归调用左右子树序列后的返回值
BTreeNode* BuildBTreeWithInorderPreOrder(int pre[], int in[], int n)
{
assert(pre != NULL && in != NULL && n > 0);
return Construct(pre, pre + n - 1, in, in + n - 1);
}
BTreeNode* Construct(int *preStart, int *preEnd, int *inStart, int *inEnd)
{
int rootVal = *preStart;
BTreeNode *root = new BTreeNode();
root->value = rootVal;
root->lchild = root->rchild = NULL;
//定义递归出口:当前序、中序序列中都仅有一个元素时,递归返回该节点
if (preStart == preEnd)
{
if (inStart == inEnd && *preStart == *inStart)
return root;
else
throw "Invalid Input.";
}
//在中序序列中查找根节点
int *pos = inStart;
while (pos <= inEnd && *pos != rootVal)
++pos;
if (pos == inEnd && *pos != rootVal)
throw "Invalid Input.";
int leftLength = pos - inStart;
int *leftPreEnd = preStart + leftLength;
//构建左子树
if (leftLength > 0)
{
root->lchild = Construct(preStart + 1, leftPreEnd, inStart, pos - 1);
}
//构建右子树
if (leftLength < preEnd - preStart)
{
root->rchild = Construct(leftPreEnd + 1, preEnd, pos + 1, inEnd);
}
return root;
}
//函数:判断二叉树的后序遍历是否合法
bool IsPostOrderValid(int post[], int n)
{
assert(post != NULL && n > 0);
//取序列最后一个元素作为根
int root = post[n-1];
//扫描序列,比根小作为左子树
int leftLength, rightLength;
int *lend = post;
while(lend <= post + n - 1 && *lend < root)
++lend;
leftLength = lend - post;
//比根大作为右子树
int *rend = lend;
while(rend <= post + n - 1)
{
if (*rend < root)
return false;
++rend;
}
rightLength = n - leftLength - 1;
//判断左子树是否二叉搜索树
bool left = true;
if (leftLength > 0)
left = IsPostOrderValid(post, leftLength);
//判断左子树是否二叉搜索树
bool right = true;
if (rightLength > 0)
left = IsPostOrderValid(rend, rightLength);
return left && right;
}
//函数:二叉树中和为某值的路径
void FindPathWithSum(BTree root, int expectedSum)
{
assert(root != NULL);
vector<BTreeNode*> path;
FindPathCore(root, path, 0, expectedSum);
}
void FindPathCore(BTree root, vector<BTreeNode*> &path,
int currentSum, int expectedSum)
{
currentSum += root->value;
path.push_back(root);
//递归出口:如果路径到达页节点,且和为期望值,则打印
bool isLeaf = (root->lchild == NULL && root->rchild == NULL);
if (isLeaf && currentSum == expectedSum)
{
vector<BTreeNode*>::iterator ite = path.begin();
for (;ite != path.end(); ++ite)
cout << (*ite)->value << "\t";
cout << endl;
}
//如果不是叶节点,则遍历他的子节点
if (root->lchild != NULL)
FindPathCore(root->lchild, path, currentSum, expectedSum);
if (root->rchild != NULL)
FindPathCore(root->rchild, path, currentSum, expectedSum);
//该条路径不合要求,路径上删除该节点
//currentSum -= root->value; //不需要,因为非引用传递
path.pop_back();
}
//函数:将二叉树转化成双向链表
BTreeNode* ConvertNode(BTree root)
{
assert(root != NULL);
BTreeNode* lastNode = NULL;
ConvertNodeCore(root, lastNode);
BTreeNode* head = lastNode;
while(head != NULL && head->lchild != NULL)
head = head->lchild;
return head;
}
void ConvertNodeCore(BTreeNode* curNode, BTreeNode* &preNode)
{
//可以借用中序遍历的思路
if (curNode == NULL)
return;
if(curNode->lchild != NULL)
ConvertNodeCore(curNode->lchild, preNode);
//curNode前驱指向preNode, preNode后继指向curNode
curNode->lchild = preNode;
if (preNode != NULL)
preNode->rchild = curNode;
preNode = curNode;
if(curNode->rchild != NULL)
ConvertNodeCore(curNode->rchild, preNode);
}
//函数:求二叉树深度
int TreeDepth(BTree root)
{
if (root == NULL)
return 0;
int llen = TreeDepth(root->lchild);
int rlen = TreeDepth(root->rchild);
return 1 + (llen > rlen ? llen : rlen);
}
//函数:判断二叉树是否平衡二叉树
bool IsAVLTree(BTree root)
{
if (root == NULL)
return true;
int llen = TreeDepth(root->lchild);
int rlen = TreeDepth(root->rchild);
if (llen - rlen <= -1 || llen - rlen >= 1)
return false;
else
return IsAVLTree(root->lchild) && IsAVLTree(root->rchild);
}
//层次遍历二叉树
void TraversalByLevel(BTree root)
{
assert(root != NULL);
queue<BTreeNode*> que;
que.push(root);
while(!que.empty())
{
BTreeNode *node = que.front();
cout << node->value << "\t";
que.pop();
if (node->lchild != NULL)
que.push(node->lchild);
if (node->rchild != NULL)
que.push(node->rchild);
}
}
//求二叉树第K层节点个数
int GetNodeNumKthLevel(BTree root, int k)
{
if (root == NULL)
return 0;
if (k == 1)
return 1;
return GetNodeNumKthLevel(root->lchild, k - 1)
+ GetNodeNumKthLevel(root->rchild, k - 1);
}
//求二叉树中两个节点的最大距离
int GetMaxDistance(BTree root)
{
return TreeDepth(root->lchild) + TreeDepth(root->rchild);
}
//判断一颗二叉树是否为完全二叉树
bool IsCompleteTree(BTree root)
{
//按照层次遍历,当遇到一个节点的左子树为空时,则该节点右子树必须为空。
//且后面遍历的节点必须都为叶节点
assert(root != NULL);
queue<BTreeNode*> que;
que.push(root);
bool leaf = false;
while (!que.empty())
{
BTreeNode* node = que.front();
que.pop();
if(leaf && (node->lchild != NULL || node->rchild != NULL)) //若应是叶节点却不是
return false;
if (node->lchild == NULL && node->rchild != NULL) //若左孩子为NULL,右却不为NULL
return false;
if (node->rchild == NULL)
leaf = true;
if (root->lchild != NULL)
que.push(root->lchild);
if (root->rchild != NULL)
que.push(root->rchild);
}
}
//求一个二叉树中两个节点最低公共祖先节点
BTreeNode* GetLCANode(BTree root, BTreeNode* u, BTreeNode *v)
{
if (root == NULL)
return NULL;
//设当前节点为root
//若当前节点左子树(或右子树)中能同时找到节点u和v,则将当前节点设置为root的左孩子(或右孩子)
//若节点u,v一个在当前节点的左子树,一个在当前节点的右子树,则当前节点为u,v最低公共祖先节点
BTreeNode *currentNode = root;
while (currentNode != NULL)
{
if (IsNodeExist(root->lchild, u) && IsNodeExist(root->rchild, v)
|| IsNodeExist(root->lchild, v) && IsNodeExist(root->rchild, u))
return currentNode;
if (IsNodeExist(root->lchild, u) && IsNodeExist(root->lchild, v))
currentNode = currentNode->lchild;
if (IsNodeExist(root->rchild, u) && IsNodeExist(root->rchild, v))
currentNode = currentNode->rchild;
}
return NULL;
}
bool IsNodeExist(BTree root, BTreeNode* node)
{
if (root != NULL)
{
if (root == node)
return true;
return IsNodeExist(root->lchild, node)
|| IsNodeExist(root->rchild, node);
return false;
}
return false;
}