Created
February 19, 2023 16:10
-
-
Save maxgoren/e190ba4f8892e44baf0ef76d04628cfb to your computer and use it in GitHub Desktop.
A very straight forward Iterative AVL insertion algorithm using parent pointers based on CLRS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T, class T2> | |
class AVLTree { | |
private: | |
struct node { | |
T key; | |
T2 value; | |
int height; | |
node* left; | |
node* right; | |
node* parent; | |
node(T k, T2 v) { | |
key = k; value = v; height = 0; left = right = parent = nullptr; | |
} | |
}; | |
node* root; | |
int height(node* h) { | |
return (h == nullptr) ? -1:h->height; | |
} | |
int max(int a, int b) { return (a < b) ? b:a; } | |
void left_rotate(node *x) { | |
node *y = x->right; | |
if (y) { | |
x->right = y->left; | |
if (y->left) y->left->parent = x; | |
y->parent = x->parent; | |
} | |
if (!x->parent) root = y; | |
else if (x == x->parent->left) x->parent->left = y; | |
else x->parent->right = y; | |
if (y) y->left = x; | |
x->parent = y; | |
x->height = 1 + max(height(x->left), height(x->right)); | |
y->height = 1 + max(height(y->left), height(y->right)); | |
} | |
void right_rotate(node *x) { | |
node *y = x->left; | |
if (y) { | |
x->left = y->right; | |
if (y->right) y->right->parent = x; | |
y->parent = x->parent; | |
} | |
if (!x->parent) root = y; | |
else if (x == x->parent->left) x->parent->left = y; | |
else x->parent->right = y; | |
if (y) y->right = x; | |
x->parent = y; | |
x->height = 1 + max(height(x->left), height(x->right)); | |
y->height = 1 + max(height(y->left), height(y->right)); | |
} | |
int balanceFactor(node* h) { | |
return height(h->left) - height(h->right); | |
} | |
void balance(node* x) { | |
while (x && x->parent) { | |
node *y = x->parent; | |
y->height = 1 + max(height(y->left), height(y->right)); | |
if (height(y->left) > height(y->right) + 1) | |
right_rotate(y); | |
if (height(y->right) > height(y->left) + 1) | |
left_rotate(y); | |
x = x->parent; | |
} | |
} | |
public: | |
AVLTree() { | |
root = nullptr; | |
} | |
void insert(T key, T2 value) { | |
node* x = root; | |
node* p = x; | |
while (x) { | |
p = x; | |
x = (key < x->key) ? x->left:x->right; | |
} | |
x = new node(key, value); | |
if (!p) root = x; | |
else if (key < p->key) p->left = x; | |
else p->right = x; | |
x->parent = p; | |
balance(x); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment