AVL Trees
Since all the operations' complexities depend on the height of a node in the BST, it is good to minimise the height as much as possible. Thus, we come up with the concept of a "self-balancing" AVL tree.
What is an AVL Tree¶
An AVL tree is a tree that satisfies the balanced height property. We say that the tree is height-balanced
- In this tree, each node has an additional attribute:
py x.height
. Ifx is None
,x.height := -1
- This is a tree such that the heights of the left and the right subtrees of each node differ by at most 1. This is known as the balance height property. (This is the same as the tree being nearly complete)
The height of an AVL tree with \(n\) nodes is \(\left\lfloor \log n \right\rfloor\)
- A node of a BST is left-heavy if the height of the left subtree is > height of the right subtree + 1
- A node of a BST is right-heavy if the height of the right subtree is > height of the left subtree + 1
Operations¶
Left Rotate¶
This is performed on a node which is right heavy
1 2 3 4 5 6 7 8 9 10 |
|
Right Rotate¶
This is performed on a node which is left heavy
1 2 3 4 5 6 7 8 9 10 |
|
Double Left (Right Left) Rotate¶
This is performed on a node which is right heavy and the right sub tree itself is left heavy
1 2 3 |
|
Double Right (Left Right) Rotate¶
This is performed on a node which is left heavy and the left sub tree itself is right heavy
1 2 3 |
|