AlgoFrames

Insert and rebalance

Standard · Trace the algorithm · about 8 min

The keys 20, 10, 30, 5, 15, 12 are inserted in this order into an empty AVL tree. The array next to the tree lists the keys in level order: the root, then each level from left to right.

Answer each question before the step plays.

Example 1

Input:

[[20,10,30,5,15,12]]

Output:

[15,10,20,5,12,30]

The final tree, listed in level order.

Constraints

  • Balance factor = left height − right height
  • The lowest unbalanced node is repaired

Learn the idea first: AVL trees and rotations, a visual lesson.

More AVL trees and rotations problems