AVL specific case balancing - algorithm

I have been revisiting old basic algorithms for a class I an online course I am about to watch after christmass. It had been a generally light read, until I reached AVLs. Back when I was learning them, I do not remember having any problem, but after a little more than 10 years, I am no longer as good. While I solve most cases easily, I have stuck for more than 5 hrs on this example:
51
/ \
19 55
/ \ \
10 37 61
/ \
28 46
Inserting 40 into the tree, left child of 46, requires a Single Left Rotation to fix the balance...why? Isn't 40 inserted into the left side of the Right child of 19, who becomes unbalanced? Why is it not a double rotation? What do I fail to see?

Here is how it goes:
1. After insertion:
51
/ \
19 55
/ \ \
10 37 61
/ \
28 46
/
40
It is clear that 19 node becomes unbalanced.
2. That's why it is rotated left once. So it becomes:
51
/ \
37 55
/ \ \
19 46 61
/ \ /
10 28 40
Now the tree is balanced again.

And funnily enough, trying to explain my problem revealed what I was doing wrong.
The tree is left heavy and tree's left subtree is NOT right heavy, so it's a single rotation.

Related

Binary Search Tree, How should I rotate this tree to balance

I am trying to rotate a tree to keep it balanced, but I am having hard time with this example. I am not so sure what I am doing wrong here.
I have below tree with L/R height difference 1 at node 30. So, I guess this tree is balanced.
30
/ \
20 60
/ \
10 25
I am adding 22 here and this is what I get after adding 22.
30
/ \
20 60
/ \
10 25
/
22
At node 20, L/R height difference is 1, but at node 30 it is 2. So, I guess it is not balanced any more. I am trying to right rotate the tree to balance it, but I am getting below tree.
20
/ \
10 30
/ \
25 60
/
22
After the rotation, node 20 has L/R height difference of 2 still.
Where am I doing wrong? Can this tree be balanced using rotation?
I can balance the tree using sorted array method like below, but I am really confused about rotation balance in this example.
22 25
/ \ / \
20 30 20 30
/ / \ / \ \
10 25 60 10 22 60
What am I doing wrong here?
Thanks a lot!!
There are basically four type of rotation in the AVL Tree.
Right-left
Left-Right
Left-Left
Right-Right
In your case, Left-Right should be applicable.
Here you need to perform two steps.
1:- Left rotate from 20 node. So your tree should like as below.
30
/ \
25 60
/
20
/ \
10 22
2:- Right rotate from 30 node. So your tree should be like as below.
25
/ \
20 30
/ \ \
10 22 60
You can refer the N website to understand the behavior. Here is one of the best link
This case requires a double rotation: rotate 25 up twice. I'm assuming that you're thinking about AVL trees, but all of the standard balanced binary trees need double rotations in some cases.

AVL Rotation - Which node to rotate

I have read many sources about AVL trees, but did not find anyone addressing this issue: When AVL tree gets unbalanced, which node should be rotated first?
Assuming I have the tree:
10
/ \
5 25
/
20
and I'm trying to add 15, both the root and its child 25 will be unbalanced.
10
/ \
5 25
/
20
/
15
I could do a RR rotation (or single rotation) of 25, resulting in the following tree:
10
/ \
5 20
/\
15 25
or a RL rotation (double rotation) about the root, creating the following tree:
20
/ \
10 25
/ \
5 15
I am confused about which rotation is the most suitable here and in similar cases.
The RR rotation is correct here. The rotation should be done as soon (as low) as the rule is broken. Which is for 25 here.
The higher rotations first don't necessarily break the rule and secondly would become too complex although it doesn't seem so here at the first sight.

More than one rotation needed to balance an AVL Tree?

My best guess is that one rotation is always enough to balance an AVL tree when you insert or delete ONE element from an already balanced AVL tree.
Is one rotation always enough?
An example will help where more than one rotations are needed.
PS: I count RL/LR rotations as one rotation only.
For insertion I believe one is enough.
but for deletion:
consider this tree:
50
/ \
25 75
/ \ / \
15 40 60 80
/ / \ \
35 55 65 90
/
62
delete 15 , first the 25's balance factor is destructed, one rotation:
50
/ \
35 75
/ \ / \
25 40 60 80
/ \ \
55 65 90
/
62
but still, we have to check, now the tree's root's balance factor is destructed, have to be rotated again:
60
/ \
50 75
/ \ / \
35 55 65 80
/ \ / \
25 40 62 90
For insert 1 rotation is at most.
For delete the number of rotation is bounded by O(log(n)). Log(n) is the height of the tree.
More explanation on AVL deletion.
When you delete a node from AVL you might cause the tree unbalanced, which you have to trace back to the point where it is unbalanced. If the unbalanced point is the root. You have to rebalance the tree from top to bottom.

AVl Tree adoption

So say I've inserted these numbers to an AVL tree: 40, 20, 25, 60, 65, 70. My tree should look like:
25
/ \
20 60
/ \
40 65
\
70
Which means doing a rotation with an adoption, like this?
60
/ \
25 65
/ \ \
20 40 70
Is this right?
Simply put, Yes
Try finding the source code for an existing implementation, that should help you in the future.

Inserting into a 2-3 Tree

I'm not sure if I am understanding the insertion process of a 2-3 Tree correctly. Say I have the tree:
and I want to insert the value 95 into it, would this be the proper new tree?
Yes this is correct.
Inserting 95 would place 3 children in the rightmost leaf (not allowed)
40
/ \
20 60, 80
/ \ / | \
10 30 50 70 90,95,100 <- not valid
The 3 nodes in the leaf make 95 move to the parent node, but now there are 3 nodes in the parent:
40
/ \
20 60,80,95 <- not valid
/ \ / | \
10 30 50 70 90,100
Moving 95 up causes the parent node to split:
40
/ \
20 80
/ \ / \
10 30 60 95
/ \ / \
50 70 90 100 valid
this is incorrect. the height of 2-3 tree is consistent hence you will split the parent not the child.
I think you are wrong. You are violating one fact about 2-3 tree: all leaves have same depth.
reference:
http://pages.cs.wisc.edu/~vernon/cs367/notes/10.23TREE.html#operations
Here is trace of insertion I have in mind:
Please let me know if this is incorrect.

Resources