Lably
Skip to content

AVL tree

Self-balancing binary search tree

  • 20 min read
  • 4 chapters

Related on Lably

Through Computer science, Big O notation and Soviet Union

The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velsky and Evgenii Landis, who published it in their 1962 paper "An algorithm for the organization of information". It is the first self-balancing binary search tree data structure to be invented.

AVL trees are often compared with red–black trees because both support the same set of operations and take O(log⁡n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. Similar to red–black trees, AVL trees are height-balanced. Both are, in general, neither weight-balanced nor μ-balanced for any μ≤12; that is, sibling nodes can have hugely differing numbers of descendants.

Definition

Balance factor

In a binary tree the balance factor of a node X is defined to be the height difference

BF(X):=Height(RightSubtree(X))−Height(LeftSubtree(X))

of its two child sub-trees rooted by node X.

A binary tree is defined to be an AVL tree if

BF(X)∈{−1,0,1}

holds for every node X in the tree.

A node X with BF(X)<0 is called "left-heavy", one with BF(X)>0 is called "right-heavy", and one with BF(X)=0 is sometimes simply called "balanced".

Properties

Balance factors can be kept up-to-date by knowing the previous balance factors and the change in height – it is not necessary to know the absolute height. For holding the AVL balance information, two bits per node are sufficient.

The height h (counted as the maximal number of levels) of an AVL tree with n nodes lies in the interval:

log2⁡(n+1)≤h<logφ⁡(n+2)+b

where φ:=1+52≈1.618 is the golden ratio and b:=log2⁡52log2⁡φ−2≈−0.3277. This is because an AVL tree of height h contains at least Fh+2−1 nodes where {Fn}n∈N is the Fibonacci sequence with the seed values F1=F2=1.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication