-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_node.cpp
90 lines (82 loc) · 1.79 KB
/
run_node.cpp
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
#include <stack>
#include "run_node.h"
Run_node::Run_node(const Run_node &arg, const bool desc_only)
: state{arg.state}, parent{nullptr}, left{nullptr}, right{nullptr}, graft{arg.graft}
{
if (!desc_only && !arg.is_root()) {
throw Non_root_run_node();
}
if (nullptr != arg.left) {
left = copy_constr_aux(*arg.left, this);
right = copy_constr_aux(*arg.right, this);
return;
}
}
Run_node::Run_node(Run_node &&arg)
: state{arg.state}, parent{arg.parent}, left{arg.left}, right{arg.right}, graft{arg.graft}
{
if (arg.is_left()) {
arg.parent->left = this;
} else if (arg.is_right()) {
arg.parent->right = this;
}
if (nullptr != arg.left) {
arg.left->parent = this;
}
if (nullptr != arg.right) {
arg.right->parent = this;
}
arg.parent = arg.left = arg.right = nullptr;
arg.graft = false;
}
Run_node *
Run_node::copy_constr_aux(const Run_node &arg, Run_node *const p)
{
Run_node *const res = new Run_node(arg.state, p);
res->graft = arg.graft;
if (nullptr != arg.left) {
res->left = copy_constr_aux(*arg.left, res);
res->right = copy_constr_aux(*arg.right, res);
return res;
}
return res;
}
Run_node::~Run_node()
{
delete left;
delete right;
}
Run_node *
Run_node::clone() const
{
std::stack<const Run_node *> stack;
stack.push(this);
while (!stack.top()->is_root()) {
stack.push(stack.top()->parent);
}
Run_node *copy = new Run_node(*stack.top());
stack.pop();
while (!stack.empty()) {
if (stack.top()->is_left()) {
copy = copy->left;
} else {
copy = copy->right;
}
stack.pop();
}
return copy;
}
const Run_node *
Run_node::root() const
{
const Run_node *ancestor = this;
while (!ancestor->is_root()) {
ancestor = ancestor->parent;
}
return ancestor;
}
Run_node *
Run_node::root()
{
return const_cast<Run_node *>(static_cast<const Run_node *>(this)->root());
}