How to simply create a complete balanced tree with several nodes at the lowest level, possibly missing, for example, for 6 elements, create
o
/ \
oo
/ \ /
ooo
Then do the usual move, and when you visit the ith node, set its key to [i].
This is a valid AVL tree, since each node has left and right children, whose height differs by no more than one.
The source tree can be built in O (n), and is like in O (n), so the complexity is O (n).
By the way, in a semidefinite note, there is a method called heapify for constructing a heap (mix or max) from an array of integers, which is O (n) for an array of length n, even if the insert into the heap is O (log n) - the trick is to do it from bottom to top.
source share