2#include "../include/cppx.h"
7template <
typename T>
int AVLTree<T>::get_height(
const Node<T> *node)
const
9 return node ? node->m_height : 0;
12template <
typename T>
int AVLTree<T>::get_balance_factor(
const Node<T> *node)
const
14 return node ? get_height(node->p_left) - get_height(node->p_right) : 0;
17template <
typename T>
void AVLTree<T>::update_height(Node<T> *node)
21 int left_h = get_height(node->p_left);
22 int right_h = get_height(node->p_right);
23 node->m_height =
static_cast<std::int8_t
>(1 + std::max(left_h, right_h));
26template <
typename T> Node<T> *AVLTree<T>::rotate_left(Node<T> *x)
28 Node<T> *y = x->p_right;
29 x->p_right = y->p_left;
36template <
typename T> Node<T> *AVLTree<T>::rotate_right(Node<T> *x)
38 Node<T> *y = x->p_left;
39 x->p_left = y->p_right;
46template <
typename T> Node<T> *AVLTree<T>::rebalance(Node<T> *node)
49 int balance = get_balance_factor(node);
53 if (get_balance_factor(node->p_left) < 0)
54 node->p_left = rotate_left(node->p_left);
55 return rotate_right(node);
60 if (get_balance_factor(node->p_right) > 0)
61 node->p_right = rotate_right(node->p_right);
62 return rotate_left(node);
72 this->p_head = this->make_node(
val);
76 std::vector<Node<T> **>
path;
82 if (
val < (*link)->m_data)
83 link = &(*link)->p_left;
84 else if (
val > (*link)->m_data)
85 link = &(*link)->p_right;
94 *(*it) = rebalance(*(*
it));
105 std::vector<Node<T> **>
path;
110 if (
val < (*link)->m_data)
113 link = &(*link)->p_left;
115 else if (
val > (*link)->m_data)
118 link = &(*link)->p_right;
126 this->m_pool.deallocate(
node);
128 else if (!
node->p_right)
131 this->m_pool.deallocate(
node);
137 while ((*succ_link)->p_left)
146 this->m_pool.deallocate(
succ);
150 *(*it) = rebalance(*(*
it));
157 *(*it) = rebalance(*(*
it));
void remove(const T &val) override
void insert(const T &val) override