0% found this document useful (0 votes)
56 views5 pages

Avl Tree Insert

The document describes the steps to insert a node into an AVL tree while maintaining the balance property. It involves: 1) Inserting the node using standard binary search tree insertion. 2) Computing the heights of nodes on the path from the inserted node to the root. 3) Checking if the tree is balanced and performing rotations if needed to balance it. 4) There are four types of rotations - left-left, right-right, left-right, right-left depending on the position of the unbalanced node.

Uploaded by

adi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

Avl Tree Insert

The document describes the steps to insert a node into an AVL tree while maintaining the balance property. It involves: 1) Inserting the node using standard binary search tree insertion. 2) Computing the heights of nodes on the path from the inserted node to the root. 3) Checking if the tree is balanced and performing rotations if needed to balance it. 4) There are four types of rotations - left-left, right-right, left-right, right-left depending on the position of the unbalanced node.

Uploaded by

adi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Avem urm relatie: z = critical node; y = r/l child of z; x = r/l child of y; w = inserted node.

The first three


nodes are on path from w to root.
If the value of the inserted node is smaller than the value of y where x < y < z:
z
y
x
if (key < critical_node->left->data)
If the value of the inserted node is bigger than the value of y where x > y and both < z
z
y
x
if (key > critical_node->left->data)
If the value of the inserted node is bigger than that of y where z < y < x
z
y
x
if (key > critical_node->right->data)
If the value of the inserted node is smaller than that of y where z < y and x < y.
z
y
x
If (key < critical_node->right->data)
De fiecare data ne-am folosit pentru comparatie de nodul y din mijloc deoarece nodul introdus poate fi ori
mai mic ori mai mare ca x sau chiar sa fie x. Daca suntem in rightsubtree si e mai mic ca y stim
intotdeauna ca suntem in in cazul right-right-left iar e mai mare suntem in cazul right-right-right. Daca
suntem in leftsubtree, daca e mai mic ca y stim intotdeauna ca suntem in cazul left-left-left iar daca e mai
mare ca y stim ca suntem in cazul left-left-right.

he nodes whose height will change are the ones that are on the path from the inserted node to the root.
Only those can change their height values. Secondly, the nearest ancestor node of the inserted node whose
height is unbalanced is called the critical node. To find this critical node we must perform the following
steps: after each insertion of a node, its obvious that its height is 0 because it has no children so no left
and right subtrees so no height. however, one we insert a node, the height of the ancestor nodes from the
inserted node to the root are changed.

