DECMATRIX

Mathematical Tools

AVL Binary Tree Builder

Enter the values separated by commas to insert into the AVL tree.
SlowFast
Enter a single value to remove from the AVL tree.
Values to be inserted into the tree: Empty
Values in the tree: Empty

Try the same sequence in the binary search tree (BST) simulator and compare the resulting structure with the AVL tree.

From visualization to practice

The simulator shows the changes caused by an insertion. In the exercises, you choose the position and each rotation, with an explanation when an attempt does not work.

Understand how rotations work? Now practice choosing where to insert each value and how to balance the tree.

Ready to practice? Try the AVL tree exercises.

Try one rotation

In this tree containing 30, 20 and 10, find the unbalanced ancestor and perform one rotation. This example is independent of the simulator's tree.

Balance this tree

One simple rotation

Tree zoom: 63%

Drag with the mouse to move the tree. With the frame focused, you can also use the arrow keys.

Swipe vertically to scroll the page and horizontally to move the tree. Enable Touch: enable vertical dragging to move the frame on both axes. Turn it off to resume page scrolling. The direction buttons work in either mode.

Balance this tree

No node selected.

Select a node and use an arrow beside it to rotate.

Select a node and rotate
Help and hints

BF = left height − right height; leaf height = 0 and empty subtree height = −1.

Use + and − to zoom around the center. Drag to pan; there are no internal scrollbars. With the region focused, use the arrow keys or the four direction buttons. Center brings the selected node or root into view. Fit to view restores automatic framing; manual framing stays unchanged between actions.

    AVL Tree - Definition

    An AVL tree (Adelson-Velsky and Landis) is a self-balancing binary search tree where the height difference between the left and right subtrees of any node is at most 1. This ensures that the tree remains approximately balanced, providing efficient insertion, removal, and search operations.

    AVL trees were the first self-balancing binary search trees invented, in 1962, by Georgy Adelson-Velsky and Evgenii Landis. They are widely used in applications requiring fast search and update operations, such as databases and file systems.

    Remember: A binary tree is, by definition, a set of nodes that is either empty or consists of a root and two disjoint binary subtrees, called the left and right subtrees of the root.

    AVL Tree - Properties

    The main properties of an AVL tree include: automatic balancing, which keeps the tree height at O(log n), ensuring efficient operations; the ordering of elements, facilitating quick searches; and the hierarchical structure that organizes data efficiently. Rotations (single and double) are used to restore balance after insertions and removals.

    AVL Tree - Search, Insertion and Removal

    Search in an AVL tree is similar to search in a standard binary search tree, leveraging the ordering of elements to quickly locate the desired value.

    Insertion in an AVL tree starts like in a binary search tree, locating the correct position for the new node. After insertion, it is necessary to check the balance of the tree and, if needed, perform rotations to restore balance.

    Removal in an AVL tree also follows the process of a binary search tree, but after removal, it is necessary to check and restore the balance of the tree, if needed, using appropriate rotations.

    AVL Tree - Balance and Rotations

    Balance in an AVL tree is maintained by calculating the balance factor for each node, which is the difference between the heights of the left and right subtrees. If the balance factor of a node becomes greater than 1 or less than -1 after an insertion or removal, the tree is unbalanced and needs to be corrected.

    There are four main cases of imbalance that can occur in an AVL tree, each requiring a specific rotation to restore balance:

    1. Right single rotation: Occurs when a node has a balance factor greater than 1 and its left child has a balance factor of 1 or 0.
    2. Left single rotation: Occurs when a node has a balance factor less than -1 and its right child has a balance factor of -1 or 0.
    3. Left double rotation: Occurs when a node has a balance factor greater than 1 and its left child has a balance factor less than 0.
    4. Right double rotation: Occurs when a node has a balance factor less than -1 and its right child has a balance factor greater than 0.

    FAQ

    Decmatrix - Your open-source mathematical toolkit for calculations, conversions, and more.
    Terms of Use
    Privacy Policy
    Contact: decimatrix25@gmail.com