AVL trees
An AVL (Adel'son-Velskii - Lan'dis) tree is a full binary tree (with failure nodes) satisfying at every internal node the balance criterion | h(L) - h(R) | <= 1. (One could describe a k-AVL tree in which the maximum difference could be k or less, or extend the concept to k-ary trees.) One can look at the definition of an AVL tree as a context-sensitive inductive definition.
The key results on AVL trees are (1) that the maximum depth of an AVL tree is O (lg n), and (2) that the AVL invariant can be restored on insertion or deletion with O (lg n) work. We outline these results below.
insert (tree T, key k) returns boolean
use binary search on T to find the insertion spot v for k;
if k is found, return an error (and exit);
else make a new node containing k with failure node children;
repeat
follow parent pointers from v up the tree visiting nodes w
until one of the following occurs:
(1) reach the root: tree is balanced;
(2) the two children of w have the same height:
tree is balanced;
(3) the tree is out of balance at w:
look at the last two pointers (or find k from w);
if they both went in the same direction (e.g., right-right)
perform a rotation at w in the opposite direction;
otherwise /* they went left-right or right-left */
/* --- first case is illustrated */
perform a left rotation at the left child of w
followed by a right rotation at w
/* brings the grandchild to the root */
/* rotation will always restore the balance invariant */
/* proof given in class */
end
We will cover B-trees and related structures, and homework on AVL and B-trees, in the next set of notes.