22 explicit avl_tree(std::vector<T> _elements = {})
noexcept : root(nullptr) {
23 if (!_elements.empty()) {
24 for (T &x : _elements) {
61 root = _insert(root, key);
80 T
get_root()
const {
return this->root->info; }
87 bool search(T key) {
return _search(root, key); }
97 std::vector<T> ino = this->inorder();
107 std::vector<T> ino = this->inorder();
116 size_t size()
const {
return _size; }
122 root = _remove(root, key);
133 [&](std::shared_ptr<node> callbacked) {
134 path.push_back(callbacked->info);
146 [&](std::shared_ptr<node> callbacked) {
147 path.push_back(callbacked->info);
159 [&](std::shared_ptr<node> callbacked) {
160 path.push_back(callbacked->info);
171 std::vector<std::vector<T>> path;
172 std::queue<std::shared_ptr<node>> q;
175 size_t size = q.size();
176 std::vector<T> level;
177 for (
size_t i = 0; i < size; i++) {
178 std::shared_ptr<node> current = q.front();
180 level.push_back(current->info);
182 q.push(current->left);
184 if (current->right) {
185 q.push(current->right);
188 path.push_back(level);
196 friend std::ostream & operator << (std::ostream &out,
avl_tree<T> &t){
197 std::vector<std::vector<T> > order = t.
inorder();
198 for(
int i = 0; i<order.size(); i++){
199 if(i != order.size() - 1){
200 out << order[i] <<
", ";
203 out << order[i] <<
'\n';
217 typedef struct node {
220 std::shared_ptr<node> left;
221 std::shared_ptr<node> right;
222 node(T key) : info(key), left(nullptr), right(nullptr) {}
225 std::shared_ptr<node> root;
228 int64_t height(std::shared_ptr<node> root) {
231 return 1 + std::max(height(root->left), height(root->right));
234 std::shared_ptr<node> createNode(T info) {
235 std::shared_ptr<node> nn = std::make_shared<node>(info);
239 int64_t getBalance(std::shared_ptr<node> root) {
240 return height(root->left) - height(root->right);
243 std::shared_ptr<node> rightRotate(std::shared_ptr<node> root) {
244 std::shared_ptr<node> t = root->left;
245 std::shared_ptr<node> u = t->right;
251 std::shared_ptr<node> leftRotate(std::shared_ptr<node> root) {
252 std::shared_ptr<node> t = root->right;
253 std::shared_ptr<node> u = t->left;
259 std::shared_ptr<node> minValue(std::shared_ptr<node> root) {
260 if (root->left ==
nullptr)
262 return minValue(root->left);
265 std::shared_ptr<node> _insert(std::shared_ptr<node> root, T item) {
266 std::shared_ptr<node> nn = createNode(item);
269 if (item < root->info) {
270 root->left = _insert(root->left, item);
272 else if (item > root->info) {
273 root->right = _insert(root->right, item);
278 int b = getBalance(root);
280 if (getBalance(root->left) < 0)
281 root->left = leftRotate(root->left);
282 return rightRotate(root);
284 if (getBalance(root->right) > 0)
285 root->right = rightRotate(root->right);
286 return leftRotate(root);
291 std::shared_ptr<node> _remove(std::shared_ptr<node> root, T key) {
294 if (key < root->info)
295 root->left = _remove(root->left, key);
296 else if (key > root->info)
297 root->right = _remove(root->right, key);
301 std::shared_ptr<node> temp = root->left;
304 }
else if (!root->left) {
305 std::shared_ptr<node> temp = root->right;
309 std::shared_ptr<node> temp = minValue(root->right);
310 root->info = temp->info;
311 root->right = _remove(root->right, temp->info);
316 bool _search(std::shared_ptr<node> root, T key) {
318 if (root->info < key) {
320 }
else if (root->info > key) {
329 void _inorder(std::function<
void(std::shared_ptr<node>)> callback,
330 std::shared_ptr<node> root)
const {
332 _inorder(callback, root->left);
334 _inorder(callback, root->right);
338 void _postorder(std::function<
void(std::shared_ptr<node>)> callback,
339 std::shared_ptr<node> root)
const {
341 _inorder(callback, root->left);
342 _inorder(callback, root->right);
347 void _preorder(std::function<
void(std::shared_ptr<node>)> callback,
348 std::shared_ptr<node> root)
const {
351 _inorder(callback, root->left);
352 _inorder(callback, root->right);