With rotations available, AVL insertion and deletion are ordinary BST operations followed by a walk back up the search path, checking and fixing balance factors.
Insertion
Insert the key as in a plain BST (it becomes a leaf).
Go back up the path towards the root, updating each node’s height (or balance factor).
At the first node with bf =±2, apply the matching rotation (LL, RR, LR or RL).
After an insertion, at most one single or double rotation is needed: it restores the subtree to its height before the insertion, so the nodes above are balanced again.
Deletion
Delete the key as in a BST (for two children, use the inorder successor or predecessor).
Go back up the path from the removed node to the root, updating heights.
Rotate at every node that reaches bf =±2.
Unlike insertion, a deletion may need rotations at several levels, because a rotation after a deletion can make the subtree one level shorter, which may unbalance its parent. Each step is O(1) and there are O(logn) levels, so deletion is still O(logn).
Applications and trade-offs
AVL trees suit applications that search much more often than they update, because they are the most strictly balanced of the common search trees (shortest height), while insertion and deletion pay for rotations:
in-memory dictionaries, symbol tables in compilers, telephone directories;
indexes where data arrives already sorted (a plain BST would degenerate into a chain);
databases and file systems that need guaranteed O(logn) look-up for data that fits in memory.
When updates are frequent, red-black trees (fewer rotations) are often preferred; for data on disk, B-trees are used.
Key takeaways
Insert: BST insert, update heights upwards, at most one (single or double) rotation.
Delete: BST delete, update heights upwards, possibly a rotation at each level.
Both are O(logn) in the worst case.
AVL trees are best when searches far outnumber updates.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Construct an AVL tree by inserting 40, 20, 10, 25, 30, 22 one at a time, naming each rotation.
Show worked answer
40; 40(20); insert 10: bf(40) = −2, LL → right rotation at 40: 20(10, 40). Insert 25: 20(10, 40(25, -)), balanced. Insert 30: goes 20 → 40 → 25 → right; bf(40) = −2 with path left (25) then right (30): LR at 40 → left rotation at 25, right rotation at 40: 20(10, 30(25, 40)). Insert 22: goes 20 → 30 → 25 → left; bf(20) = +2, path right (30) then left (25): RL at 20 → right rotation at 30, then left rotation at 20: 25(20(10, 22), 30(-, 40)). Final balance factors: 25: 0, 20: 0, 30: +1, others 0.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/advanced-trees/avl-insertion-deletion-and-applications/ to read this page.