AVL Trees

From

(Difference between revisions)
Jump to: navigation, search

Admin (Talk | contribs)
(Created page with 'Named after two Russian mathematicians, G. M. Adel’son-Vel’skii and E. M. Landis who published their result in 1962, this variation of a binary search tree rebalances itself ...')
Newer edit →

Revision as of 16:26, 28 March 2009

Named after two Russian mathematicians, G. M. Adel’son-Vel’skii and E. M. Landis who published their result in 1962, this variation of a binary search tree rebalances itself after every insertion or deletion.

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.

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. The rules for which rotation are somewhat complex, but we will look at an example of a single and a double rotation below.

Single Left Rotation



Double Left Rotation



Personal tools
MediaWiki Appliance - Powered by TurnKey Linux