00:01
Hello students, so avl tree insertion.
00:03
To insert a value into the avl tree, we first follow the standard binary search tree insertion algorithm.
00:10
We start at the root node and compare the new value to the current node's value.
00:14
If the new value is less than the current node's value, we go to the left.
00:19
If the new node value is greater than the current node, we go to the right.
00:24
So in this way, we form this is left, this is right, and this is root.
00:31
So if root node having the less value as compared to root node, then left.
00:37
If having the value greater than the root node, then right.
00:41
So in this way, we insert the node.
00:43
So this process goes on until we find a null node and there we insert the new node.
00:49
Once the new node is inserted, we need to check if the tree is still balanced.
00:53
We do this by calculating the balance factor of each node.
00:57
The balance factor of the node is the difference between the heights of the left and the right subtree.
01:02
A balanced avl tree has all of its nodes with a balance factor of either minus one, zero or one.
01:10
So this is the factor associated with each node, either one, minus one or zero.
01:16
If any node has a balance factor of greater than one or less than minus one, the tree is unbalanced and we need to perform a rotation to rebalance it.
01:25
There are four types of rotation in the avl tree left, right, left, right and right, left.
01:31
So this type of rotation that we perform depends on the balancing factor of the unbalanced node and its parent node.
01:40
Example, let's insert here.
01:42
So here we have to insert the values into the avl tree.
01:47
15, 20, 24, 10, 13, 7, 30, 36, 25.
01:51
These values are given to us.
01:53
Now we have to insert.
01:54
So initial tree 15.
01:56
So here root is 15.
01:58
Now the next is here 10, 20, 7, 30, 24.
02:02
So we insert in 24.
02:04
So we start at the root node 15 and compare 24 to 15.
02:10
So we have to here specially insert the 24.
02:14
So we will come, we will start with the, when we are inserting, we will start from the root node.
02:20
So first the root node value is 15.
02:23
So 24 is compared with, 24 is compared with 15 and 24 is greater.
02:28
So we go to the right.
02:30
So here we decide in which part we have to go.
02:34
So left or right.
02:36
So here we find that we have to go to the right by, so we have the 24 value.
02:44
So we reach the node with the value 20.
02:47
So 24, 20 is greater, 24 is greater than 20.
02:51
So we go to the right again.
02:53
We reach a null node.
02:54
So we insert the new node with the value of 24 here...