DECMATRIX

Mathematical Tools

AVL Binary Tree Builder

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

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. To understand insertion operations in binary search trees, you can visit our binary search tree simulator.

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

01. What is an AVL tree and what is it used for?

02. How does an AVL tree perform its automatic balancing?

03. What are rotations in an AVL tree and how do they work?

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