CPPX 3.2.1
A Modern C++ Utility Library
Loading...
Searching...
No Matches
avl.tpp
Go to the documentation of this file.
1#pragma once
2#include "cppx/cppx.h"
3
4#include <algorithm>
5#include <stdexcept>
6
7namespace stl_ext
8{
9
10template <typename T> int AVLTree<T>::get_height(const Node<T> *node) const
11{
12 return node ? node->m_height : 0;
13}
14
15template <typename T> int AVLTree<T>::get_balance_factor(const Node<T> *node) const
16{
17 return node ? get_height(node->p_left) - get_height(node->p_right) : 0;
18}
19
20template <typename T> void AVLTree<T>::update_height(Node<T> *node)
21{
22 if (!node)
23 return;
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));
27}
28
29template <typename T> Node<T> *AVLTree<T>::rotate_left(Node<T> *x)
30{
31 Node<T> *y = x->p_right;
32 x->p_right = y->p_left;
33 y->p_left = x;
34 update_height(x);
35 update_height(y);
36 return y;
37}
38
39template <typename T> Node<T> *AVLTree<T>::rotate_right(Node<T> *x)
40{
41 Node<T> *y = x->p_left;
42 x->p_left = y->p_right;
43 y->p_right = x;
44 update_height(x);
45 update_height(y);
46 return y;
47}
48
49template <typename T> Node<T> *AVLTree<T>::rebalance(Node<T> *node)
50{
51 update_height(node);
52 int balance = get_balance_factor(node);
53
54 if (balance > 1)
55 {
56 if (get_balance_factor(node->p_left) < 0)
57 node->p_left = rotate_left(node->p_left);
58 return rotate_right(node);
59 }
60
61 if (balance < -1)
62 {
63 if (get_balance_factor(node->p_right) > 0)
64 node->p_right = rotate_right(node->p_right);
65 return rotate_left(node);
66 }
67
68 return node;
69}
70
71template <typename T> void AVLTree<T>::insert(const T &val)
72{
73 if (!this->p_head)
74 {
75 this->p_head = this->make_node(val);
76 return;
77 }
78
79 std::vector<Node<T> **> path;
80 Node<T> **link = &this->p_head;
81
82 while (*link)
83 {
84 path.push_back(link);
85 if (val < (*link)->m_data)
86 link = &(*link)->p_left;
87 else if (val > (*link)->m_data)
88 link = &(*link)->p_right;
89 else
90 return;
91 }
92
93 *link = this->make_node(val);
94
95 for (auto it = path.rbegin(); it != path.rend(); ++it)
96 {
97 *(*it) = rebalance(*(*it));
98 }
99}
100
101template <typename T> void AVLTree<T>::remove(const T &val)
102{
103 struct Frame
104 {
105 Node<T> **link;
106 };
107
108 std::vector<Node<T> **> path;
109 Node<T> **link = &this->p_head;
110
111 while (*link)
112 {
113 if (val < (*link)->m_data)
114 {
115 path.push_back(link);
116 link = &(*link)->p_left;
117 }
118 else if (val > (*link)->m_data)
119 {
120 path.push_back(link);
121 link = &(*link)->p_right;
122 }
123 else
124 {
125 Node<T> *node = *link;
126 if (!node->p_left)
127 {
128 *link = node->p_right;
129 this->m_pool.deallocate(node);
130 }
131 else if (!node->p_right)
132 {
133 *link = node->p_left;
134 this->m_pool.deallocate(node);
135 }
136 else
137 {
138 Node<T> **succ_link = &node->p_right;
139 std::vector<Node<T> **> succ_path;
140 while ((*succ_link)->p_left)
141 {
142 succ_path.push_back(succ_link);
143 succ_link = &(*succ_link)->p_left;
144 }
145
147 node->m_data = succ->m_data;
148 *succ_link = succ->p_right;
149 this->m_pool.deallocate(succ);
150
151 for (auto it = succ_path.rbegin(); it != succ_path.rend(); ++it)
152 {
153 *(*it) = rebalance(*(*it));
154 }
155 *link = rebalance(node);
156 }
157
158 for (auto it = path.rbegin(); it != path.rend(); ++it)
159 {
160 *(*it) = rebalance(*(*it));
161 }
162 return;
163 }
164 }
165}
166
167template <typename T> int AVLTree<T>::height() const
168{
169 return this->p_head ? static_cast<int>(this->p_head->m_height) : 0;
170}
171
172} // namespace stl_ext
int height() const override
Definition avl.tpp:167
void remove(const T &val) override
Definition avl.tpp:101
void insert(const T &val) override
Definition avl.tpp:71
Definition cppx.h:11