Section 8.16 AVL Tree Performance
Before we proceed any further let’s look at the result of enforcing this new balance factor requirement. Our claim is that by ensuring that a tree always has a balance factor of -1, 0, or 1 we can get better Big-O performance of key operations. Let us start by thinking about how this balance condition changes the worst-case tree. The worse case performance for binary search trees is because the worst case "tree" equivalent to a singly linked list (see Section 8.14).
What is the worst case tree which also preserves a balance factor of -1, 0 or 1? Another way of asking that question is: how tall of a tree can we create for a given number of nodes that also preserves the balanace factor requirements? To answer this, we depict the minimum number of nodes to construct trees of height 0, 1, 2, and 3 in Figure 8.16.1. In the diagram, the most left-heavy tree possible is constructed for each height. We could have chosen to create the most right-heavy tree without loss of generalization.
Image showing the most unbalanced left-heavy AVL trees for heights 0, 1, 2, and 3, adhering to the AVL balance factor rules. Each tree is labeled with nodes and balance factors: - For height 0, the tree contains a single node "A" with a balance factor of 0. - For height 1, the tree has "B" as the root with a balance factor of 1 and "A" as its left child with a balance factor of 0. - For height 2, the tree has "C" as the root with a balance factor of 1. "B" is the left child of "C" with a balance factor of 1, and "A" is the left child of "B" with a balance factor of 0. "D" is the right child of "C" with a balance factor of 0. - For height 3, the tree has "E" as the root with a balance factor of 1. "C" is the left child of "E" with a balance factor of 1. "B" is the left child of "C" with a balance factor of 1, and "A" is the left child of "B" with a balance factor of 0. "D" is the right child of "C" with a balance factor of 0. "G" is the right child of "E" with a balance factor of 1, and "F" is the left child of "G" with a balance factor of 0. This sequence demonstrates the maximum permissible left-heavy configurations in AVL trees while maintaining balance factors of -1, 0, or 1 at all nodes.
Going from left to right in Figure 8.16.1, the tree is constructed by adding a new root node (depicted in green). Then the previous tree (show in red) is added as its left subtree. Finally, tree before that (depicted in blue) is added as the root’s right subtree.
Looking at the total number of nodes in the tree we see that for a tree of height 0 there is 1 node, for a tree of height 1 there is nodes, for a tree of height 2 there are and for a tree of height 3 there are More generally the pattern we see for the number of nodes in a tree of height h ( ) is:
This recurrence may look familiar to you because it 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. Recall that for the Fibonacci sequence the Fibonacci number is given by:
We can express the value of the number of nodes in a tree of height in terms of its position in the Fibonacci sequence:
You can consult a math text if you want to see a derivation of the previous formula. is the golden ratio and as is defined as:
In Binet’s formula, as grows large, the term approaches zero, so we can use the following approximation to obtain a value close to the Fibonacci number:
If we rearrange the terms, and take the base 2 log of both sides and then solve for we get the following derivation:
This derivation shows us that at any time the height of our AVL tree is equal to a constant(1.44) times the log of the number of nodes in the tree. This is great news for searching our AVL tree because it limits the search to
You have attempted 1 of 1 activities on this page.