Insertion in an AVL tree is hard because it implies many steps. The reasoning is: when we insert a node,
we first call the search algorithm to properly insert it (to maintain the order). Then, we compute the height
of its ancestors. Then we check if the tree is balances by subtracting the height of the left child of root
from the height of the right child of root and if this difference is not -1, 0 or 1 it means that the tree is not
balanced case in which we rotate the tree.
Step 1. Create the structure of the AVL tree as the one of binary search tree but with one additional
variable for the height.
struct AVL {
int data;
int height;
struct AVL* left;
struct AVL* right;
};
Step 2. Create a function new_node which takes input data, allocates memory for a new node, assign to
new_node->data = data, sets the two pointers to NULL, sets the variable for height to 0 and returns the
node. This function is used for creating the new node.
struct AVL* AVL_new_node(int data) {
struct AVL* node = (struct AVL*)malloc(sizeof(struct AVL));
node->data = data;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
Step 3. Create another function height which takes as input a node and which establishes the height of a
node. Note that this function is called inside the insert function and it works from bottom to up. After we
insert, we go up to the root on the same path that we traversed, each time visiting an ancestor of the new
node and computing its heigth. When we find We compute the height of the ancestors by using the
following reasoning: if the ancestor has only one child means that we are on the same path and the height
of the current node is that of its child + 1. If the ancestor has two children, it means that besides the path
that we traversed, there is also another path. In this situation, we compare the heights of both paths and
we assign to the current node the bigger height + 1.
void height(struct AVL* node) {
if (node->left == NULL && node->right == NULL) node->height = 1;
else if (node->left == NULL) node->height = node->right->height + 1;
else if (node->right == NULL) node->height = node->left->height + 1;
else {
if (node->left->height > node->right->height) node->height = node->left->height + 1;
else node->height = node->right->height + 1;
}
}
Step 4. Create another function rotation which is used to rebalance the tree. In rotations we always work
with three nodes: the critical node, its child and its parent. There are four kinds of rotations. We notate the
critical node with C, its parent un P and its child with B.

Step 4.1. LL rotation: Because we have a binary search tree we have the following relationship: C>B. To
rebalance it, we perform the following operation: we make C the right child of B, then we make B the left
child of P and then we make the left child of C NULL (because it was pointing to B). To perform the rotation
in our algorithm, we proceed the following way: after we inserted the node, we establish its height, then
we go up while computing heights until we find a node on the path to the root that has an unbalanced
factor. This node is called the critical node. When we find it, we go up again to its parent and then:
B->right = C
// (A is already the left child of B)
P->left = B
C->left (which points to B) = NULL
Thus:
void AVL_LL_rotation(struct AVL* parent) { // we pass to this function the parent of the critical node
parent->left->left->right = parent->left; // the right child of B = C
parent->left = parent->left->left; // the left child of P = B
parent->left->right->left = NULL; // we made the children of node C null
}
Step 4.2. RR rotation: 4
3 5
6
7
We have the following relationship: C<B. Here we make C the left child of B, then we make B the right
child of P and then we make the right child of C NULL (because it was pointing to B). The reasoning in our
algorithm is the same LL rotation.Thus:
B->left = C
// (A is already the right child of B).
P->right = B
C->right (which points to B) = NULL;
Thus:
void AVL_RR_rotation(struct AVL* parent) {
parent->right->right->left = parent->right; // the left child of B = C
parent->right = parent->right->right; // the right child of P = B
parent->right->left->right = NULL; // we made the childre of node C
null
}
Step 4.3. LR rotation: 4
3 5
2
1
Step 5. Create the insert function. Insert fuction.
We first compare the data of the new node with the data of the current node so we know where to go, left
or right. Suppose left.
Then check if the left child is NULL.
If it is, create the new node there, and call the function for height to give value to height of this node and
find the height of the ancestor nodes. This algorithm works because we establish all the heights every
time we insert a node. Thus, when only the root we know its height. Then, when we insert a new node, we
also know its height which is 1. We only need to compiute the height of the root which will be the height
of its child + 1. And so on for all nodes.
Step 5.3. Check if the tree is balanced by subtracting the height of the left child of root from the height of
the right child of root. If the difference is 0,1 or -1 it means that the tree is balanced and we stop here. If
its not, we continue.
Step 4.1. Check which rotation we need to perform.
struct AVL {
int data;
int height;
struct AVL* left;
struct AVL* right;
};

struct AVL* AVL_new_node(int data) {


struct AVL* node = (struct AVL*)malloc(sizeof(struct AVL));
node->data = data;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}

void AVL_insert(struct AVL* root, int data ) {


if (data < root->data) {
if (root->left == NULL) root->left = AVL_new_node(data);
else AVL_insert(root->left, data);
}
else {
if (root->right == NULL) root->right = AVL_new_node(data);
else AVL_insert(root->right, data);
}
}

int AVL_height(struct AVL* curr) {


if (curr == NULL) return 0; // some authors wrote here return -1
else {
int left = AVL_height(curr->left);
int right = AVL_height(curr->right);
if (left > right) return 1 + left;
else return 1 + right;
}
}

void AVL_rebalance(struct AVL* root) {


}

void main() {
// Create the root.
struct AVL* root = AVL_new_node(data);
// Insert a new node.
AVL_insert(root, data);
if ((AVL_height(root->left) - AVL_height(root->right)) > 1 || (AVL_height(root-
>left) - AVL_height(root->right)) < -1) AVL_rebalance(root);
}
void height(struct AVL* node) {
if (node->left == NULL && node->right == NULL) node->height = 1;
else if (node->left == NULL) node->height = node->right->height + 1;
else if (node->right == NULL) node->height = node->left->height + 1;
else {
if (node->left->height > node->right->height) node->height = node->left-
>height + 1;
else node->height = node->right->height + 1;
}
}

void AVL_LL_rotation(struct AVL* parent) { // we pass to this function the parent of


the critical node
parent->left->left->right = parent->left; // the right child of B = C
parent->left = parent->left->left; // the left child of P = B
parent->left->right->left = NULL; // we made the children of node C null
}
void AVL_RR_rotation(struct AVL* parent) {
parent->right->right->left = parent->right; // the left child of B = C
parent->right = parent->right->right; // the right child of P = B
parent->right->left->right = NULL; // we made the childre of node C
null
}

You might also like