Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVL tree balance

I have implemented an AVL tree, but I have a problem.

Suppose I have following tree:

enter image description here

And after adding another node:

enter image description here

Now I must rotate node5 to left:

enter image description here

But after rotation, it is still unbalanced.

Where am I making a mistake?

like image 738
MRB Avatar asked Oct 09 '13 16:10

MRB


People also ask

How do you balance an AVL tree?

The balance factor of a node is the difference in the heights of the left and right subtrees. The balance factor of every node in the AVL tree should be either +1, 0 or -1. In case a node is imbalanced, a rotation technique can be applied to balance it.

What is balance factor in AVL tree?

Balance factor of a node in an AVL tree is the difference between the height of the left subtree and that of the right subtree of that node. Balance Factor = (Height of Left Subtree - Height of Right Subtree) or (Height of Right Subtree - Height of Left Subtree)

Is AVL tree balanced or unbalanced?

AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.


1 Answers

The presented scenario conforms to the Right-Left case from this Wikipedia description.

Your mistake is that you rotate the imbalanced node (5) at once, without first performing a rotation of its sub-tree.

In general having P as the unbalanced node, L as its left sub-tree and R as its right sub-tree the following rules should be followed at insertion:

balance(N) = Depth(Nleft) - Depth(Nright)

if (balance(P) > 1)  // P is node 5 in this scenario
{
    if (balance(L) < 0)
    {
        rotate_left(L);
    }

    rotate_right(P);
}
else if (balance(P) < -1) // P is node 5 in this scenario
{
    if (balance(R) > 0)  // R is node 11 in this scenario
    {
        rotate_right(R); // This case conforms to this scenario
    }

    rotate_left(P);      // ... and of course this, after the above
}

So sometimes two rotations need to be performed, and sometimes only one.

This is nicely visualized at Wikipedia:

enter image description here

The top row shows situations when two rotations are needed. The middle row presents possible scenarios when one rotation is sufficient. Additional rotations transform any top-row scenario to the middle-row scenario.

In particular, for this tree:

enter image description here

After 7 is added:

enter image description here

The balance of 5 is 2. This conforms to the scenario marked with a comment above in the pseudo-code and also to the top-row scenario (the one on the right) in the Wikipedia picture. So before 5 is left-rotated, its right sub-tree 11 needs to be right-rotated:

enter image description here

And it becomes:

enter image description here

Only now, it's the simple case (middle-row right scenario in the Wikipedia picture) to restore balance at 5 by one left-rotation:

enter image description here

And the tree becomes balanced again:

enter image description here

like image 95
BartoszKP Avatar answered Oct 12 '22 21:10

BartoszKP