AVL Search Trees
Objectives
Upon completion you will be able to:
Explain the differences between a BST and an AVL tree
Insert, delete, and process nodes in an AVL tree
Understand the operation of the AVL tree ADT
Data Structures: A Pseudocode Approach with C, S
AVL Tree Basic Concepts
While BST is simple and easy to understand, it has one major
problem which is not balanced.
In 1968, two Russian mathematicians, G.M. Adelson-Velskii and
E.M. Landis, created the balanced binary tree structure that is
AVL tree.
An AVL tree is a BST that is guaranteed to always be balanced.
An AVL tree is a binary tree that either is
1. empty or
2. consists of two AVL subtrees, TL and TR , whose heights
differ by no more than 1
| HL HR | 1
Data Structures: A Pseudocode Approach with C, S
Data Structures: A Pseudocode Approach with C, S
8-1 AVL Tree Basic Concepts
AVL Tree Balance Factor
Balancing Trees
Data Structures: A Pseudocode Approach with C, S
AVL Tree Balance Factor
The balance factor = HL HR
The balance factor for any node in AVL tree must be +1, 0, -1
The descriptive identifiers are as follows:
LH for left high (+1) indicates the left subtree is higher than the right
subtree.
EH for even high (0) indicates the subtrees are the same height.
RH for right high (-1) indicates the left subtree is shorter than the right
subtree.
Data Structures: A Pseudocode Approach with C, S
Data Structures: A Pseudocode Approach with C, S
Balancing Trees
AVL trees are balanced by rotating nodes either to the left or to
the right
Four cases that require rebalancing. All unbalanced trees fall
into one of these four cases:
1. Left of left: A subtree of a tree that is left high has also
become left high.
2. Right of right: A subtree of a tree that is right high has also
become right high.
3. Right of left: A subtree of a tree that is left high become
right high.
4. Left of right: A subtree of a tree that is right high become
left high.
Data Structures: A Pseudocode Approach with C, S
Data Structures: A Pseudocode Approach with C, S
(continued)
Data Structures: A Pseudocode Approach with C, S
Case 1:
Left of Left
Data Structures: A Pseudocode Approach with C, S
10
Case 2:
Right of Right
Data Structures: A Pseudocode Approach with C, S
11
Case 3:
Right of Left
Data Structures: A Pseudocode Approach with C, S
12
Case 4:
Left of Right
Data Structures: A Pseudocode Approach with C, S
13
AVL Tree Implementations
Insertion and deletion in an AVL tree follow the same basic rules
for insertion and deletion in a BST. The difference lies in the tree
balancing, which occurs as we back out of the tree. In this
section we develop the insertion and deletion algorithms for a
AVL tree, including algorithms to balance the tree.
Insert into AVL Tree
AVL Tree Delete Algorithm
Data Structures: A Pseudocode Approach with C, S
14
Data Structures: A Pseudocode Approach with C, S
15
Data Structures: A Pseudocode Approach with C, S
16
Data Structures: A Pseudocode Approach with C, S
17
Data Structures: A Pseudocode Approach with C, S
18
Data Structures: A Pseudocode Approach with C, S
19
Data Structures: A Pseudocode Approach with C, S
20
Data Structures: A Pseudocode Approach with C, S
21
Data Structures: A Pseudocode Approach with C, S
22
Data Structures: A Pseudocode Approach with C, S
23
Data Structures: A Pseudocode Approach with C, S
24
Data Structures: A Pseudocode Approach with C, S
25