11 #ifndef TLX_CONTAINER_SPLAY_TREE_HEADER 12 #define TLX_CONTAINER_SPLAY_TREE_HEADER 68 template <
typename Tree,
typename Compare>
70 const Tree*& out_tmin,
const Tree*& out_tmax,
72 if (t ==
nullptr)
return true;
74 const Tree* tmin =
nullptr, * tmax =
nullptr;
79 if (tmax && !cmp(tmax->key, t->key))
81 if (tmin && !cmp(t->key, tmin->key))
87 template <
typename Tree,
typename Compare>
89 if (t ==
nullptr)
return true;
90 const Tree* tmin =
nullptr, * tmax =
nullptr;
96 template <
typename Key,
typename Tree,
typename Compare>
97 Tree *
splay(
const Key& k, Tree* t,
const Compare& cmp) {
98 Tree* N_left =
nullptr, * N_right =
nullptr;
99 Tree* l =
nullptr, * r =
nullptr;
101 if (t ==
nullptr)
return t;
104 if (cmp(k, t->key)) {
105 if (t->left ==
nullptr)
break;
107 if (cmp(k, t->left->key)) {
113 if (t->left ==
nullptr)
break;
116 (r ? r->left : N_left) = t;
120 else if (cmp(t->key, k)) {
121 if (t->right ==
nullptr)
break;
123 if (cmp(t->right->key, k)) {
129 if (t->right ==
nullptr)
break;
132 (l ? l->right : N_right) = t;
141 (l ? l->right : N_right) = (r ? r->left : N_left) =
nullptr;
144 (l ? l->right : N_right) = t->left;
145 (r ? r->left : N_left) = t->right;
155 template <
typename Tree,
typename Compare>
158 nn->left = nn->right =
nullptr;
160 else if (cmp(nn->key, t->key)) {
166 nn->right = t->right;
175 template <
typename Key,
typename Tree,
typename Compare>
177 if (t ==
nullptr)
return nullptr;
178 t =
splay(k, t, cmp);
180 if (!cmp(k, t->key) && !cmp(t->key, k)) {
183 if (t->left ==
nullptr) {
187 Tree* x =
splay(k, t->left, cmp);
200 template <
typename Tree,
typename Functor>
202 if (t ==
nullptr)
return;
209 template <
typename Tree,
typename Functor>
211 if (t ==
nullptr)
return;
220 template <
typename Key,
typename Compare = std::less<Key>,
221 bool Duplicates = false,
222 typename Allocator = std::allocator<Key> >
230 explicit Node(
const Key& k) : key(k) { }
236 explicit SplayTree(Compare cmp, Allocator alloc = Allocator())
245 if (
root_ !=
nullptr) {
262 if (!out)
return false;
304 template <
typename Functor>
328 node_allocator_.deallocate(n, 1);
333 template <
typename Key,
typename Compare = std::less<Key>,
334 typename Allocator = std::allocator<Key> >
337 template <
typename Key,
typename Compare = std::less<Key>,
338 typename Allocator = std::allocator<Key> >
346 #endif // !TLX_CONTAINER_SPLAY_TREE_HEADER size_t size_
number of items in tree container
bool check() const
check the tree order
Tree * splay(const Key &k, Tree *t, const Compare &cmp)
Splay using the key i (which may or may not be in the tree.) The starting root is t...
Tree * splay_insert(Tree *nn, Tree *t, const Compare &cmp)
Insert key i into the tree t, if it is not already there.
bool erase(const Node *n)
erase node from tree, return true if it existed.
Node * find(const Key &k)
find tree node containing key or return smallest key larger than k
bool exists(const Key &k)
check if key exists
bool insert(const Key &k)
insert key into tree if it does not exist, returns true if inserted.
bool erase(const Key &k)
erase key from tree, return true if it existed.
void traverse_preorder(const Functor &f) const
traverse the whole tree in preorder (key order)s
void splay_traverse_postorder(const Functor &f, Tree *t)
traverse the tree in postorder (left, right, node)
void splay_traverse_preorder(const Functor &f, const Tree *t)
traverse the tree in preorder (left, node, right)
Allocator alloc_
key allocator
size_t size() const
return number of items in tree
SplayTree(Allocator alloc=Allocator())
void clear()
free all nodes
Node * root_
root tree node
void delete_node(Node *n)
delete node
node_alloc_type node_allocator_
node allocator
Compare cmp_
key comparator
Allocator::template rebind< Node >::other node_alloc_type
node allocator
bool empty() const
return true if tree is empty
SplayTree(Compare cmp, Allocator alloc=Allocator())
splay tree node, also seen as public iterator
bool splay_check(const Tree *t, const Tree *&out_tmin, const Tree *&out_tmax, const Compare &cmp)
check the tree order, recursively calculate min and max elements
Tree * splay_erase(const Key &k, Tree *&t, const Compare &cmp)
Erases i from the tree if it's there.