際際滷

際際滷Share a Scribd company logo
AVL Trees 
Algorithms and Data Structures 
M V B REDDY 
GITAM UNIVERSITY
Balanced and unbalanced BST 
4 
2 5 
1 3 
AVL Trees 2 
1 
5 
2 
4 
3 
7 
6 
4 
2 6 
1 3 5 7 
Is this balanced?
Perfect Balance 
 Want a complete tree after every operation 
 tree is full except possibly in the lower right 
 This is expensive 
 For example, insert 2 in the tree on the left and 
then rebuild as a complete tree 
Insert 2 & 
complete tree 
AVL Trees 3 
6 
4 9 
1 5 8 
5 
2 8 
1 4 6 9
AVL - Good but not Perfect 
Balance 
 AVL trees are height-balanced binary 
search trees 
 Balance factor of a node 
 height(left subtree) - height(right subtree) 
 An AVL tree has balance factor 
calculated at every node 
 For every node, heights of left and right 
subtree can differ by no more than 1 
 Store current heights in each node 
AVL Trees 4
Node Heights 
Tree A (AVL) Tree B (AVL) 
1 
2 
6 
1 
4 9 
0 0 
0 
1 5 8 
height of node = h 
balance factor = hleft-hright 
empty height = -1 
AVL Trees 5 
0 
0 
height=2 BF=1-0=1 
0 
6 
1 
4 9 
1 5
Node Heights after Insert 7 
2 
3 
6 
1 
4 9 
0 0 
1 
1 5 8 
height of node = h 
balance factor = hleft-hright 
empty height = -1 
AVL Trees 6 
1 
0 
2 
0 
6 
1 
4 9 
1 5 
07 
07 
balance factor 
1-(-1) = 2 
-1 
Tree A (AVL) Tree B (not AVL)
Insert and Rotation in AVL 
Trees 
 Insert operation may cause balance factor 
to become 2 or 2 for some node 
 only nodes on the path from insertion point to 
root node have possibly changed in height 
 So after the Insert, go back up to the root 
node by node, updating heights 
 If a new balance factor (the difference hleft-hright) 
is 2 or 2, adjust tree by rotation around the 
node 
AVL Trees 7
Single Rotation in an AVL 
Tree 
1 
AVL Trees 8 
2 
2 
1 
0 0 
1 
6 
4 9 
1 5 8 
07 
0 
1 
0 
2 
0 
6 
4 
9 
8 
1 5 
07
Insertions in AVL Trees 
Let the node that needs rebalancing be a. 
There are 4 cases: 
Outside Cases (require single rotation) : 
1. Insertion into left subtree of left child of a. 
2. Insertion into right subtree of right child of a. 
Inside Cases (require double rotation) : 
3. Insertion into right subtree of left child of a. 
4. Insertion into left subtree of right child of a. 
The rebalancing is performed through four 
separate rotation algorithms. 
AVL Trees 9
AVL Insertion: Outside Case 
j 
AVL Trees 10 
k 
X Y 
Z 
Consider a valid 
AVL subtree 
h 
h h
AVL Insertion: Outside Case 
j 
AVL Trees 11 
k 
X 
Y 
Z 
Inserting into X 
destroys the AVL 
property at node j 
h 
h+1 h
AVL Insertion: Outside Case 
j 
Do a right rotation 
AVL Trees 12 
k 
X 
Y 
Z 
h 
h+1 h
Single right rotation 
j 
Do a right rotation 
AVL Trees 13 
k 
X 
Y 
Z 
h 
h+1 h
Outside Case Completed 
j 
Right rotation done! 
(Left rotation is mirror 
symmetric) 
h 
AVL Trees 14 
k 
X Y Z 
AVL property has been restored! 
h+1 
h
AVL Insertion: Inside Case 
j 
AVL Trees 15 
k 
X Y 
Z 
Consider a valid 
AVL subtree 
h 
h h
AVL Insertion: Inside Case 
Does right rotation 
restore balance? 
AVL Trees 16 
Inserting into Y 
destroys the 
AVL property 
at node j 
j 
k 
X 
Y 
Z 
h 
h h+1
AVL Insertion: Inside Case 
j 
Right rotation 
does not restore 
balance now k is 
out of balance 
h+1 h 
AVL Trees 17 
k 
X 
Y 
Z 
h
AVL Insertion: Inside Case 
Consider the structure 
of subtree Y j 
AVL Trees 18 
k 
X 
Y 
Z 
h 
h h+1
AVL Insertion: Inside Case 
j 
AVL Trees 19 
k 
X 
V 
Z 
W 
i 
Y = node i and 
subtrees V and W 
h 
h h+1 
h or h-1
AVL Insertion: Inside Case 
j 
AVL Trees 20 
k 
X 
V 
Z 
W 
i 
We will do a left-right 
double rotation . . .
Double rotation : first rotation 
j 
AVL Trees 21 
k 
X V 
Z 
W 
i 
left rotation complete
Double rotation : second 
j 
AVL Trees 22 
k 
X V 
Z 
W 
i 
rotation 
Now do a right rotation
Double rotation : second 
rotation 
k j 
X V W Z 
AVL Trees 23 
i 
right rotation complete 
Balance has been 
restored 
h h or h-1 h
Implementation 
balance (1,0,-1) 
key 
left right 
No need to keep the height; just the difference in height, 
i.e. the balance factor; this has to be modified on the path of 
insertion even if you dont perform rotations 
Once you have performed a rotation (single or double) you wont 
need to go back up the tree 
AVL Trees 24
Insertion in AVL Trees 
 Insert at the leaf (as for all BST) 
 only nodes on the path from insertion point to 
