yfast 0.6.1
Loading...
Searching...
No Matches
avl.h
1#ifndef _YFAST_INTERNAL_AVL_H
2#define _YFAST_INTERNAL_AVL_H
3
4#include <yfast/internal/bst.h>
5
6namespace yfast::internal {
7
8template <typename Key, typename T>
9struct AVLNodeBase: public BSTNodeBase<Key, T> {
10 using typename BSTNodeBase<Key, T>::Child;
11
12 using BSTNodeBase<Key, T>::_left;
13 using BSTNodeBase<Key, T>::_right;
14
15 explicit AVLNodeBase(const Key& key): BSTNodeBase<Key, T>(key) {}
16
17 [[nodiscard]] bool is_left_heavy() const { return Child(_left).get_bit(0); }
18 [[nodiscard]] bool is_right_heavy() const { return Child(_right).get_bit(0); }
19 [[nodiscard]] bool is_balanced() const { return !is_left_heavy() && !is_right_heavy(); }
20
21 void set_left_heavy() {
22 Child left_child = _left;
23 left_child.set_bit(0);
24 _left = left_child.value;
25
26 Child right_child = _right;
27 right_child.clear_bit(0);
28 _right = right_child.value;
29 }
30 void set_right_heavy() {
31 Child left_child = _left;
32 left_child.clear_bit(0);
33 _left = left_child.value;
34
35 Child right_child = _right;
36 right_child.set_bit(0);
37 _right = right_child.value;
38 }
39 void set_balanced() {
40 Child left_child = _left;
41 left_child.clear_bit(0);
42 _left = left_child.value;
43
44 Child right_child = _right;
45 right_child.clear_bit(0);
46 _right = right_child.value;
47 }
48};
49
50}
51
52#endif
Definition bst.h:11