This repository was archived by the owner on Nov 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOrderStatisticTree.cpp
220 lines (212 loc) · 6.38 KB
/
OrderStatisticTree.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
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
//
// algorithm - some algorithms in "Introduction to Algorithms", third edition
// Copyright (C) 2018 lxylxy123456
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef MAIN
#define MAIN
#define MAIN_OrderStatisticTree
#endif
#ifndef FUNC_OrderStatisticTree
#define FUNC_OrderStatisticTree
#include "utils.h"
#include "RedBlackTree.cpp"
template <typename T>
class SData {
// Size Data
public:
SData(): size(0) {}
SData(T d): data(d), size(0) {}
SData(T d, size_t s): data(d), size(s) {}
friend bool operator<(T x, const SData<T>& rhs) {
return x < rhs.data;
}
bool operator<(const SData<T>& rhs) const { return data < rhs.data; }
bool operator>(const SData<T>& rhs) const { return data > rhs.data; }
bool operator<=(const SData<T>& rhs) const { return data <= rhs.data; }
bool operator>=(const SData<T>& rhs) const { return data >= rhs.data; }
bool operator==(const SData<T>& rhs) const { return data == rhs.data; }
bool operator!=(const SData<T>& rhs) const { return data != rhs.data; }
friend std::ostream& operator<<(std::ostream& os, const SData<T>& rhs) {
return os << rhs.data << '/' << rhs.size;
}
T data;
size_t size;
};
template <typename T>
class OrderStatisticTree: public RedBlackTree<SData<T>> {
public:
OrderStatisticTree(void) {}
virtual void LeftRotate(Node<CData<SData<T>>>* x) {
Node<CData<SData<T>>>* y = x->right;
x->right = y->left;
if (y->left != this->nil)
y->left->parent = x;
y->parent = x->parent;
if (x->parent == this->nil)
this->root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
y->data.data.size = x->data.data.size;
x->data.data.size = x->left->data.data.size +
x->right->data.data.size + 1;
}
virtual void RightRotate(Node<CData<SData<T>>>* x) {
Node<CData<SData<T>>>* y = x->left;
x->left = y->right;
if (y->right != this->nil)
y->right->parent = x;
y->parent = x->parent;
if (x->parent == this->nil)
this->root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->right = x;
x->parent = y;
y->data.data.size = x->data.data.size;
x->data.data.size = x->left->data.data.size +
x->right->data.data.size + 1;
}
void OSInsert(T d) {
Node<CData<SData<T>>>* y = this->nil;
Node<CData<SData<T>>>* x = this->root;
while (x != this->nil) {
x->data.data.size++;
y = x;
if (d < x->data.data)
x = x->left;
else
x = x->right;
}
Node<CData<SData<T>>>* z = new Node<CData<SData<T>>>(
CData<SData<T>>(SData<T>(d, 1), red), y);
if (y == this->nil)
this->root = z;
else if (d < y->data.data)
y->left = z;
else
y->right = z;
z->left = this->nil;
z->right = this->nil;
RedBlackTree<SData<T>>::RBInsertFixup(z);
}
void UpdateSize(Node<CData<SData<T>>>* x) {
if (x != this->nil)
x->data.data.size = x->left->data.data.size +
x->right->data.data.size + 1;
}
void OSDelete(Node<CData<SData<T>>>* z) {
Node<CData<SData<T>>>* x;
Node<CData<SData<T>>>* y = z;
Color y_original_color = y->data.color;
if (z->left == this->nil) {
x = z->right;
RedBlackTree<SData<T>>::RBTransplant(z, z->right);
} else if (z->right == this->nil) {
x = z->left;
RedBlackTree<SData<T>>::RBTransplant(z, z->left);
} else {
y = this->TreeMinimum(z->right);
y_original_color = y->data.color;
x = y->right;
if (y->parent == z) {
x->parent = y;
UpdateSize(x);
} else {
RedBlackTree<SData<T>>::RBTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
}
RedBlackTree<SData<T>>::RBTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->data.color = z->data.color;
}
delete z;
for (Node<CData<SData<T>>>* i = x; i != this->root;) {
i = i->parent;
UpdateSize(i);
}
if (y_original_color == black)
RedBlackTree<SData<T>>::RBDeleteFixup(x);
}
Node<CData<SData<T>>>* OSSelect(Node<CData<SData<T>>>* x, size_t i) {
size_t& r = x->left->data.data.size;
if (i == r)
return x;
else if (i < r)
return OSSelect(x->left, i);
else
return OSSelect(x->right, i - r - 1);
}
Node<CData<SData<T>>>* OSSelect(size_t i) {
return OSSelect(this->root, i);
}
size_t OSRank(Node<CData<SData<T>>>* x) {
size_t r = x->left->data.data.size;
for (Node<CData<SData<T>>>* y = x; y != this->root; y = y->parent) {
if (y == y->parent->right)
r += y->parent->left->data.data.size + 1;
}
return r;
}
// TreeInsert = RBInsert, TreeDelete = RBDelete, TreeSearch
void TreeInsert(T v) { OSInsert(v); }
void TreeDelete(Node<CData<SData<T>>>* z) { OSDelete(z); }
Node<CData<SData<T>>>* TreeSearch(T k) {
return BinarySearchTree<CData<SData<T>>>::TreeSearch(SData<T>(k));
}
private:
void RBInsert(T d);
void RBDelete(Node<CData<T>>* z);
};
#endif
#ifdef MAIN_OrderStatisticTree
void f(char& c, OrderStatisticTree<int>* T, Node<CData<SData<int>>>** p) {
OrderStatisticTree<int>& OS = *T;
Node<CData<SData<int>>>*& ptr = *p;
size_t k;
switch(c) {
case 'l':
std::cout << "r = ";
std::cin >> k;
ptr = OS.OSSelect(k);
print_ptr(ptr, OS.nil);
break;
case 'r':
std::cout << "rank = " << OS.OSRank(ptr) << std::endl;
break;
}
}
int main(int argc, char *argv[]) {
size_t n = get_argv(argc, argv, 1, 0);
OrderStatisticTree<int> OS;
if (n) {
std::vector<int> a;
random_integers(a, 0, n - 1, n);
for (std::vector<int>::iterator i = a.begin(); i != a.end(); i++)
OS.OSInsert(*i);
}
std::string s = "l: select\nr: rank\n";
tree_interact<OrderStatisticTree<int>, CData<SData<int>>, int>(OS, s, f);
return 0;
}
#endif