The rotation will update the height of the nodes involved in the rotation, but not subtrees below the nodes being rotated (pivot and its longer child). When we start the process, one subtree is taller than the other by 2. That taller subtree is the one that will rotate up. As we do so, itβs height will decrease by one (because the root of that subtree is moving up). Meanwhile, the other subtree (the shorter one) will gain one height because the pivot node is being rotated down into it. The net effect is that the heights of the two subtrees will now be equal, restoring balance.
Subsection32.5.2Structure of a Worst-Case AVL Tree
Even if we use rotations to fix trees with a balance factor of 2 or -2, we do allow a balance factor of -1, 1 at each node. So what kind of performance guarantees can we make given that we wonβt always have perfectly balanced trees?
To answer this, we depict the minimum number of nodes to construct trees of height 0, 1, 2, and 3 in FigureΒ 32.5.1. In the diagram, the most left-heavy tree possible is constructed for each height. Take a minute to convince yourself that each tree is a legal AVL tree and that removing any node on the right side of a subtree would make it invalid.
If you look carefully at the trees, you can see that each tree is constructed by making a root node and then attaching the worst-case tree of height h-1 as the left child and the worst-case tree of height h-2 as the right child. (i.e. The height 3 tree has a height 2 tree on its left and a height 1 tree on its right. The height 4 tree has a height 3 tree on its left and a height 2 tree on its right.)
We can see that this is true of the values in the table. For instance, for height 3 we have \(N_3 = 1 + N_2 + N_1 = 1 + 4 + 2 = 7\text{.}\) For height 4 we have \(N_4 = 1 + N_3 + N_2 = 1 + 7 + 4 = 12\text{.}\)
This says that the number of nodes in a worst-case AVL tree of height \(h\) is always greater than twice the number of nodes in a worst-case AVL tree of height \(h-2\) plus one.
solves to \(O(2^{\frac{n}{2}})\text{.}\) By analogy, we can see that the number of nodes in a worst-case AVL tree of height \(h\) is greater than \(2^{\frac{n}{2}}\text{.}\) So, at this point we have
What we really care about is the height of an AVL tree given the number of nodes in the tree. If we take the base 2 log of both sides of the inequality
This final equation tells us that the height of an AVL tree is less than twice the base 2 log of the number of nodes in the tree. This is great news for searching our AVL tree because it limits height of the tree to \(O(\log{N})\text{.}\) This ensures that our search, insertion, and deletion operations will all run in \(O(\log{N})\) time.
is very similar to the Fibonacci sequence. We can use this fact to derive a formula for the height of an AVL tree given the number of nodes in the tree.
In Binetβs formula, as \(i\) grows large, the term \((1 - \Phi)^i\) approaches zero, so we can use the following approximation to obtain a value close to the \(i^{th}\) Fibonacci number:
This derivation shows us that at any time the max height of our AVL tree is approximately equal to a constant (1.44) times the log of the number of nodes in the tree.
We already knew that the height of an AVL tree is \(O(\log{N})\text{,}\) but this formal analysis gives us a more precise understanding of the key constant factor that is hidden in the big-O notation.