← Back to problems
UNSW COMP2521 · hard

AVL Tree: Height After Inserts

AVL Tree: Height After Inserts

Insert n distinct integers into an AVL tree in input order (rotating to stay balanced after each insert); print the final height.

Height is in edges: single node = 0, empty = -1.

A plain BST fed 1 2 3 4 5 degenerates to a chain (height 4); an AVL rotates and ends at height 2. You must implement all four rotations (LL/RR/LR/RL).

Input: line 1 n; line 2 n integers. Output: final AVL height.

Example: 5 / 1 2 3 4 5 -> 2.