root node have possibly changed in height 
 So after the Insert, go back up to the root 
node by node, updating heights 
 If a new balance factor (the difference hleft-hright) 
is 2 or 2, adjust tree by rotation around the 
node 
AVL Trees 25
Insert in AVL trees 
Insert(T : reference tree pointer, x : element) : { 
if T = null then 
{T := new tree; T.data := x; height := 0; return;} 
AVL Trees 26 
case 
T.data = x : return ; //Duplicate do nothing 
T.data > x : Insert(T.left, x); 
if ((height(T.left)- height(T.right)) = 2){ 
if (T.left.data > x ) then //outside case 
T = RotatefromLeft (T); 
else //inside case 
T = DoubleRotatefromLeft (T);} 
T.data < x : Insert(T.right, x); 
code similar to the left case 
Endcase 
T.height := max(height(T.left),height(T.right)) +1; 
return; 
}
Example of Insertions in an 
AVL Tree 
0 
AVL Trees 27 
1 
0 
2 
20 
10 30 
25 
0 
35 
Insert 5, 40
Example of Insertions in an 
AVL Tree 
0 
0 1 
AVL Trees 28 
1 
0 
2 
20 
10 30 
25 
1 
35 
0 
5 
20 
10 30 
25 
1 
5 35 
40 
0 
0 
2 
3 
Now Insert 45
Single rotation (outside case) 
2 
1 
0 0 
AVL Trees 29 
2 
0 
3 
20 
10 30 
25 
1 
35 
0 
5 
20 
10 30 
25 
1 
5 40 
40 
0 
0 
0 
1 
2 
3 
45 
Imbalance 
35 45 
Now Insert 34
Double rotation (inside case) 
2 
AVL Trees 30 
3 
0 
3 
20 
10 30 
25 
1 
40 
0 
5 
20 
10 35 
30 
1 
5 40 
45 
0 1 
2 
3 
Imbalance 
0 
1 
45 
Insertion of 34 
35 
34 
0 
0 
1 0 25 34
AVL Tree Deletion 
 Similar but more complex than insertion 
 Rotations and double rotations needed to 
rebalance 
 Imbalance may propagate upward so that 
many rotations may be needed. 
AVL Trees 31
Pros and Cons of AVL Trees 
Arguments for AVL trees: 
1. Search is O(log N) since AVL trees are always balanced. 
2. Insertion and deletions are also O(logn) 
3. The height balancing adds no more than a constant factor to the 
AVL Trees 32 
speed of insertion. 
Arguments against using AVL trees: 
1. Difficult to program & debug; more space for balance factor. 
2. Asymptotically faster but rebalancing costs time. 
3. Most large searches are done in database systems on disk and use 
other structures (e.g. B-trees).

More Related Content

What's hot (20)

Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
Aakash deep Singhal
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
Avl trees
Avl treesAvl trees
Avl trees
ppreeta
Computer notes - AVL Tree
Computer notes - AVL TreeComputer notes - AVL Tree
Computer notes - AVL Tree
ecomputernotes
AVL Tree
AVL TreeAVL Tree
AVL Tree
Dr Sandeep Kumar Poonia
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
Yaksh Jethva
AVL Trees
AVL TreesAVL Trees
AVL Trees
Shreyans Pathak
Avltrees
AvltreesAvltrees
Avltrees
komalkoyal
AVL Tree
AVL TreeAVL Tree
AVL Tree
Chhatra Thapa
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
4. avl
4. avl4. avl
4. avl
Rajandeep Gill
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
ElifTech
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
Amrinder Arora
Splay tree
Splay treeSplay tree
Splay tree
hina firdaus
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
United International University
Splay Tree
Splay TreeSplay Tree
Splay Tree
Dr Sandeep Kumar Poonia
Tree
TreeTree
Tree
SHEETAL WAGHMARE
Trees
TreesTrees
Trees
9590133127
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
Aakash deep Singhal
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
Avl trees
Avl treesAvl trees
Avl trees
ppreeta
Computer notes - AVL Tree
Computer notes - AVL TreeComputer notes - AVL Tree
Computer notes - AVL Tree
ecomputernotes
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
Yaksh Jethva
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
ElifTech
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
Amrinder Arora
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan

