10template <
typename T>
int AVLTree<T>::get_height(
const Node<T> *node)
const
12 return node ? node->m_height : 0;
15template <
typename T>
int AVLTree<T>::get_balance_factor(
const Node<T> *node)
const
17 return node ? get_height(node->p_left) - get_height(node->p_right) : 0;
20template <
typename T>
void AVLTree<T>::update_height(Node<T> *node)
24 int left_h = get_height(node->p_left);
25 int right_h = get_height(node->p_right);
26 node->m_height =
static_cast<std::int8_t
>(1 + std::max(left_h, right_h));
29template <
typename T> Node<T> *AVLTree<T>::rotate_left(Node<T> *x)
31 Node<T> *y = x->p_right;
32 x->p_right = y->p_left;
39template <
typename T> Node<T> *AVLTree<T>::rotate_right(Node<T> *x)
41 Node<T> *y = x->p_left;
42 x->p_left = y->p_right;
49template <
typename T> Node<T> *AVLTree<T>::rebalance(Node<T> *node)
52 int balance = get_balance_factor(node);
56 if (get_balance_factor(node->p_left) < 0)
57 node->p_left = rotate_left(node->p_left);
58 return rotate_right(node);
63 if (get_balance_factor(node->p_right) > 0)
64 node->p_right = rotate_right(node->p_right);
65 return rotate_left(node);
75 this->p_head = this->make_node(
val);
79 std::vector<Node<T> **>
path;
85 if (
val < (*link)->m_data)
86 link = &(*link)->p_left;
87 else if (
val > (*link)->m_data)
88 link = &(*link)->p_right;
97 *(*it) = rebalance(*(*
it));
108 std::vector<Node<T> **>
path;
113 if (
val < (*link)->m_data)
116 link = &(*link)->p_left;
118 else if (
val > (*link)->m_data)
121 link = &(*link)->p_right;
129 this->m_pool.deallocate(
node);
131 else if (!
node->p_right)
134 this->m_pool.deallocate(
node);
140 while ((*succ_link)->p_left)
149 this->m_pool.deallocate(
succ);
153 *(*it) = rebalance(*(*
it));
160 *(*it) = rebalance(*(*
it));
169 return this->p_head ?
static_cast<int>(this->p_head->m_height) : 0;
int height() const override
void remove(const T &val) override
void insert(const T &val) override