-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadedbts.h
157 lines (144 loc) · 3.02 KB
/
threadedbts.h
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
#ifndef THREADEDBTS
#define THREADEDBTS
#include "bts.h"
template<typename T>
struct ThreadedBtsNode : NodeCRTP<T, ThreadedBtsNode<T>>{
using NodeCRTP<T, ThreadedBtsNode<T>>::NodeCRTP;
std::int_fast8_t lf{0}, rf{1};
};
template<typename T>
class ThreadedBts : public Bts<T>
{
using Node = ThreadedBtsNode<T>;
public:
ThreadedBts() {
root = new Node;
root->left = root->right = root;
}
virtual void add(T&& data) override {
Bts<T>::inc();
auto n = new Node;
n->data = std::move(data);
if (root->left == root && root->right == root) {
n->lf = root->lf;
n->left = root->left;
root->left = n;
root->lf = 1;
n->rf = 0;
n->right = root;
} else {
auto current = root->left;
if (!current)
qDebug() << "alerta";
while (true) {
if (n->data < current->data) {
if (!current->lf) {
directionLeft = true;
directionRight = false;
break;
} else
current = current->left;
} else {
if (!current->rf) {
directionLeft = false;
directionRight = true;
break;
} else
current = current->right;
}
}
if (directionLeft) {
n->lf = current->lf;
n->left = current->left;
current->left = n;
current->lf = 1;
n->rf = 0;
n->right = current;
} else if (directionRight) {
n->rf = current->rf;
n->right = current->right;
current->right = n;
current->rf = 1;
n->lf = 0;
n->left = current;
} else
EXIT_FAILURE;
}
}
/*
* Not used in this example
*/
virtual void add(T& data) override {
qDebug() << "Adding by lvalue to tbts";
Bts<T>::inc();
auto n = new Node;
n->data = data;
if (root->left == root && root->right == root) {
n->lf = root->lf;
n->left = root->left;
root->left = n;
root->lf = 1;
n->rf = 0;
n->right = root;
} else {
auto current = root->left;
if (!current)
qDebug() << "alerta";
while (true) {
if (n->data < current->data) {
if (!current->lf) {
directionLeft = true;
directionRight = false;
break;
} else
current = current->left;
} else {
if (!current->rf) {
directionLeft = false;
directionRight = true;
break;
} else
current = current->right;
}
}
if (directionLeft) {
n->lf = current->lf;
n->left = current->left;
current->left = n;
current->lf = 1;
n->rf = 0;
n->right = current;
} else if (directionRight) {
n->rf = current->rf;
n->right = current->right;
current->right = n;
current->rf = 1;
n->lf = 0;
n->left = current;
} else
EXIT_FAILURE;
}
}
void traverse(auto c) override {
auto current = root->left;
while (current->lf == 1)
current = current->left;
while(current != root){
c(current->data);
current = next(current);
}
}
private:
Node *root;
bool directionLeft{true},
directionRight{false};
Node* next(Node *const & n) const {
if(n->rf == 0)
return n->right;
auto tmp = n->right;
while(tmp->lf == 1)
tmp = tmp->left;
return tmp;
}
};
#endif // THREADEDBTS