Difference between AVL trees and splay trees
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
AVL trees and splay trees are two types of self-balancing binary search trees, each with its own trade-offs and use cases. Understanding their differences is crucial for making informed decisions about which data structure to use in various scenarios.
AVL Trees
Overview
AVL trees, named after inventors Adelson-Velsky and Landis, are a type of self-balancing binary search tree where the difference in heights between the left and right subtrees (known as the balance factor) of every node is at most 1. This strict balancing condition ensures that the tree maintains time complexity for operations like insertion, deletion, and look-up.
Key Characteristics
- Balance Factor: For any node in an AVL tree, the balance factor should be -1, 0, or 1. This is recalculated and potentially corrected after every insert or delete operation.
- Rotations: To maintain balance, AVL trees use single or double rotations (right, left, left-right, and right-left).
- Height: AVL trees guarantee a height of at most for a tree with nodes.
- Strict Balance: Due to the strict balancing criteria, AVL trees offer optimized search operations at the cost of slightly more complex insertion and deletion operations.
Example
Consider inserting a series of nodes with values 10, 20, and 30 into an initially empty AVL tree:
- Insert 10:
- Tree consists of a single node, 10.
- Insert 20:
- Tree is still balanced. 10 is root, 20 is right child.
- Insert 30:
- Right-right case imbalance. Perform a left rotation on root (10).
- New root is 20, with 10 as left child and 30 as right child.
- Splaying: Accessing a node performs rotations to bring it to the root. This recent-access pattern optimizes the tree for sequences with locality of reference.
- Amortized Complexity: Operations (insert, delete, search) have an amortized time complexity of , but individual operations can be as slow as in the worst case.
- No Balance Factor: Unlike AVL trees, splay trees do not maintain a strict balance factor.
- Applications: Ideal for situations with frequent access to certain elements, capitalizing on temporal locality.
- Access 10: No change needed, 10 becomes the root if not already.
- Access 20: Splay 20 to the root via rotations.
- Access 30: Splay 30 to the root.
Related reading
- Difference between back tracking and dynamic programming
- Difference between Big-O and Little-O Notation
- Difference between Big-Theta and Big O notation in simple language
- Difference between binary search and binary search tree?
- Difference between bidirectional_dynamic_rnn and stack_bidirectional_dynamic_rnn in Tensorflow
- Difference between del, remove, and pop on lists in Python
- Difference between Divide and Conquer Algo and Dynamic Programming
- Difference between hamiltonian path and euler path

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.