41 template<
typename Node,
typename Get_key,
typename Compare >
70 Bits::Bst_node::operator = (o);
71 _balance = o._balance;
81 bool balanced()
const {
return _balance ==
Bal::N; }
84 Bal balance()
const {
return _balance; }
87 void balance(Bal b) { _balance = b; }
107 template<
typename Node,
typename Get_key,
129 void operator = (
Avl_tree const &o);
164 Node *
remove(Key_param_type key);
168 Node *
erase(Key_param_type key) {
return remove(key); }
175 this->remove_all([](Node *){});
178 #ifdef __DEBUG_L4_AVL 179 bool rec_dump(
Avl_tree_node *n,
int depth,
int *dp,
bool print,
char pfx);
180 bool rec_dump(
bool print)
183 return rec_dump(static_cast<Avl_tree_node *>(_head), 0, &dp, print,
'+');
199 N *tmp[2] = { *t, N::next(*t, idir) };
200 *t = N::next(tmp[1], !idir);
201 A *n =
static_cast<A*
>(*t);
203 N::next(tmp[0], idir, N::next(n, !idir));
204 N::next(tmp[1], !idir, N::next(n, idir));
205 N::next(n, !idir, tmp[0]);
206 N::next(n, idir, tmp[1]);
212 static_cast<A*
>(tmp[0])->balance(
Bal::N);
213 static_cast<A*
>(tmp[1])->balance(
Bal::N);
217 static_cast<A*
>(tmp[pre != idir])->balance(!pre);
218 static_cast<A*
>(tmp[pre == idir])->balance(
Bal::N);
220 return N::next(tmp[pre == idir], !pre);
227 template<
typename Node,
typename Get_key,
class Compare>
235 Key_param_type new_key = Get_key::key_of(new_node);
238 for (N *p; (p = *t);)
240 Dir b = this->dir(new_key, p);
242 return pair(static_cast<Node*>(p),
false);
244 if (!static_cast<A const *>(p)->balanced())
250 *
static_cast<A*
>(new_node) = A(
true);
254 A *a =
static_cast<A*
>(n);
257 A::Bal b(this->greater(new_key, n));
258 if (a->balance() != b)
265 else if (b ==
Bal(this->greater(new_key, N::next(n, b))))
270 static_cast<A*
>(*s)->balance(
Bal::N);
276 n = N::next(N::next(n, b), !b);
277 n = A::rotate2(s, b, n == new_node ?
Bal::N :
Bal(this->greater(new_key, n)));
281 for (A::Bal b; n && n != new_node;
static_cast<A*
>(n)->balance(b), n = N::next(n, b))
282 b =
Bal(this->greater(new_key, n));
284 return pair(new_node,
true);
289 template<
typename Node,
typename Get_key,
class Compare>
303 for (N *n; (n = *q); q = N::next_p(n, dir))
305 dir =
Dir(this->greater(key, n));
306 if (dir ==
Dir::L && !this->greater(k(n), key))
310 if (!N::next(n, dir))
313 A
const *a =
static_cast<A
const *
>(n);
314 if (a->balanced() || (a->balance() == !dir && N::next<A>(n, !dir)->balanced()))
322 A *i =
static_cast<A*
>(*t);
324 for (N *n; (n = *s); s = N::next_p(n, dir))
326 dir =
Dir(this->greater(key, n));
328 if (!N::next(n, dir))
331 A *a =
static_cast<A*
>(n);
335 else if (a->balance() == dir)
340 Bal b = N::next<A>(n, !dir)->balance();
342 A::rotate2(s, !dir, N::next<A>(N::next(n, !dir), dir)->balance());
349 static_cast<A*
>(*s)->balance(
Bal::N);
354 static_cast<A*
>(*s)->balance(dir);
358 t = N::next_p(*s, dir);
362 A *n =
static_cast<A*
>(*q);
364 *q = N::next(n, !dir);
367 return static_cast<Node*
>(i);
370 #ifdef __DEBUG_L4_AVL 371 template<
typename Node,
typename Get_key,
class Compare>
379 int dpx[2] = {depth,depth};
382 res = rec_dump(n->
next<A>(
Dir::R), depth + 1, dpx + 1, print,
'/');
386 fprintf(stderr,
"%2d: [%8p] b=%1d: ", depth, n, (
int)n->balance().d);
388 for (
int i = 0; i < depth; ++i)
391 std::cerr << pfx << (static_cast<Node*>(n)->item) << std::endl;
394 res = res & rec_dump(n->
next<A>(
Dir::L), depth + 1, dpx, print,
'\\');
396 int b = dpx[1] - dpx[0];
403 Bal x = n->balance();
404 if ((b < -1 || b > 1) ||
405 (b == 0 && x !=
Bal::N) ||
406 (b == -1 && x !=
Bal::L) ||
410 fprintf(stderr,
"%2d: [%8p] b=%1d: balance error %d\n", depth, n, (
int)n->balance().d, b);
Avl_tree_node()
Create an uninitialized node, this is what you should do.
Node * erase(Key_param_type key)
An alias for remove().
__Bst_iter< _Node, _Node, Rev > Rev_iterator
Backward iterator.
Bst_node()
Create uninitialized node.
Node * remove(Key_param_type key)
Remove the node with key from the tree.
Pair< Node *, bool > insert(Node *new_node)
Insert a new node into this AVL tree.
Bst::Const_rev_iterator Const_rev_iterator
Constant backward iterator for the tree.
__Bst_iter< _Node, _Node const, Fwd > Const_iterator
Constant forward iterator.
BasicOStream cerr
Standard error stream.
__Bst_iter< _Node, _Node const, Rev > Const_rev_iterator
Constant backward.
The direction to go in a binary search tree.
Get_key::Key_type Key_type
The type of key values used to generate the total order of the elements.
Bst::Rev_iterator Rev_iterator
Backward iterator for the tree.
static Bst_node * next(Bst_node const *p, Direction d)
Get next node in direction d.
Bst::Const_iterator Const_iterator
Constant forward iterator for the tree.
static void rotate(Bst_node **t, Direction idir)
Rotate subtree t in the opposite direction of idir.
Type_traits< Key_type >::Param_type Key_param_type
The type for key parameters.
Avl_tree()
Create an empty AVL tree.
Generic comparator class that defaults to the less-than operator.
Basic binary search tree (BST).
Basic type of a node in a binary search tree (BST).
static Bst_node ** next_p(Bst_node *p, Direction d)
Get pointer to link in direction d.
Bst::Iterator Iterator
Forward iterator for the tree.
__Bst_iter< Node, Node, Fwd > Iterator
Forward iterator.
~Avl_tree() noexcept
Destroy the tree.