Beruflich Dokumente
Kultur Dokumente
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 sub-trees of a tree are
almost equal, a tree of n nodes will have log2 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, Adelson-Velskii 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 sub-trees 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 2k. 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
level
-------------------------------------------- 0
2
1
4
3
--------------------------1
---------------------------------2
------------------------------------------------------------ 3
Page 2 of 11
-1
1
3
level
-------------------------------------------- 0
--------------------------1
--------------------------------2
------------------------------------------------------------ 3
-1
0
1
3
0
0
level
------------------------------------------0
--------------------------1
----------------------------------------------2
0
------------------------------------3
From the above discussion, we encounter two terms i.e. height and balance which can
be defined as under.
Page 3 of 11
0 12
0 2
0
1 10
0 3
0 8
-1 14
0 11
0 9
0 13
0 16
0 15
0 17
Page 4 of 11
U1
U4
0 3
1
U2
0 12
0 2
0
1 10
5
B
0 8
-1 14
0 11
U3
0
U5
U12
0 9
U6
U7
0 13
B
0 16
B
0 15
U8
U9
0 17
U10
U11
0 12
0 2
0
U1
U4
U2
1 10
5
B
0 3
0 8
0 11
U3
0 7-1
1
-1 14
U5
U6
0 13
B
0 9
U7
0 16
0 15
0
U8
12
U9
0 17
U10
U11
U12
1 10
-1 14
0 2
0 5
Fig 20.6: Insertions and effect in a balanced tree
B
0 discuss
0 3should we do
0 when
Now lets
what
the0insertion
1
8
11 0 of13a node 0makes
16 the tree
unbalanced. For this purpose, consider the node that has a balance 1 in the previous
tree. This is the root of the left subtree of the previous tree. This tree is shown as
U1the following
U2
U3 figure.
B B
B
B
shaded in
U4
0 7
0 9
0 15
0 17
U5
U12
U6
U7
U8
U9
U10
U11
Page 6 of 11
We will now focus our discussion on this left subtree of node having balance 1 before
applying it to other nodes. Look at the following figure (Fig 20.8). Here we are talking
about the tree that has a node with balance 1 as the root. We did not mention the other
part of the tree. We indicate the root node of this left subtree with label A. It has
balance 1. The label B mentions the first node of its left subtree. Here we did not
mention other nodes individually. Rather, we show a triangle that depicts all the nodes
in subtrees. The triangle T3 encloses the right subtree of the node A. We are not
concerned about the number of nodes in it. The triangles T1 and T2 mention the left
and right subtree of the B node respectively. The balance of node B is 0 that describes
that its left and right subtrees are at same height. This is also shown in the figure.
Similarly we see that the balance of node A is 1 i.e. its left subtree is one level deep
than its right subtree. The dotted lines in the figure show that the difference of
depth/height of left and right subtree of node A is 1 and that is the balance of node A.
B 0
T3
T1
T2
Now considering the notations of figure 20.8, lets insert a new node in this tree and
observe the effectFig
of 20.8:
this insertion
the tree.
newtree
node can be inserted in the tree
Insertinginnew
node The
in AVL
Page 7 of 11
B 1
T3
T1
T2
1
2
new
Fig 20.9: Inserting new node in AVL tree
Due to the increase of level in T1, its difference with the right subtree of node A (i.e.
T3) will become 2. This is shown with the help of dotted line in the above figure. This
difference will affect the balances of node A and B. Now the balance of node A
becomes 2 while balance of node B becomes 1. These new balances are also shown in
the figure. Now due to the balance of node A (that is 2), the AVL condition has been
violated. This condition states that in an AVL tree the balance of a node cannot be
other than 1, 0 or 1. Thus the tree in fig 20.9 is not a balanced (AVL) tree.
Now the question arises what a programmer should do in case of violation of AVL
condition .In case of a binary search tree, we insert the data in a particular order. So
that at any time if we traverse the tree with inorder traversal, only sorted data could be
obtained. The order of the data depends on its nature. For example, if the data is
numbers, these may be in ascending order. If we are storing letters, then A is less than
B and B is less than C. Thus the letters are generally in the order A, B, C . This
order of letters is called lexographic order. Our dictionaries and lists of names follow
this order.
If we want that the inorder traversal of the tree should give us the sorted data, it will
not be necessary that the nodes of these data items in the tree should be at particular
positions. While building a tree, two things should be kept in mind. Firstly, the tree
should be a binary tree. Secondly, its inorder traversal should give the data in a sorted
order. Adelson-Velskii and Landis considered these two points
. They said that if we see that after insertion, the tree is going to be unbalanced. Then
the things should be reorganized in such a way that the balance of nodes should fulfill
the AVL condition. But the inorder traversal should remain the same.
Now lets see the example of tree in figure 20.9 and look what we should do to balance
the tree in such a way that the inorder traversal of the tree remains the same. We have
Page 8 of 11
0
A
0
T1
T2
T3
Lets build an AVL tree as an example. We will insert the numbers and take care of the
balance of nodes after each insertion. While inserting a node, if the balance of a node
becomes greater than 1 (that means tree becomes unbalance), we will rearrange the
tree so that it should become balanced again. Lets see this process.
Assume that we have insert routine (we will write its code later) that takes a data item
as an argument and inserts it as a new node in the tree. Now for the first node, lets say
Page 9 of 11
-2
2
Lets see the balance of nodes at this stage. We see that node 1 is at level 0 (as it is the
root node). The nodes 2 and 3 are at level 1 and 2 respectively. So with respect to the
node 1, the deepest level (height) of its right subtree is 2. As there is no left subtree of
node 1 the level of left subtree of 1 is 0. The difference of the heights of left and right
subtree of 1 is 2 and that is its balance. So here at node 1, the AVL condition has been
violated. We will not insert a new node at this time. First we will do the rotation to
make the tree (up to this step) balanced. In the process of inserting nodes, we will do
the rotation before inserting next node at the points where the AVL condition is being
violated. We have to identify some things for doing rotation. We have to see that on
what nodes this rotation will be applied. That means what nodes will be rearranged.
Some times, it is obvious that at what nodes the rotation should be done. But there
may situations, when the things will not be that clear. We will see these things with the
help of some examples.
In the example under consideration, we apply the rotation at nodes1 and 2. We rotate
these nodes to the left and thus the node 1 (along with any tree if were associated with
it) becomes down and node 2 gets up. The node 3 (and trees associated with it, here is
no tree as it is leaf node) goes one level upward. Now 2 is the root node of the tree
and 1 and 3 are
its left and right subtrees respectively as shown in the following figure.
1 -2
2
2
1
3
Non AVL Tree
3
Page 10 of 11
We see that after the rotation, the tree has become balanced. The figure reflects that
the balance of node 1, 2 and 3 is 0. We see that the inorder traversal of the above tree
before rotation (tree on left hand side) is 1 2 3. Now if we traverse the tree after
rotation (tree on right hand side) by inorder traversal, it is also 1 2 3. With respect to
the inorder traversal, both the traversals are same. So we observe that the position of
nodes in a tree does not matter as long as the inorder traversal remains the same. We
have seen this in the above figure where two different trees give the same inorder
traversal. In the same way we can insert more nodes to the tree. After inserting a node
we will check the balance of nodes whether it violates the AVL condition. If the tree,
after inserting a node, becomes unbalance then we will apply rotation to make it
balance. In this way we can build a tree of any number of nodes.
Page 11 of 11