Similar to AVL TREE PREPARED BY M V BRAHMANANDA REDDY (20)

avl.ppt
avl.pptavl.ppt
avl.ppt
cotisa2402
avl.ppt
avl.pptavl.ppt
avl.ppt
plagcheck
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
Pran K Mohanty
Lecture3
Lecture3Lecture3
Lecture3
Ali Baba
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
omkarbhabal
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
lokaprasaadvs
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
22001003058
Ie
IeIe
Ie
surya pandian
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
SSE_AndyLi
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
avl insertion-rotation
avl insertion-rotationavl insertion-rotation
avl insertion-rotation
Xad Kuain
AVL Tree.ppt
AVL Tree.pptAVL Tree.ppt
AVL Tree.ppt
ChiragJoshi59934
AVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinyaAVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinya
bagusciptapratama
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
SanghdipUdrake
Avl tress in data structures and algorithms
Avl tress in data structures and algorithmsAvl tress in data structures and algorithms
Avl tress in data structures and algorithms
surya332ygl
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
meenamadhuvandhi2
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMSAVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AnyaForger34
M.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-treeM.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
Lecture3
Lecture3Lecture3
Lecture3
Ali Baba
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
omkarbhabal
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
lokaprasaadvs
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
22001003058
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
SSE_AndyLi
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
avl insertion-rotation
avl insertion-rotationavl insertion-rotation
avl insertion-rotation
Xad Kuain
AVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinyaAVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinya
bagusciptapratama
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
SanghdipUdrake
Avl tress in data structures and algorithms
Avl tress in data structures and algorithmsAvl tress in data structures and algorithms
Avl tress in data structures and algorithms
surya332ygl
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMSAVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AnyaForger34
M.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-treeM.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806

More from Malikireddy Bramhananda Reddy (20)

M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
Malikireddy Bramhananda Reddy
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
Malikireddy Bramhananda Reddy
Data representation UNIT-1
Data representation UNIT-1Data representation UNIT-1
Data representation UNIT-1
Malikireddy Bramhananda Reddy
C SLIDES PREPARED BY M V B REDDY
C SLIDES PREPARED BY  M V B REDDYC SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY M V B REDDY
Malikireddy Bramhananda Reddy
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
Malikireddy Bramhananda Reddy
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
Malikireddy Bramhananda Reddy
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
Malikireddy Bramhananda Reddy
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
Malikireddy Bramhananda Reddy
Vcs29
Vcs29Vcs29
Vcs29
Malikireddy Bramhananda Reddy
Vcs28
Vcs28Vcs28
Vcs28
Malikireddy Bramhananda Reddy
Vcs26
Vcs26Vcs26
Vcs26
Malikireddy Bramhananda Reddy
Vcs24
Vcs24Vcs24
Vcs24
Malikireddy Bramhananda Reddy
Vcs23
Vcs23Vcs23
Vcs23
Malikireddy Bramhananda Reddy
Vcs22
Vcs22Vcs22
Vcs22
Malikireddy Bramhananda Reddy
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
Malikireddy Bramhananda Reddy

