Lecture 14-AVL Trees Lecture
Lecture 14-AVL Trees Lecture
AVL Trees
Outline
Requirement:
– Define and maintain balance to ensure (ln(n)) height
Prototypical Examples
Again, the product is a linked list; however, we can fix this, too
Prototypical Examples
These examples may seem trivial, but they are the basis for the
corrections in the next data structure we will see: AVL trees
AVL Trees
Recall:
– An empty tree has height –1
– A tree with a single node has height 0
AVL Trees
h (h I) h
( 3 5 5 ) ( 1 5 ) 1e ( 53 5 ) 2
1
h 5 h
5 ( 1 5 ) 2 ( 1 5 ) ( 1 5 )
h
1 5
c
2
Height of an AVL Tree
This is approximately
F(h) ≈ 1.8944 h – 1
where ≈ 1.6180 is the golden ratio
– That is, F(h) = (h)
Observe that:
– Inserting a node can increase the height of a tree by at most 1
– Removing a node can decrease the height of a tree by at most 1
This may cause some nodes to be unbalanced. We may need to
rebalance the tree after insertion or removal.
Maintaining Balance
The heights of each of the sub-trees from here to the root are
increased by one
Maintaining Balance
Consider adding 6:
Maintaining Balance
The height of each of the trees in the path back to the root are
increased by one
Maintaining Balance
The height of each of the trees in the path back to the root are
increased by one
– However, only the root node is now unbalanced
Maintaining Balance
AVL_node<Type> *b = left(),
*BR = b->right();
Maintaining Balance: Case 1
AVL_node<Type> *b = left(),
*BR = b->right();
Maintaining Balance: Case 1
AVL_node<Type> *b = left(),
*BR = b->right();
b->right_tree = this;
Maintaining Balance: Case 1
AVL_node<Type> *b = left(),
*BR = b->right();
b->right_tree = this;
ptr_to_this = b;
left_tree = BR;
Maintaining Balance: Case 1
The nodes b and f are now balanced, and all remaining nodes of the
subtrees are in their correct positions
Maintaining Balance: Case 1
Q: will this insertion affect the balance of any ancestors all the way
back to the root?
No, because height of the tree rooted at b equals the original height
of the tree rooted at f
Maintaining Balance: Case 1
AVL_node<Type> *b = left(),
*d = b->right(),
*DL = d->left(),
*DR = d->right();
Maintaining Balance: Case 2
AVL_node<Type> *b = left(),
*d = b->right(),
*DL = d->left(),
*DR = d->right();
d->left_tree = b;
d->right_tree = this;
ptr_to_this = d;
b->right_tree = DL;
left_tree = DR;
Maintaining Balance: Case 2
return true;
} else {
return false;
}
} else if ( obj > element ) {
// ...
Insertion (Implementation)
Comments:
– Both balances are (1)
– All insertions are still (ln(n))
– It is possible to tighten the previous code
– Aside
• if you want to explicitly rotate the nodes A and B, you must also pass a
reference to the parent pointer as an argument:
insert( Type obj, AVL_node<Type> * & parent )
Insertion
Insert 73
Insertion
Insert 77
Insertion
Insert 76
Insertion
Again, balanced
Insertion
Insert 80
Insertion
Again, balanced
Insertion
Insert 74
Insertion
Again, balanced
Insertion
Insert 64?
Insertion
Insert 55
Insertion
Insert 70?
Insertion
Removing a node from an AVL tree may cause more than one AVL
imbalance
– Like insert, erase must check if it caused an imbalance
– Unfortunately, it may cause O(h) imbalances that must be corrected
• Insertions will only cause one imbalance that must be fixed
Erase
Insertion
– May require one correction to maintain balance
– Each correction require (1) time
Erasion
– May require O(h) corrections to maintain balance
– Each correction require (1) time
– Depth h is ( ln(n) )
– So the time complexity is O( ln(n) )
AVL Trees as Arrays?
Recall that in the worst case, an AVL tree of n nodes has a height at
most
log(n) – 1.3277
Such a tree requires an array of size
2log(n) – 1.3277 + 1 – 1
We can rewrite this as
2–0.3277 nlog(2) ≈ 0.7968 n1.44
Thus, we would require O(n1.44) memory
AVL Trees as Arrays?
AVL trees
– Balance: difference in subtree heights is 0 or 1
– Insertion and erasion may require the tree to be rebalanced by tree
rotations
Usage Notes
• These slides are made publicly available on the web for anyone to
use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an
acknowledgment) in my set of slides
Sincerely,
Douglas Wilhelm Harder, MMath
dwharder@alumni.uwaterloo.ca