Repository navigation
Expand file tree
/
Copy pathtest_intrusive_binary_tree.cpp
More file actions
539 lines (472 loc) · 19.7 KB
/
Copy pathtest_intrusive_binary_tree.cpp
File metadata and controls
539 lines (472 loc) · 19.7 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
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
// intrusive_binary_tree.h 的最小自检 / 用法示例。
//
// Build with MSVC:
// cl /nologo /std:c++latest /EHsc /utf-8 /I . test_intrusive_binary_tree.cpp
//
// 覆盖:无父指针与带父指针两套侵入式树、proxy 建模、link/cut/splice、
// 旋转、swap_sub_tree/swap_node、erase/clear 的生命周期语义、
// 遍历、移动,以及带 T 的侵入式节点。
// 同时静态检查:侵入式公开类型不提供 pointer / node_pointer 这类指针别名。
#include "intrusive_binary_tree.h"
#include <cassert>
#include <cstdio>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
namespace
{
template<class T, class = void>
struct has_pointer_alias : std::false_type {};
template<class T>
struct has_pointer_alias<T, std::void_t<typename T::pointer>> : std::true_type {};
template<class T, class = void>
struct has_node_pointer_alias : std::false_type {};
template<class T>
struct has_node_pointer_alias<T, std::void_t<typename T::node_pointer>> : std::true_type {};
static_assert(!has_pointer_alias<Yc::intrusive_binary_tree_node_base<>>::value);
static_assert(!has_node_pointer_alias<Yc::intrusive_binary_tree_node_base<>>::value);
static_assert(!has_pointer_alias<Yc::intrusive_binary_tree_node_proxy<>>::value);
static_assert(!has_node_pointer_alias<Yc::intrusive_binary_tree_node_proxy<>>::value);
static_assert(!has_pointer_alias<Yc::intrusive_binary_tree_edge_proxy<>>::value);
static_assert(!has_node_pointer_alias<Yc::intrusive_binary_tree_edge_proxy<>>::value);
static_assert(!has_pointer_alias<Yc::basic_intrusive_binary_tree<>>::value);
static_assert(!has_node_pointer_alias<Yc::basic_intrusive_binary_tree<>>::value);
static_assert(!has_pointer_alias<Yc::intrusive_parent_aware_binary_tree_node_base<>>::value);
static_assert(!has_pointer_alias<Yc::intrusive_parent_aware_binary_tree_virtual_node_base<>>::value);
static_assert(!has_pointer_alias<Yc::intrusive_parent_aware_binary_tree_node_proxy<>>::value);
static_assert(!has_pointer_alias<Yc::intrusive_parent_aware_binary_tree_edge_proxy<>>::value);
static_assert(!has_pointer_alias<Yc::basic_intrusive_parent_aware_binary_tree<>>::value);
using plain_base = Yc::intrusive_binary_tree_node_base<>;
using plain_tree = Yc::basic_intrusive_binary_tree<>;
struct plain_node : plain_base
{
int v = 0;
explicit plain_node(int x) : v(x) {}
};
plain_node& as_plain(plain_base& p) noexcept
{
return static_cast<plain_node&>(p);
}
// 中序收集值
template<class Tree>
std::vector<int> in_order(Tree& t)
{
std::vector<int> out;
t.for_each([&](plain_base& n) { out.push_back(as_plain(n).v); });
return out;
}
// 结构形状:只通过 proxy 导航
std::string shape(plain_tree::node_proxy p)
{
if (!p)
{
return ".";
}
auto& n = as_plain(*p);
return "(" + std::to_string(n.v) + " " + shape(p.get_left()) + " "
+ shape(p.get_right()) + ")";
}
using pa_base = Yc::intrusive_parent_aware_binary_tree_node_base<>;
using pa_tree = Yc::basic_intrusive_parent_aware_binary_tree<>;
struct pa_node : pa_base
{
int v = 0;
explicit pa_node(int x) : v(x) {}
};
std::string pa_shape(pa_tree::node_proxy p)
{
if (!p)
{
return ".";
}
auto& n = static_cast<pa_node&>(*p);
return "(" + std::to_string(n.v) + " " + pa_shape(p.get_left()) + " "
+ pa_shape(p.get_right()) + ")";
}
// 递归验证 parent 链:每个节点的 get_parent() 都指向实际父节点
void check_parents(pa_tree::node_proxy p, pa_tree::node_proxy expected)
{
if (!p)
{
return;
}
assert(p.get_parent() == expected);
check_parents(p.get_left(), p);
check_parents(p.get_right(), p);
}
}
int main()
{
// ---- 无父指针:insert / 遍历 / 形状 ----
{
plain_node a(1), b(2), c(3), d(4), e(5);
plain_tree t;
assert((*plain_tree::node_proxy{ a }).is_detached());
plain_tree::insert(t.root(), a);
assert((*plain_tree::node_proxy{ a }).is_managed());
auto r = t.root();
plain_tree::insert(r.get_left(), b);
plain_tree::insert(r.get_right(), c);
plain_tree::insert(r.get_left().get_left(), d);
plain_tree::insert(r.get_left().get_right(), e);
assert((in_order(t) == std::vector<int>{4, 2, 5, 1, 3}));
assert(shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
// 二叉树 / 边 proxy 的导航
auto root_node = t.nroot();
assert(as_plain(*root_node).v == 1);
assert(as_plain(*root_node.get_left()).v == 2);
assert(as_plain(*root_node.get_right()).v == 3);
assert(!(*root_node.get_left().get_left()).has_children());
t.clear();
assert((*plain_tree::node_proxy{ a }).is_detached());
assert((*plain_tree::node_proxy{ b }).is_detached());
}
// ---- 无父指针:insert 换出原子树 ----
{
plain_node a(1), b(2), c(3);
plain_tree t;
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
plain_tree old = plain_tree::insert(t.root().get_left(), c);
assert(shape(old.nroot()) == "(2 . .)");
assert(shape(t.nroot()) == "(1 (3 . .) .)");
old.clear();
t.clear();
}
// ---- 无父指针:旋转 ----
{
plain_node a(1), b(2), c(3), d(4), e(5);
plain_tree t;
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
plain_tree::insert(t.root().get_right(), c);
plain_tree::insert(t.root().get_right().get_left(), d);
plain_tree::insert(t.root().get_right().get_right(), e);
t.left_rotate(t.root());
assert(as_plain(*t.nroot()).v == 3);
assert(shape(t.nroot()) == "(3 (1 (2 . .) (4 . .)) (5 . .))");
t.right_rotate(t.root());
assert(shape(t.nroot()) == "(1 (2 . .) (3 (4 . .) (5 . .)))");
assert(t.checked_left_rotate(t.root()));
assert(!t.checked_right_rotate(t.root().get_left().get_left()));
t.right_rotate(t.root());
assert(shape(t.nroot()) == "(1 (2 . .) (3 (4 . .) (5 . .)))");
t.clear();
}
// ---- 无父指针:cut / splice / swap_sub_tree / swap_node ----
{
plain_node a(1), b(2), c(3), d(4), e(5);
plain_tree t;
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
plain_tree::insert(t.root().get_right(), c);
plain_tree::insert(t.root().get_left().get_left(), d);
plain_tree::insert(t.root().get_left().get_right(), e);
// 切下右边子树,再拼回去
plain_tree sub = plain_tree::cut(t.root().get_right());
assert(as_plain(*sub.nroot()).v == 3);
assert((*sub.nroot()).is_managed());
assert(shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) .)");
plain_tree::splice(t.root().get_right(), sub.root());
assert(shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
// 交换两个兄弟子树
plain_tree::swap_sub_tree(t.root().get_left(), t.root().get_right());
assert(shape(t.nroot()) == "(1 (3 . .) (2 (4 . .) (5 . .)))");
plain_tree::swap_sub_tree(t.root().get_left(), t.root().get_right());
assert(shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
// 交换两个兄弟:值互换、各自的孩子留在原位
plain_tree::swap_node(t.root().get_left(), t.root().get_right());
assert(shape(t.nroot()) == "(1 (3 (4 . .) (5 . .)) (2 . .))");
plain_tree::swap_node(t.root().get_left(), t.root().get_right());
assert(shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
// 交换父子(单次调用;相邻情形依赖槽位自指在同一次调用内被解开)
plain_tree::swap_node(t.root(), t.root().get_left());
assert(shape(t.nroot()) == "(2 (1 (4 . .) (5 . .)) (3 . .))");
// 移动:根槽位 O(1) 转移(相邻交换后根是 b)
plain_tree moved{ std::move(t) };
assert(t.empty());
assert(shape(moved.nroot()) == "(2 (1 (4 . .) (5 . .)) (3 . .))");
moved.clear();
}
// ---- 无父指针:移动赋值先拆掉原目标树 ----
{
plain_node a(1), b(2), c(3);
plain_tree src, dst;
plain_tree::insert(src.root(), a);
plain_tree::insert(src.root().get_left(), b);
plain_tree::insert(dst.root(), c);
dst = std::move(src);
assert(src.empty());
assert(shape(dst.nroot()) == "(1 (2 . .) .)");
assert((*plain_tree::node_proxy{ c }).is_detached());
dst.clear();
}
// ---- 无父指针:erase / clear 不销毁节点 ----
{
int deleted = 0;
plain_node a(1), b(2), c(3);
plain_tree t;
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
plain_tree::insert(t.root().get_right(), c);
plain_tree::erase(t.root(), [&](auto p)
{
++deleted;
assert(p);
assert(p->is_detached());
});
assert(deleted == 3);
assert(t.empty());
assert((*plain_tree::node_proxy{ a }).is_detached());
assert((*plain_tree::node_proxy{ b }).is_detached());
// clear 只拆结构
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
t.clear();
assert(t.empty());
assert(!t.nroot());
assert((*plain_tree::node_proxy{ a }).is_detached());
// 无 deleter 的 erase 只让子树脱管
plain_tree t2;
plain_tree::insert(t2.root(), a);
plain_tree::insert(t2.root().get_left(), b);
plain_tree::erase(t2.root());
assert(t2.empty());
assert((*plain_tree::node_proxy{ a }).is_detached());
assert((*plain_tree::node_proxy{ b }).is_detached());
}
// ---- 带父指针:insert 换出原子树 ----
{
pa_node a(1), b(2), c(3);
pa_tree t;
pa_tree::insert(t.root(), a);
pa_tree::insert(t.root().get_left(), b);
pa_tree old = pa_tree::insert(t.root().get_left(), c);
assert(pa_shape(old.nroot()) == "(2 . .)");
assert(old.nroot().get_parent() == old.parent_of_nroot());
assert(pa_shape(t.nroot()) == "(1 (3 . .) .)");
old.clear();
t.clear();
}
// ---- 带父指针:parent 链 ----
{
pa_node a(1), b(2), c(3), d(4), e(5);
pa_tree t;
assert((*pa_tree::node_proxy{ a }).is_detached());
pa_tree::insert(t.root(), a);
assert((*pa_tree::node_proxy{ a }).is_managed());
pa_tree::insert(t.root().get_left(), b);
pa_tree::insert(t.root().get_right(), c);
pa_tree::insert(t.root().get_left().get_left(), d);
pa_tree::insert(t.root().get_left().get_right(), e);
assert(t.nroot().get_parent() == t.parent_of_nroot());
assert(t.nroot().get_left().get_parent() == t.nroot());
assert(t.nroot().get_left().get_left().get_parent() == t.nroot().get_left());
check_parents(t.nroot(), t.parent_of_nroot());
// 旋转后 parent 仍然正确
t.left_rotate(t.root());
assert(pa_shape(t.nroot()) == "(3 (1 (2 (4 . .) (5 . .)) .) .)");
check_parents(t.nroot(), t.parent_of_nroot());
t.right_rotate(t.root());
assert(pa_shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
// cut 后子树根在新树里指向新哨兵;splice 回来后再指向原哨兵
{
pa_tree sub = pa_tree::cut(t.root().get_right());
assert((*sub.nroot()).is_managed());
assert(sub.nroot().get_parent() == sub.parent_of_nroot());
assert(pa_shape(sub.nroot()) == "(3 . .)");
pa_tree::splice(t.root().get_right(), sub.root());
assert(t.nroot().get_right().get_parent() == t.nroot());
assert(pa_shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
}
// 交换两个兄弟(可逆)
pa_tree::swap_node(t.root().get_left(), t.root().get_right());
assert(pa_shape(t.nroot()) == "(1 (3 (4 . .) (5 . .)) (2 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
pa_tree::swap_node(t.root().get_left(), t.root().get_right());
assert(pa_shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
// 交换父子(单次)
pa_tree::swap_node(t.root(), t.root().get_left());
assert(pa_shape(t.nroot()) == "(2 (1 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
// 选择性检查掩码:未选中的方向走通用路径
pa_tree::swap_node(t.root(), t.root().get_left(), Yc::no_check);
assert(pa_shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
pa_tree::swap_node(t.root(), t.root().get_left(), Yc::check_left);
assert(pa_shape(t.nroot()) == "(2 (1 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
pa_tree::swap_node(t.root().get_left(), t.root(), Yc::check_left_rev);
assert(pa_shape(t.nroot()) == "(1 (2 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
pa_tree::swap_node(t.root(), t.root().get_left());
assert(pa_shape(t.nroot()) == "(2 (1 (4 . .) (5 . .)) (3 . .))");
check_parents(t.nroot(), t.parent_of_nroot());
// 移动:根 parent 指向新树的哨兵
pa_tree moved{ std::move(t) };
assert(t.empty());
assert(static_cast<pa_node&>(*moved.nroot()).v == 2);
check_parents(moved.nroot(), moved.parent_of_nroot());
moved.clear();
assert((*pa_tree::node_proxy{ a }).is_detached());
}
// ---- 带父指针:移动赋值先拆掉原目标树 ----
{
pa_node a(1), b(2), c(3);
pa_tree src, dst;
pa_tree::insert(src.root(), a);
pa_tree::insert(src.root().get_left(), b);
pa_tree::insert(dst.root(), c);
dst = std::move(src);
assert(src.empty());
assert(static_cast<pa_node&>(*dst.nroot()).v == 1);
check_parents(dst.nroot(), dst.parent_of_nroot());
assert((*pa_tree::node_proxy{ c }).is_detached());
dst.clear();
}
// ---- 带父指针:erase / clear ----
{
int deleted = 0;
pa_node a(1), b(2), c(3), d(4);
pa_tree t;
pa_tree::insert(t.root(), a);
pa_tree::insert(t.root().get_left(), b);
pa_tree::insert(t.root().get_right(), c);
pa_tree::insert(t.root().get_right().get_left(), d);
pa_tree::erase(t.root(), [&](auto p)
{
++deleted;
assert(p->is_detached()); // 交给 deleter 前已经脱管
});
assert(deleted == 4);
assert(t.empty());
assert((*pa_tree::node_proxy{ a }).is_detached());
assert((*pa_tree::node_proxy{ b }).is_detached());
// clear 只拆结构
pa_node f(6), g(7);
pa_tree::insert(t.root(), f);
pa_tree::insert(t.root().get_left(), g);
t.clear();
assert(t.empty());
assert(!t.nroot());
assert((*pa_tree::node_proxy{ f }).is_detached());
// 无 deleter 的 erase 只让子树脱管
pa_tree t2;
pa_tree::insert(t2.root(), f);
pa_tree::insert(t2.root().get_left(), g);
pa_tree::erase(t2.root());
assert(t2.empty());
assert((*pa_tree::node_proxy{ f }).is_detached());
assert((*pa_tree::node_proxy{ g }).is_detached());
}
// ---- const 代理 / 自由 swap / splice 返回旧树 ----
{
plain_node a(1), b(2), c(3);
plain_tree t;
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
const plain_tree& ct = t;
auto ce = ct.root();
auto ce2 = ct.croot();
auto cn = ct.nroot();
auto cn2 = ct.cnroot();
assert(ce);
assert(ce2);
assert(cn);
assert(cn2);
assert(static_cast<const plain_node&>(*cn).v == 1);
assert(ce.get_children().first);
assert(std::hash<plain_tree::node_const_proxy>{}(cn) != 0);
assert(std::hash<plain_tree::edge_const_proxy>{}(ce) != 0);
ct.for_each([](const plain_tree::node_base&) {});
plain_tree t2;
plain_tree::insert(t2.root(), c);
auto old = plain_tree::splice(t.root().get_left(), t2.root());
assert(static_cast<plain_node&>(*old.nroot()).v == 2);
assert(static_cast<plain_node&>(*t.nroot().get_left()).v == 3);
plain_tree x, y;
Yc::swap(x, y);
old.clear();
t.clear();
t2.clear();
}
{
pa_node a(1), b(2);
pa_tree t;
pa_tree::insert(t.root(), a);
pa_tree::insert(t.root().get_left(), b);
const pa_tree& ct = t;
auto ce = ct.croot();
auto cn = ct.cnroot();
auto cp = ct.parent_of_cnroot();
assert(ce);
assert(cn);
assert(cp);
assert(ct.parent_of_nroot() == cp);
auto e = static_cast<pa_tree::edge_proxy>(t.nroot());
assert(e == t.root());
auto ec = static_cast<pa_tree::edge_const_proxy>(ct.nroot());
assert(ec.is_left());
assert(std::hash<pa_tree::node_const_proxy>{}(cn) != 0);
assert(std::hash<pa_tree::edge_const_proxy>{}(ec) != 0);
ct.for_each([](const pa_tree::node_base&) {});
t.clear();
}
// ---- 父感知:跨树 swap_node 可用 no_check ----
{
pa_node a(1), b(2);
pa_tree ta, tb;
pa_tree::insert(ta.root(), a);
pa_tree::insert(tb.root(), b);
pa_tree::swap_node(ta.root(), tb.root(), Yc::no_check);
assert(static_cast<pa_node&>(*ta.nroot()).v == 2);
assert(static_cast<pa_node&>(*tb.nroot()).v == 1);
assert(ta.nroot().get_parent() == ta.parent_of_nroot());
assert(tb.nroot().get_parent() == tb.parent_of_nroot());
ta.clear();
tb.clear();
}
// ---- 树对象析构会自动脱管节点,但不销毁节点 ----
{
plain_node a(1), b(2);
{
plain_tree t;
plain_tree::insert(t.root(), a);
plain_tree::insert(t.root().get_left(), b);
assert((*plain_tree::node_proxy{ a }).is_managed());
}
// 离开作用域:t 的析构函数调用 clear(),节点重新脱管
assert((*plain_tree::node_proxy{ a }).is_detached());
assert((*plain_tree::node_proxy{ b }).is_detached());
pa_node c(3);
{
pa_tree t;
pa_tree::insert(t.root(), c);
assert(c.is_managed());
assert(pa_tree::node_proxy{ c }.get_parent() == t.parent_of_nroot());
}
// 根节点的 parent 不再悬垂到已销毁的哨兵
assert(c.is_detached());
}
// ---- 带 T 的侵入式节点:延迟构造 / 析构 ----
{
using data_node = Yc::intrusive_binary_tree_node<void*, std::string>;
std::allocator<data_node> a;
data_node n;
n.construct_value(a, "plain");
assert(n.value() == "plain");
n.destroy_value(a);
using pa_data_node =
Yc::intrusive_parent_aware_binary_tree_node<void*, std::string>;
std::allocator<pa_data_node> pa;
pa_data_node pn;
pn.construct_value(pa, "parent-aware");
assert(pn.value() == "parent-aware");
pn.destroy_value(pa);
}
std::puts("test_intrusive_binary_tree: ok");
return 0;
}