-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.hpp
93 lines (72 loc) · 1.73 KB
/
tree.hpp
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
#pragma once
#include <vector>
/**
* Represents an n-ary tree with a node id and a vector of children.
*/
template<
typename value_type
>
class tree {
public:
typedef typename std::vector<size_t>::difference_type index_type;
private:
value_type _id;
std::vector<tree> _children;
// A hack to allow the LCA implementation to inject an offset into a
// node.
index_type _repr;
public:
tree() {}
/**
* Constructs a leaf node.
*/
tree(const value_type &i)
: _id(i),
_children()
{}
// No vector copying allowed.
tree(const value_type &i, const std::vector<tree> &c) = delete;
/**
* Constructs an internal node.
*/
tree(const value_type &i, std::vector<tree> &&c)
: _id(i),
_children(std::move(c))
{}
/**
* Move constructor (marked noexcept so that vector will use it).
*/
tree(tree &&o) noexcept
: _id(std::move(o._id)),
_children(std::move(o._children)),
_repr(std::move(o._repr))
{}
// No copying allowed.
tree(const tree &o) = delete;
/**
* Move assignment.
*/
tree& operator=(tree &&o) {
_id = std::move(o._id);
_children = std::move(o._children);
_repr = std::move(o._repr);
return *this;
}
// No copying assignment allowed.
tree& operator=(const tree &o) = delete;
/**
* This node's identifier.
*/
const value_type &id() const { return _id; }
/**
* A list of this node's children.
*/
std::vector<tree> &children() { return _children; }
const std::vector<tree> &children() const { return _children; }
/**
* The node's representative index in the euler tour. Used by the LCA
* algorithm.
*/
index_type repr() const { return _repr; }
void set_repr(index_type r) { _repr = r; }
};