CS301 – Data Structures
Lecture No.
20
___________________________________________________________________
Data Structures
Lecture No. 20
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 4
4.4
Summary
•
AVL Tree
•
Insertion in AVL Tree
•
Example (AVL Tree Building)
We will continue the discussion on AVL tree in this lecture. Before going ahead, it
will be better to recap things talked about in the previous lecture. We built a balanced
search tree (BST) with sorted data. The numbers put in that tree were in increasing
sorted order.
The tree built in this way was like a linked list. It was witnessed that the
use of the tree data structure can help make the process of searches faster. We have
seen that in linked list or array, the searches are very time consuming. A loop is
executed from start of the list up to the end. Due to this fact, we started using tree
data structure. It was evident that in case, both the left and right subtrees of a tree are
almost equal, a tree of
n
nodes will have
log
2
n
levels. If we want to search an item in
this tree, the required result can be achieved, whether the item is found or not, at the
maximum in the
log n
comparisons.
Suppose we have 100,000 items (number or
names) and have built a balanced search tree of these items. In 20 (i.e. log 100000)
comparisons, it will be possible to tell whether an item is there or not in these
100,000 items.
AVL Tree
In the year 1962, two Russian scientists, AdelsonVelskii and Landis,
proposed the criteria to save the binary search tree (BST) from its degenerate form.
This was an effort to propose the development of a balanced search tree by
considering the height as a standard. This tree is known as AVL tree. The name AVL
is an acronym of the names of these two scientists.
An AVL tree is identical to a BST, barring one difference i.e. the height of the left
and right subtrees can differ by at most 1.
Moreover, the height of an empty tree is
defined to be (–1).
Keeping in mind the idea of the level of a tree, we can understand that if the root of a
tree is at level zero, its two children (subtrees) i.e. nodes will be at level 1. At level 2,
there will be 4 nodes in case of a complete binary tree. Similarly at level 3, the
number of nodes will be 8 and so on. As discussed earlier, in a complete binary tree,
the number of nodes at any level
k
will be 2
k
. We have also seen the level order
traversal of a tree. The term height is identical to the level of a tree. Following is the
figure of a tree in which level/height of nodes is shown.
Page 1 of 11
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
CS301 – Data Structures
Lecture No.
20
___________________________________________________________________
Here in the figure, the root node i.e. 5 is at the height zero. The next two nodes 2 and
8 are at height (or level) 1. Then the nodes 1, 4 and 7 are at height 2 i.e. two levels
below the root. At the last, the single node 3 is at level (height) 3. Looking at the
figure, we can say that the maximum height of the tree is 3. AVL states that a tree
should be formed in such a form that the difference of the heights (maximum no of
levels i.e. depth) of left and right subtrees of a node should not be greater than 1. The
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 Dr.Naveed Malik
 Data Structures, AVL Tree Building

Click to edit the document details