AVL TREE PREPARED BY M V BRAHMANANDA REDDY

  • 1. AVL Trees Algorithms and Data Structures M V B REDDY GITAM UNIVERSITY
  • 2. Balanced and unbalanced BST 4 2 5 1 3 AVL Trees 2 1 5 2 4 3 7 6 4 2 6 1 3 5 7 Is this balanced?
  • 3. Perfect Balance Want a complete tree after every operation tree is full except possibly in the lower right This is expensive For example, insert 2 in the tree on the left and then rebuild as a complete tree Insert 2 & complete tree AVL Trees 3 6 4 9 1 5 8 5 2 8 1 4 6 9
  • 4. AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node AVL Trees 4
  • 5. Node Heights Tree A (AVL) Tree B (AVL) 1 2 6 1 4 9 0 0 0 1 5 8 height of node = h balance factor = hleft-hright empty height = -1 AVL Trees 5 0 0 height=2 BF=1-0=1 0 6 1 4 9 1 5
  • 6. Node Heights after Insert 7 2 3 6 1 4 9 0 0 1 1 5 8 height of node = h balance factor = hleft-hright empty height = -1 AVL Trees 6 1 0 2 0 6 1 4 9 1 5 07 07 balance factor 1-(-1) = 2 -1 Tree A (AVL) Tree B (not AVL)
  • 7. Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or 2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or 2, adjust tree by rotation around the node AVL Trees 7
  • 8. Single Rotation in an AVL Tree 1 AVL Trees 8 2 2 1 0 0 1 6 4 9 1 5 8 07 0 1 0 2 0 6 4 9 8 1 5 07
  • 9. Insertions in AVL Trees Let the node that needs rebalancing be a. There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of a. 2. Insertion into right subtree of right child of a. Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of a. 4. Insertion into left subtree of right child of a. The rebalancing is performed through four separate rotation algorithms. AVL Trees 9
  • 10. AVL Insertion: Outside Case j AVL Trees 10 k X Y Z Consider a valid AVL subtree h h h
  • 11. AVL Insertion: Outside Case j AVL Trees 11 k X Y Z Inserting into X destroys the AVL property at node j h h+1 h
  • 12. AVL Insertion: Outside Case j Do a right rotation AVL Trees 12 k X Y Z h h+1 h
  • 13. Single right rotation j Do a right rotation AVL Trees 13 k X Y Z h h+1 h
  • 14. Outside Case Completed j Right rotation done! (Left rotation is mirror symmetric) h AVL Trees 14 k X Y Z AVL property has been restored! h+1 h
  • 15. AVL Insertion: Inside Case j AVL Trees 15 k X Y Z Consider a valid AVL subtree h h h
  • 16. AVL Insertion: Inside Case Does right rotation restore balance? AVL Trees 16 Inserting into Y destroys the AVL property at node j j k X Y Z h h h+1
  • 17. AVL Insertion: Inside Case j Right rotation does not restore balance now k is out of balance h+1 h AVL Trees 17 k X Y Z h
  • 18. AVL Insertion: Inside Case Consider the structure of subtree Y j AVL Trees 18 k X Y Z h h h+1
  • 19. AVL Insertion: Inside Case j AVL Trees 19 k X V Z W i Y = node i and subtrees V and W h h h+1 h or h-1
  • 20. AVL Insertion: Inside Case j AVL Trees 20 k X V Z W i We will do a left-right double rotation . . .
  • 21. Double rotation : first rotation j AVL Trees 21 k X V Z W i left rotation complete
  • 22. Double rotation : second j AVL Trees 22 k X V Z W i rotation Now do a right rotation
  • 23. Double rotation : second rotation k j X V W Z AVL Trees 23 i right rotation complete Balance has been restored h h or h-1 h
  • 24. Implementation balance (1,0,-1) key left right No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you dont perform rotations Once you have performed a rotation (single or double) you wont need to go back up the tree AVL Trees 24
  • 25. Insertion in AVL Trees Insert at the leaf (as for all BST) only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or 2, adjust tree by rotation around the node AVL Trees 25
  • 26. Insert in AVL trees Insert(T : reference tree pointer, x : element) : { if T = null then {T := new tree; T.data := x; height := 0; return;} AVL Trees 26 case T.data = x : return ; //Duplicate do nothing T.data > x : Insert(T.left, x); if ((height(T.left)- height(T.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : Insert(T.right, x); code similar to the left case Endcase T.height := max(height(T.left),height(T.right)) +1; return; }
  • 27. Example of Insertions in an AVL Tree 0 AVL Trees 27 1 0 2 20 10 30 25 0 35 Insert 5, 40
  • 28. Example of Insertions in an AVL Tree 0 0 1 AVL Trees 28 1 0 2 20 10 30 25 1 35 0 5 20 10 30 25 1 5 35 40 0 0 2 3 Now Insert 45
  • 29. Single rotation (outside case) 2 1 0 0 AVL Trees 29 2 0 3 20 10 30 25 1 35 0 5 20 10 30 25 1 5 40 40 0 0 0 1 2 3 45 Imbalance 35 45 Now Insert 34
  • 30. Double rotation (inside case) 2 AVL Trees 30 3 0 3 20 10 30 25 1 40 0 5 20 10 35 30 1 5 40 45 0 1 2 3 Imbalance 0 1 45 Insertion of 34 35 34 0 0 1 0 25 34
  • 31. AVL Tree Deletion Similar but more complex than insertion Rotations and double rotations needed to rebalance Imbalance may propagate upward so that many rotations may be needed. AVL Trees 31
  • 32. Pros and Cons of AVL Trees Arguments for AVL trees: 1. Search is O(log N) since AVL trees are always balanced. 2. Insertion and deletions are also O(logn) 3. The height balancing adds no more than a constant factor to the AVL Trees 32 speed of insertion. Arguments against using AVL trees: 1. Difficult to program & debug; more space for balance factor. 2. Asymptotically faster but rebalancing costs time. 3. Most large searches are done in database systems on disk and use other structures (e.g. B-trees).