-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBtree.h
65 lines (58 loc) · 1.74 KB
/
Btree.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
#pragma once
#include "PoolAllocator.h"
#include "Vector.h"
#include "BinarySearchTree.h"
#include "BinarySearchTreeUtilities.h"
#include "RedBlackTree.h"
#include <cmath>
#include <algorithm>
namespace jumbuna {
template<class T, class C, class B>
struct BtreeNode {
using Node = BtreeNode<T, C, B>;
BtreeNode(Node *, std::size_t);
Allocator<B> nodeAllocator;
struct NodePointerComparator {
bool operator()(Node *, Node *);
};
Allocator<RedBlackTree<Node *, NodePointerComparator>> rbAllocator;
BinarySearchTree<T, C> &keys;
BinarySearchTree<Node *, NodePointerComparator> &children;
Node *parent;
};
template<class T, class C, class B>
struct Utility {
using Node = BtreeNode<T, C, B>;
static bool isOverflow(Node *, std::size_t);
static bool isUnderflow(Node *, std::size_t);
static bool isLeaf(Node *);
static Node *successorChild(Node *, T);
static T preOrderSuccessor(Node *);
};
template<class T, class C = std::greater<T>, class B = RedBlackTree<T, C>>
class Btree {
using Node = BtreeNode<T, C, B>;
using Util = Utility<T, C, B>;
Allocator<Node> nodeAllocator;
void borrowKeyFromLeftSibling(Node *, Node *, T);
void borrowKeyFromRightSibling(Node *, Node *, T);
void mergeSiblings(Node *, Node *, T);
void postInsertBalance(Node *);
void postRemoveBalance(Node *);
void insert(Node *, Node *, T);
void remove(Node *, T);
void removeCaseOne(Node *, Vector<T> &, Vector<Node*> &);
void removeCaseTwo(Node *, Vector<T> &, Vector<Node*> &);
void removeCaseThree(Node *, Vector<T> &, Vector<Node*> &);
Node *root;
std::size_t elementCount, treeOrder;
public:
Btree(std::size_t = 5);
void insert(T);
void remove(T);
bool contains(T);
std::size_t size();
Vector<T> treeTraversal(TraversalOrder = LEVEL_ORDER);
};
}
#include "BtreeImpl.h"