AVL Trees
From
← Binary Search Trees | ↑ Binary Trees | Red-Black Trees → |
An AVL tree is a self-balancing binary search tree, and it is the first such data structure to be invented.[1] In an AVL tree, the heights of the two child subtrees of any node differ by at most one; therefore, it is also said to be height-balanced. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.
The AVL tree is named after its two inventors, G.M. Adelson-Velskii and E.M. Landis, who published it in their 1962 paper "An algorithm for the organization of information."
AVL Behavior
Each node in an AVL tree contains information regarding the balance of the tree at that node. This information, called the balance factor, is defined as the height of the left subtree minus the height of the right subtree. Thus, when a tree is perfectly balanced, every node has a balance factor that equals zero. Recall that the height of a binary tree is the maximum path length from the root to a leaf. A single-node binary tree has height 0, and we will define an empty binary tree (or a null subtree) to have height -1.
An AVL tree is a binary search tree in which every node is height balanced, that is, the difference in the heights of its two subtrees is at most 1. An equivalent definition, then, for an AVL tree is that it is a binary search tree in which each node has a balance factor of -1, 0, or +1. Note that a balance factor of -1 means that the subtree is left-heavy, and a balance factor of +1 means that the subtree is right-heavy.
Whenever a node is inserted or deleted into an AVL tree, the balance factor for each node along the path from the root to the inserted or deleted node must be recalculated. As long as the balance factor for each node is a -1, 0, or +1, no further action is taken. If a balance factor does not fall within this range, one of four type of rotations is performed that involve the out-of-balance node. The possible rotations are: (1) single right rotation, (2) single left rotation, (3) double left-right rotation, and (4) double right-left rotation.
AVL Tree Single and Double Right Rotations
Single Right Rotation for subtrees that are out of balance to the left (balance factor <= -2), and the left subtree is left heavy (balance factor = -1). This is the Left-Left case:
AVL Tree Double Rotations
Double Right Rotation for trees that are out of balance to the left (balance factor <= -2), and the left subtree is right heavy (balance factor = 1). This is the Left-Right case:
Double Left Rotation for subtrees that are out of balance to the right (balance factor >= 2) and the right subree is left heavy (balance factor = -1). This is the Right-Left Case:
CS2: Data Structures
Theory of Computation - ADT Preliminaries
Linear ADTs - Tree ADTs - Graph ADTs - Unordered Collection ADTs