Chapter 8 - AVL Trees.pptkabulcs.weebly.com/.../chapter_8_-_avl_trees.pdf · 16 August, 2010 2 AVL...
Transcript of Chapter 8 - AVL Trees.pptkabulcs.weebly.com/.../chapter_8_-_avl_trees.pdf · 16 August, 2010 2 AVL...
16 August, 20101 AVL Tree
AVL Trees
s
The height of the BST be as small as possible
Alg
orith
m The first balanced BSTs.Also called height-balanced trees.
res
and
A
Introduced by Adel’son-Vel’skii and Landis in 1962. BSTs with the following conditions:
ta S
truc
tu 1- The height of the left and right sub-trees of the root differs by at most 1.
2 The left and right sub trees of the root are also AVL trees
Dat 2- The left and right sub-trees of the root are also AVL trees.
In other word, for all nodes v in the tree, the difference in height of the left and right sub trees ofdifference in height of the left and right sub-trees of v is no greater than 1.
16 August, 20102
AVL Trees
s
An AVL Tree is a binary search tree such that f i t l d f T th h i ht f th
Alg
orith
m forevery internal node v of T, the heights of the children of v can differ by at most 1.Th diti th t th d th f th t
res
and
A These conditions ensure that the depth of the tree is O (Log N) where N is the number of elements in the tree
ta S
truc
tu the tree.Suppose that, v is a node in an AVL tree then:
If left v > right v then v is left high
Dat If left v > right v then v is left high
If left v = right v then v is equal highIf left v < right v then v is right highg g g
16 August, 20103
AVL Trees
s
In addition to the data and references, each node stores one more thing which is the balance factor.
Alg
orith
m The balance factor for node x is a value such that: If v is left high then the balance factor of v is -1If v is equal high then the balance factor of v is 0
res
and
A If v is equal high then the balance factor of v is 0If v is right high then the balance factor of v is 1.
Since AVL tree is a BST, therefore, the search operation in an AVL tree use the ordinary BST search algorithm Because of the
ta S
truc
tu tree use the ordinary BST search algorithm. Because of the logarithmic depth of an AVL tree, the search operations in an AVL tree have logarithmic worst-case bounds. However insertion and deletion algorithms are different than BST
Dat
However, insertion and deletion algorithms are different than BST algorithms. After insertion or deletion, the balance criteria must be restored.
16 August, 20104
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
16 August, 20105
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
16 August, 20106
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
16 August, 20107
AVL Trees
s
For inserting a new item into an AVL tree, first we searchthe tree to find the place for the new item. If the item is
l d i h h h d i h b
Alg
orith
m already in the tree, the search ends with at a non-empty sub-tree. Because duplicate is not allowed in AVL tree therefore, we can output an appropriate error message. If the item is not in the tree then the search ends at an empty sub tree
res
and
A not in the tree then the search ends at an empty sub-treeand we insert the new item in that sub-tree. After inserting the new item, the resulting tree may not be an AVL tree. Thus we must restore the tree balance criteria Only the nodes that
ta S
truc
tu must restore the tree balance criteria. Only the nodes that are on the path from the insertion point to the root might have their balance changed. Therefore, we follow the same path, back to the root, which was followed when the new item
Dat
path, back to the root, which was followed when the new item was inserted in the AVL tree. The nodes on this path are visited and either their balance factor is changed, or we might have to reconstruct part of the tree. If we find a nodeg pwhose new balance factor violates the AVL tree conditions we will rebalance the tree by rotation.
16 August, 20108
AVL Trees
s
Suppose that the node to be rebalanced is X. Since any node has at most two children and the height imbalance in an AVL
Alg
orith
m has at most two children and the height imbalance in an AVL tree require that the height of the two sub-trees of X is differ by more than 1, therefore, a violation may occur in any of the f ll i
res
and
A following cases:An insertion into the left sub-tree of the left child of XAn insertion into the right sub-tree of the left child of X
ta S
truc
tu An insertion into the left sub-tree of the right child of XAn insertion into the right sub-tree of the right child of X
Case 1 and case 4 are symmetric and in these two cases the
Dat Case 1 and case 4 are symmetric and in these two cases the
insertion occurs on the outside (that is left-left or right-right), can be fixed by single rotation. A single rotation changes the roles of the parent and child while maintaining the search order for the tree.
16 August, 20109
AVL Trees
s
A single rotation can be left or right. If the rotation occurs at node x and it is a left
Alg
orith
m rotation occurs at node x and it is a left rotation, then some nodes from the right sub-tree of x move to its left sub-tree. The root of th i ht b t f b th t
res
and
A the right sub-tree of x becomes the new rootof the reconstructed sub-tree. Similarly, if it is a right rotation at node x some nodes from
ta S
truc
tu a right rotation at node x, some nodes from the left sub-tree of x move to its right sub-treeand the root of the left sub-tree of x becomes th t f th t t d b tD
at the new root of the reconstructed sub-tree.
16 August, 201010
AVL Trees
s
Case 2 and case 3, in which the insertion occurs on the inside (that is, left-right or right-left), are
Alg
orith
m
( , g g ),complex and can be fixed by double rotations.The double rotation is required when the balance factor of the node where the tree is to be
res
and
A factor of the node where the tree is to be reconstructed and the balance factor of the higher sub-tree are opposite. In this case first we rotate the tree at the lower node and then at the upper node If
ta S
truc
tu tree at the lower node and then at the upper node. If the lower sub-tree is right high, we make a left rotation and if it is left high we make a right
t ti S dl t t th t t th
Dat rotation. Secondly we rotate the tree at the upper
node. It the tree rooted at the upper node is left high, we make a right rotation and if it is right high, we
k l f imake a left rotation.
16 August, 201011
AVL Trees
s
To remove a node from an AVL tree, first we search the tree to find the node to be removed. If we find the node,
Alg
orith
m we can remove it in the same way as we remove a node from am ordinary BST. The difference is that, after removing the node, the BST may not be an AVL tree. Th b b l d d i th t th
res
and
A There may be an unbalanced node in the tree on the path from the parent of the deleted node to the root of the tree. In fact, there can be one such unbalanced node
t t Th f ft d l ti h t t th
ta S
truc
tu at most. Therefore, after deletion we have to restore the balance criteria for the tree. To do this we follow the pathfrom the parent of the deleted node back to the rootnode We isit each node on this path and sometimes
Dat node. We visit each node on this path, and sometimes
we need to node is reconstructed. Suppose that v is a node on the path back to the root node. We check the current balance change only the balance factor whilecurrent balance change only the balance factor, while other times the tree at a particular factor of v.
16 August, 201012
AVL Trees
s
If the current balance factor of v is equal high, then the balance factor of v is changed according to whether the left sub-tree of v was h t d th i ht b t f h t d
Alg
orith
m shortened or the right sub-tree of v was shortened.Suppose that the balance factor of v is not equal high and the taller sub-tree of v is shortened. The balance factor of v is changed to equal high
res
and
A high.Suppose that the balance factor of v is not equal high and the shorter sub-tree of v is shortened. Also suppose that q points to the root of the taller sub tree of v
ta S
truc
tu taller sub-tree of v.If the balance factor of q is equal high, a single rotation is required at v.If the balance factor of q is the same as v a single rotation is required
Dat If the balance factor of q is the same as v, a single rotation is required
at v.Suppose that the balance factor of v and q are opposite. A double rotation is required at v (a single rotation at q and then a single rotation q ( g q gat v). [D. S. Malik].
16 August, 201013
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
AVL b f i i 90AVL tree before inserting 90.
16 August, 201014
AVL Trees
s
For inserting 90 we search the tree starting at th t d t fi d th l f 90
Alg
orith
m the root node to find the place for 90.
res
and
Ata
Str
uctu
Dat
16 August, 201015
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
AVL tree after inserting 90 and adjusting the balance factors
16 August, 201016
AVL Trees
s
Now consider the following AVL tree:
Alg
orith
mre
s an
d A
ta S
truc
tuD
at
Let us insert 75 into the tree:
16 August, 201017
AVL Trees
s
As before we search the tree starting at the root node. After inserting 75 the resulting BST is:
Alg
orith
m After inserting 75, the resulting BST is:
res
and
Ata
Str
uctu
Dat
16 August, 201018
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
16 August, 201019
AVL Trees
s
Consider the following tree:
Alg
orith
mre
s an
d A
ta S
truc
tuD
at
16 August, 201020
AVL Trees
s
Now insert 95 into this AVL tree. After inserting 95 the tree will be:
Alg
orith
m 95 the tree will be:
res
and
Ata
Str
uctu
Dat
16 August, 201021
AVL Trees
sA
lgor
ithm
res
and
Ata
Str
uctu
Dat
16 August, 201022
AVL Trees
s
Let us consider one more case. Consider the f ll i AVL t
Alg
orith
m following AVL tree:
res
and
Ata
Str
uctu
Dat
16 August, 201023
AVL Trees
s
Now let us insert 88 into this tree. The tree will be:
Alg
orith
mre
s an
d A
ta S
truc
tuD
at
d h h h h i b l f b fNodes other than 88 show their balance factors before insertion
16 August, 201024
AVL Trees
s
As before, we now backtrack to the root node. We adjust the balance factors of node 85 and 90 When
Alg
orith
m adjust the balance factors of node 85 and 90. When we visit node 80, we discover that at this node we need to reconstruct the sub-tree. The tree will be:
res
and
A need to reconstruct the sub tree. The tree will be:
ta S
truc
tuD
at
16 August, 201025
AVL Trees
s
To make the algorithm efficient, we store the balance factor of each node in the node itself
Alg
orith
m balance factor of each node in the node itself.class avlitem {
int idno;
res
and
A int idno;String firstname;int bfactor;
ta S
truc
tu int bfactor;avlitem leftchild;avlitem rightchild;
Dat avlitem rightchild;
}
16 August, 201026 Insertion into AVL Tree
AVL Trees
s
class avlitemlist {public avlitem root;
Alg
orith
m
public avlitem root;public boolean unbalance = false;public void insert(int id, String name){
res
and
A {avlitem newstudent = new avlitem ();newstudent idno= id;
ta S
truc
tu newstudent.idno id;newstudent.firstname = name;newstudent.bfactor = 0;
Dat newstudent.leftchild = null;
newstudent.rightchild = null;root = insertAVL(root newstudent);root insertAVL(root, newstudent);
}
16 August, 201027
AVL Trees
s
public avlitem insertAVL(avlitem root, avlitem newstudent) {
Alg
orith
m if(root == null) {root = newstudent;unbalance = true;
res
and
A unbalance = true;}else
if ( id d id )
ta S
truc
tu if (root.idno == newstudent.idno)System.err.println("No duplicates are allowed");
else
Dat if (root.idno > newstudent.idno)
{root leftchild = insertAVL(root leftchild newstudent);root.leftchild = insertAVL(root.leftchild, newstudent);
16 August, 201028
AVL Trees
s
if (unbalance )switch(root.bfactor)
Alg
orith
m
( ){
case -1 : root = balancefromleft(root);unbalance = false;
res
and
A unbalance = false;break;
case 0 : root.bfactor = -1;b l
ta S
truc
tu unbalance = true;break;
case 1 : root.bfactor = 0;
Dat
;unbalance = false;
}}}
16 August, 201029
AVL Trees
s
else{
root rightchild = insertAVL(root rightchild newstudent);
Alg
orith
m root.rightchild = insertAVL(root.rightchild, newstudent);if (unbalance )switch(root.bfactor){
res
and
A case -1 : root.bfactor = 0;unbalance = false;break;case 0 : root bfactor = 1;
ta S
truc
tu case 0 : root.bfactor = 1;unbalance = true;break;case 1 : root = balancefromright(root);
Dat unbalance = false;
}}
return root;return root;}
16 August, 201030
AVL Trees
s
public avlitem balancefromright(avlitem root) {avlitem p;avlitem w;
Alg
orith
m avlitem w;p = root.rightchild;switch (p.bfactor){
res
and
A case -1 : w = p.leftchild;switch(w.bfactor){
case -1 : root bfactor = 0;
ta S
truc
tu case -1 : root.bfactor = 0;p.bfactor = 1;break;case 0 : root.bfactor = 0;
Dat p.bfactor = 0;
break;case 1 : root.bfactor = -1;p.bfactor = 0;p.bfactor 0;
}
16 August, 201031
AVL Trees
s
w.bfactor = 0;p = rotatetoright(p);
Alg
orith
m
p rotatetoright(p);root.rightchild = p;root = rotatetoleft(root);b k
res
and
A break;case 0 : System.err.println("can't balance from the right");break;
ta S
truc
tu
break;case 1 : root.bfactor = 0;p.bfactor = 0;
Dat root = rotatetoleft(root);
}return root;return root;
}
16 August, 201032
AVL Trees
s
public avlitem balancefromleft(avlitem root) {avlitem p;
Alg
orith
m avlitem p;avlitem w;p = root.leftchild;
res
and
A switch(p.bfactor){
case 1 : root bfactor = 0;
ta S
truc
tu case -1 : root.bfactor = 0;p.bfactor = 0;root = rotatetoright(root);
Dat break;
case 0 : System.err.println("can't balance from the left");b kbreak;case 1 : w = p.rightchild;
16 August, 201033
AVL Trees
s
switch (w.bfactor){
1 t bf t 1
Alg
orith
m case -1 :root.bfactor = 1;p.bfactor = 0;break;case 0 : root bfactor =0;
res
and
A case 0 : root.bfactor 0;p.bfactor = 0;break;case 1 : root.bfactor = 0;
ta S
truc
tu p.bfactor = -1;}w.bfactor = 0;p = rotatetoleft(p);
Dat p = rotatetoleft(p);
root.leftchild = p;root = rotatetoright(root);
}}return root;
}
16 August, 201034
AVL Trees
s
public avlitem rotatetoright (avlitem root) {avlitem p;
Alg
orith
m if (root == null)System.err.println("Error in the tree");
else
res
and
A if (root.leftchild == null)System.err.println("No left subtree");
else{
ta S
truc
tu {p = root.leftchild;root.leftchild = p.rightchild;root leftchild = p rightchild;
Dat root.leftchild = p.rightchild;
p.rightchild = root;root = p;
}}return root;
}
16 August, 201035
AVL Trees
s
public avlitem rotatetoleft(avlitem root) {avlitem p;
Alg
orith
m if (root == null)System.err.println("Error in the tree");
else
res
and
A if (root.rightchild == null)System.err.println("No right subtree");
else{
ta S
truc
tu {p = root.rightchild;root.rightchild = p.leftchild;p leftchild = root;
Dat p.leftchild = root;
root = p;}
return root;return root;}
16 August, 201036
AVL Trees
s
class avlApp {public static void main (String [] args){
Alg
orith
m public static void main (String [] args){avlitemlist tree1 = new avlitemlist();tree1.insert(50, "Farid");
res
and
A ( , );tree1.insert(40, "Faisal");tree1.insert(70, "Zabi");
ta S
truc
tu tree1.insert(80, "Karim");tree1.insert(75, "Wali");
Dat
tree1.traverse();}}
}
16 August, 201037
AVL Trees
s
As before, after reconstructing the sub-tree, the ti t i b l d S f th i i
Alg
orith
m entire tree is balanced. So for the remaining nodes on the path back to the root node, we do nothing
res
and
A nothing.
ta S
truc
tuD
at