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 | namespace mds { template<typename T, typename Func = std::less<T>> class b_tree { struct BNode { unsigned size; bool leaf; T *keys; BNode** childs; BNode(bool is_leaf, unsigned degree) : size(0), leaf(is_leaf), keys(new T[2 * degree - 1]), childs(leaf ? nullptr : new BNode*[2 * degree]) { } ~BNode() { delete[] keys; delete[] childs; } }; unsigned size; unsigned degree; BNode *root; Func cmp; template<typename Func> void in_order_recursive(const BNode *node, const Func& func) const { auto size = node->size; auto keys = node->keys; auto childs = node->childs; if (node->leaf) { std::for_each(keys, keys + size, func); } else { for (unsigned i = 0; i < node->size; ++i) { in_order_recursive(childs[i], func); func(keys[i]); } in_order_recursive(childs[node->size], func); } } void remove(BNode* node, const T& key) { auto n = node; while (!n->leaf) { auto i = search_index(n, key); if (equal(n, i, key)) { return delete_internal(n, i); } auto child = n->childs[i]; if (child->size < degree) { i = fill(n, i); } n = n->childs[i]; } auto i = search_index(n, key); if (equal(n, i, key)) { node_remove(n, i); --n->size; --size; } } void take_from_rigth(BNode *n, unsigned index) { auto left = n->childs[index]; auto rigth = n->childs[index + 1]; left->keys[left->size] = std::move(n->keys[index]); n->keys[index] = std::move(node_remove(rigth, 0)); if (!left->leaf) { left->childs[left->size + 1] = rigth->childs[0]; for (auto i = 0u; i < rigth->size; ++i) { rigth->childs[i] = rigth->childs[i + 1]; } } ++left->size; --rigth->size; } void take_from_left(BNode *n, unsigned index) { auto left = n->childs[index - 1]; auto rigth = n->childs[index]; node_insert_index(rigth, 0, std::move(n->keys[index - 1])); n->keys[index - 1] = std::move(left->keys[left->size - 1]); if (!rigth->leaf) { auto childs = rigth->childs; for (auto i = rigth->size + 1; i--> 0; ) { childs[i + 1] = childs[i]; } childs[0] = left->childs[left->size]; } --left->size; ++rigth->size; } void split_child(BNode *n, unsigned index) { auto splited = n->childs[index]; auto new_child = new BNode(splited->leaf, degree); new_child->size = degree - 1; splited->size = degree - 1; for (unsigned i = 0, to = degree - 1; i < to; ++i) { new_child->keys[i] = std::move(splited->keys[i + degree]); } if (!splited->leaf) { for (unsigned i = 0; i < degree; ++i) { new_child->childs[i] = splited->childs[i + degree]; } } for (unsigned i = n->size; i > index; --i) { n->childs[i + 1] = n->childs[i]; } n->childs[index + 1] = new_child; node_insert_index(n, index, std::move(splited->keys[degree - 1])); ++n->size; } template<typename TT> void node_insert_index(BNode *n, unsigned index, TT&& key) { auto i = n->size; auto keys = n->keys; while (i --> index) { keys[i + 1] = std::move(keys[i]); } keys[index] = std::forward<TT>(key); } std::pair<BNode*, unsigned> search(BNode* n, const T& key) const { auto i = search_index(n, key); while (!equal(n, i, key)) { if (n->leaf) { return std::make_pair(nullptr, 0); } n = n->childs[i]; i = search_index(n, key); } return std::make_pair(n, i); } bool equal(const BNode *n, unsigned index, const T& key) const { return index < n->size && !cmp(n->keys[index], key) && !cmp(key, n->keys[index]); } unsigned search_index(const BNode* n, const T& key) const { int begin = 0; int end = n->size - 1; auto keys = n->keys; while (begin <= end) { unsigned mid = begin + (end - begin) / 2; if (cmp(keys[mid], key)) { begin = mid + 1; } else if (cmp(key, keys[mid])) { end = mid - 1; } else { return mid; } } return begin; } template<typename TT> bool node_insert(BNode *n, TT&& key) { auto bound = search_index(n, key); bool not_found = !equal(n, bound, key); if (not_found) { node_insert_index(n, bound, std::forward<TT>(key)); ++n->size; ++size; } return not_found; } unsigned fill(BNode *n, unsigned index) { auto childs = n->childs; if (index != n->size && degree <= childs[index + 1]->size) { take_from_rigth(n, index); } else if (index != 0 && degree <= childs[index - 1]->size) { take_from_left(n, index); } else { if (index == n->size) { --index; } merge(n, index); } return index; } void destroy_tree(BNode *n) { if (!n->leaf) { for (auto i = 0u; i <= n->size; ++i) { destroy_tree(n->childs[i]); } } delete n; } template<typename TT> bool insert_generic(TT&& key) { auto max = 2 * degree - 1; if (root->size == max) { auto child = root; root = new BNode(false, degree); root->childs[0] = child; split_child(root, 0); } auto n = root; while (!n->leaf) { auto index = search_index(n, key); bool found = equal(n, index, key); if (n->childs[index]->size == max) { split_child(n, index); found = found || equal(n, index, key); index += cmp(n->keys[index], key); } if (found) { return false; } n = n->childs[index]; } return node_insert(n, std::forward<TT>(key)); } const T& max(BNode* n) const { while (!n->leaf) { n = n->childs[n->size]; } return n->keys[n->size - 1]; } const T& min(BNode* n) const { while (!n->leaf) { n = n->childs[0]; } return n->keys[0]; } void delete_internal(BNode *n, unsigned index) { auto left = n->childs[index]; auto rigth = n->childs[index + 1]; if (left->size >= degree) { n->keys[index] = max(left); remove(left, n->keys[index]); } else if (rigth->size >= degree) { n->keys[index] = min(rigth); remove(rigth, n->keys[index]); } else { merge(n, index); remove(left, left->keys[degree - 1]); } } T node_remove(BNode* n, unsigned i) { T k = std::move(n->keys[i]); for (auto to = n->size - 1; i < to; ++i) { n->keys[i] = std::move(n->keys[i + 1]); } return k; } void merge(BNode *n, unsigned index) { auto left = n->childs[index]; auto rigth = n->childs[index + 1]; left->keys[degree - 1] = std::move(node_remove(n, index)); for (auto i = index + 1; i < n->size; ++i) { n->childs[i] = n->childs[i + 1]; } if (!rigth->leaf) { for (auto i = 0u; i <= rigth->size; ++i) { left->childs[i + degree] = rigth->childs[i]; } } for (auto i = 0u; i < rigth->size; ++i) { left->keys[i + degree] = std::move(rigth->keys[i]); } --n->size; left->size += rigth->size + 1; delete rigth; } public: b_tree(unsigned pdegree, Func pcmp) : degree(pdegree), root(new BNode(true, pdegree)), cmp(pcmp), size(0) { } b_tree(unsigned pdegree) : b_tree(pdegree, Func()) { } b_tree(unsigned min_degree, std::initializer_list<T> list) : b_tree(min_degree) { for (auto e : list) { insert(e); } } b_tree(std::initializer_list<T> list) : b_tree(50, list) { } b_tree() : b_tree(50) { } ~b_tree() { destroy_tree(root); } void remove(const T& key) { remove(root, key); if (root->size == 0 && !root->leaf) { auto tmp = root; root = root->childs[0]; delete tmp; } } unsigned len() const { return size; } bool contains(const T& key) { return search(root, key).first != nullptr; } bool insert(T&& key) { return insert_generic(std::move(key)); } bool insert(const T& key) { return insert_generic(key); } template<typename... Args> void emplace(Args&&... args) { insert(T(std::forward<Args>(args)...)); } template<typename Func> void in_order_walk(const Func& func) const { in_order_recursive(root, func); } bool empty() { return size == 0; } }; } |
Friday, July 15, 2016
B-Tree implementation
Here is an in-memory implementation of a B-Tree in C++ 11:
Labels:
implementations
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment