Insert 20, 10, 30, 5, 15, 25, 35 one at a time. After each, walk back up checking balance, and rotate the first node whose sides differ in height by two.
Building an AVL tree by inserting 7 values with rebalancing.
Insert 20. First as an ordinary BST insert: descend and attach a leaf.
Inserting 20.
20 is the first value, so it is the root.
20 is added as a leaf.
Insert 10. First as an ordinary BST insert: descend and attach a leaf.
Inserting 10.
10 attaches as the left child of 20.
10 is added as a leaf.
At 20: left height 1, right height 0, balance 1. Within one, so it stays.
Node 20 has balance 1. It is fine.
Insert 30. First as an ordinary BST insert: descend and attach a leaf.
Inserting 30.
30 attaches as the right child of 20.
30 is added as a leaf.
At 20: left height 1, right height 1, balance 0. Within one, so it stays.
Node 20 has balance 0. It is fine.
Insert 5. First as an ordinary BST insert: descend and attach a leaf.
Inserting 5.
5 attaches as the left child of 10.
5 is added as a leaf.
At 10: left height 1, right height 0, balance 1. Within one, so it stays.
Node 10 has balance 1. It is fine.
At 20: left height 2, right height 1, balance 1. Within one, so it stays.
Node 20 has balance 1. It is fine.
Insert 15. First as an ordinary BST insert: descend and attach a leaf.
Inserting 15.
15 attaches as the right child of 10.
15 is added as a leaf.
At 10: left height 1, right height 1, balance 0. Within one, so it stays.
Node 10 has balance 0. It is fine.
At 20: left height 2, right height 1, balance 1. Within one, so it stays.
Node 20 has balance 1. It is fine.
Insert 25. First as an ordinary BST insert: descend and attach a leaf.
Inserting 25.
25 attaches as the left child of 30.
25 is added as a leaf.
At 30: left height 1, right height 0, balance 1. Within one, so it stays.
Node 30 has balance 1. It is fine.
At 20: left height 2, right height 2, balance 0. Within one, so it stays.
Node 20 has balance 0. It is fine.
Insert 35. First as an ordinary BST insert: descend and attach a leaf.
Inserting 35.
35 attaches as the right child of 30.
35 is added as a leaf.
At 30: left height 1, right height 1, balance 0. Within one, so it stays.
Node 30 has balance 0. It is fine.
At 20: left height 2, right height 2, balance 0. Within one, so it stays.
Node 20 has balance 0. It is fine.
All 7 values in, 0 rotations. Height 3: an unbalanced BST with these values could be up to 7 tall.
The AVL tree is complete with height 3 after 0 rotations.