AVL Trees: Balanced Search Trees for Efficient Operations

AVL Trees
CS202 - Fundamental Structures of Computer Science II
1
Initially prepared by Dr. 
İ
lyas 
Çiç
ekli; improved by various Bilkent CS202 instructors.
CS202 - Fundamental Structures of Computer Science II
2
Balanced Search Trees
The height of a binary search tree is sensitive to the order of
insertions and deletions.
The height of a binary search tree is between  
 
log
2
(
N
+1)
 
  and  
N
.
So, the worst case behavior of some BST operations are O(N).
There are various search trees that can retain their balance at the
end of each insertion and deletion.
AVL Trees
2-3 Trees
2-3-4 Trees
Red-Black Trees
In these height balanced search trees, the run time complexity of
insertion, deletion, and retrieval operations is O(log
2
N) at the worst
case.
CS202 - Fundamental Structures of Computer Science II
3
AVL Trees
An AVL tree is a binary search tree with a 
balance
 condition.
AVL is named for its inventors:  
A
del’son-
V
el’skii and 
L
andis
AVL tree 
approximates
 the ideal tree (completely balanced tree).
AVL Tree maintains a height close to the minimum.
AVL Trees
 
Definition
:
 
An AVL tree is a binary search tree such that
for any node in the tree, the height of the left
and right subtrees can differ by at most 1.
CS202 - Fundamental Structures of Computer Science II
4
An AVL tree
NOT an AVL tree
(unbalanced nodes
are darkened)
AVL Trees -- Properties
The depth of a typical node in an AVL tree is very close to the optimal
log
2
N
.
Consequently, all searching operations in an AVL tree have
logarithmic worst-case bounds.
An update (insert or delete) in an AVL tree could destroy the balance.
 
 
 
 
It must then be rebalanced before the operation can be
  
considered complete.
CS202 - Fundamental Structures of Computer Science II
5
AVL Tree -- Insertions
Insert is the same as 
Binary Search Tree 
insertion
Then, starting from the insertion point, check for balance at each node
It is enough to perform 
correction “rotation” 
only 
at the first node
w
here imbalance occurs
On the path from the inserted node to the root.
CS202 - Fundamental Structures of Computer Science II
6
AVL -- Insertions
An AVL violation might occur in four possible cases:
1)
Insertion into left subtree of left child of node 
n
2)
Insertion into right subtree of left child of node 
n
3)
Insertion into left subtree of right child of node 
n
4)
Insertion into right subtree of right child of node 
n
(1) and (4) are mirror cases
(2) and (3) are mirror cases
If insertion occurs “
outside
” (1 & 4), then perform 
single rotation
.
If insertion occurs “
inside
” (2 & 3), then perform 
double rotation.
CS202 - Fundamental Structures of Computer Science II
7
CS202 - Fundamental Structures of Computer Science II
8
AVL Trees -- Balance Operations
Balance is restored by tree 
rotations
.
There are four different cases for rotations:
1.
Single Right Rotation
2.
Single Left Rotation
3.
Double Right-Left Rotation
4.
Double Left-Right Rotation
CS202 - Fundamental Structures of Computer Science II
9
AVL Trees -- Single Rotation
A single rotation switches the roles of the parent and the child while
maintaining the search order.
We rotate between a node and its child (left or right).
Child becomes parent
Parent becomes right child in Case 1 (single right rotation)
 
Parent becomes left child in Case 2 (single left rotation)
The result is a binary search tree that satisfies the AVL property.
CS202 - Fundamental Structures of Computer Science II
10
Case 1 -- Single Right Rotation
Child becomes parent
Parent becomes right child
CS202 - Fundamental Structures of Computer Science II
11
Case 1 -- Single Right Rotation
Child becomes parent
Parent becomes right child
CS202 - Fundamental Structures of Computer Science II
12
Case 2 – Single Left Rotation
Child becomes parent
Parent becomes left child
CS202 - Fundamental Structures of Computer Science II
13
Case 3 -- Double Right-Left Rotation
The height of
B (or C) is the
same as the
height of D
First perform single right rotation on k2 and k3
Then perform single left rotation on k2 and k1
CS202 - Fundamental Structures of Computer Science II
14
Case 4 -- Double Left-Right Rotation
First perform single left rotation on k2 and k1
Then perform single right rotation on k2 and k3
The height of
B (or C) is the
same as the
height of A
CS202 - Fundamental Structures of Computer Science II
15
Case 4 -- Double Left-Right Rotation
First perform single left rotation on k2 and k1
Then perform single right rotation on k2 and k3
CS202 - Fundamental Structures of Computer Science II
16
AVL Trees -- Insertion
It is enough to perform rotation only at the first node
Where imbalance occurs
On the path from the inserted node to the root.
The rotation takes 
O(1) time.
After insertion, only nodes that are on the path from the insertion
point to the root can have their balances changed.
Hence insertion is O(logN)
AVL Trees -- Insertion
Exercise
: 
Starting with an empty AVL tree, insert the following items
  
    7    6    5    4    3    2    1    8    9    10    11    12
Check the following applet for more exercises.
  
http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html
CS202 - Fundamental Structures of Computer Science II
17
CS202 - Fundamental Structures of Computer Science II
18
AVL Trees -- Deletion
Deletion is more complicated.
It requires both single and double rotations
We may need more than one rebalance operation (rotation) on the path from
the deleted node back to the root.
Steps:
First delete the node the same as deleting it from a binary search tree
Remember that a node can be either 
a leaf node 
or 
a node with a single child 
or
a node with two children
Walk through from the deleted node back to the root and rebalance the nodes
on the path if required
Since a rotation can change the height of the original tree
Deletion is O(logN)
Each rotation takes O(1) time
We may have at most h (height) rotations, where h = O(logN)
AVL Trees -- Deletion
For the implementation
We have a 
shorter
 flag that shows if a subtree has been shortened
Each node is associated with a 
balance
 
factor
left-high
 
the height of the left subtree is higher than that of the right subtree
right-high
 
 
the height of the right subtree is higher than that of the left subtree
equal
 
  
 
the height of the left and right subtrees is equal
In the deletion algorithm
Shorter is initialized as true
Starting from the deleted node back to the root, take an action depending on
The value of shorter
The balance factor of the current node
Sometimes the balance factor of a child of the current node
Until shorter becomes false
CS202 - Fundamental Structures of Computer Science II
19
AVL Trees -- Deletion
Three cases according to the balance factor of the current node
1.
The balance factor is equal
 
 
no rotation
2.
The balance factor is not equal and the taller subtree was shortened
 
 
no rotation
3.
The balance factor is not equal and the shorter subtree was shortened
 
 
rotation is necessary
CS202 - Fundamental Structures of Computer Science II
20
AVL Trees -- Deletion
Case 1
: 
The balance factor of p is equal.
Change the balance factor of 
p
 to 
right-high
 (or 
left-high
)
Shorter becomes false
CS202 - Fundamental Structures of Computer Science II
21
No rotations
Height unchanged
AVL Trees -- Deletion
Case 2
: 
The balance factor of p is not equal and the taller subtree is shortened.
Change the balance factor of 
p 
to 
equal
Shorter remains true
CS202 - Fundamental Structures of Computer Science II
22
No rotations
Height reduced
AVL Trees -- Deletion
Case 3
: 
The balance factor of p is not equal and the shorter subtree is shortened.
Rotation is necessary
Let 
q
 be the root of the taller subtree of 
p
We have three sub-cases according to the balance factor of 
q
CS202 - Fundamental Structures of Computer Science II
23
AVL Trees -- Deletion
Case 3a
: 
The balance factor of q is equal.
Apply a single rotation
Change the balance factor of 
q 
to 
left-high
 (or 
right-high
)
Shorter becomes false
CS202 - Fundamental Structures of Computer Science II
24
Single rotation
Height unchanged
AVL Trees -- Deletion
Case 3b
: 
The balance factor of q is the same as that of p.
Apply a single rotation
Change the balance factors of 
p 
and 
q 
to 
equal
Shorter remains true
CS202 - Fundamental Structures of Computer Science II
25
Single rotation
Height reduced
AVL Trees -- Deletion
Case 3c
: 
The balance factor of q is the opposite of that of p.
Apply a double rotation
Change the balance factor of the new root to 
equal
Also change the balance factors of 
p
 and 
q
Shorter remains true
CS202 - Fundamental Structures of Computer Science II
26
Double rotation
Height reduced
AVL Trees -- Deletion
Exercise
: 
Delete 
o
 from the following AVL tree
Check the following applet for more exercises.
  
http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html
CS202 - Fundamental Structures of Computer Science II
27
AVL Trees -- Analysis
 
What is the minimum number of
nodes in an AVL tree?
  
minN(0) = 0
  
minN(1) = 1
  
minN(2) = 2
  
minN(3) = 4
  
...
 
  
minN(h) = minN(h-1) + minN(h-2) + 1
 
Max height of an N-node AVL tree is
less than 1.44 log N
CS202 - Fundamental Structures of Computer Science II
28
Slide Note

lec06-balancedtrees

Embed
Share

AVL Trees are balanced search trees that ensure efficient insertion, deletion, and retrieval operations with a worst-case time complexity of O(log N). Named after their inventors Adelson-Velskii and Landis, AVL Trees maintain balance by limiting the height difference between left and right subtrees of any node to at most 1. This property allows AVL Trees to approach the optimal logarithmic depth, resulting in logarithmic worst-case bounds for all searching operations. By performing rotation to correct imbalances during insertions, AVL Trees retain their balance and optimize performance in various applications.

  • AVL Trees
  • Balanced Search Trees
  • Binary Search Tree
  • Data Structures
  • Efficient Algorithms

Uploaded on Nov 25, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. AVL Trees Initially prepared by Dr. lyas i ekli; improved by various Bilkent CS202 instructors. CS202 - Fundamental Structures of Computer Science II 1

  2. Balanced Search Trees The height of a binary search tree is sensitive to the order of insertions and deletions. The height of a binary search tree is between log2(N+1) and N. So, the worst case behavior of some BST operations are O(N). There are various search trees that can retain their balance at the end of each insertion and deletion. AVL Trees 2-3 Trees 2-3-4 Trees Red-Black Trees In these height balanced search trees, the run time complexity of insertion, deletion, and retrieval operations is O(log2N) at the worst case. CS202 - Fundamental Structures of Computer Science II 2

  3. AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel son-Vel skii and Landis AVL tree approximates the ideal tree (completely balanced tree). AVL Tree maintains a height close to the minimum. CS202 - Fundamental Structures of Computer Science II 3

  4. AVL Trees Definition: An AVL tree is a binary search tree such that for any node in the tree, the height of the left and right subtrees can differ by at most 1. An AVL tree NOT an AVL tree (unbalanced nodes are darkened) CS202 - Fundamental Structures of Computer Science II 4

  5. AVL Trees -- Properties The depth of a typical node in an AVL tree is very close to the optimal log2N. Consequently, all searching operations in an AVL tree have logarithmic worst-case bounds. An update (insert or delete) in an AVL tree could destroy the balance. It must then be rebalanced before the operation can be considered complete. CS202 - Fundamental Structures of Computer Science II 5

  6. AVL Tree -- Insertions Insert is the same as Binary Search Tree insertion Then, starting from the insertion point, check for balance at each node It is enough to perform correction rotation only at the first node where imbalance occurs On the path from the inserted node to the root. CS202 - Fundamental Structures of Computer Science II 6

  7. AVL -- Insertions An AVL violation might occur in four possible cases: 1) Insertion into left subtree of left child of node n 2) Insertion into right subtree of left child of node n 3) Insertion into left subtree of right child of node n 4) Insertion into right subtree of right child of node n (1) and (4) are mirror cases (2) and (3) are mirror cases If insertion occurs outside (1 & 4), then perform single rotation. If insertion occurs inside (2 & 3), then perform double rotation. CS202 - Fundamental Structures of Computer Science II 7

  8. AVL Trees -- Balance Operations Balance is restored by tree rotations. There are four different cases for rotations: 1. Single Right Rotation 2. Single Left Rotation 3. Double Right-Left Rotation 4. Double Left-Right Rotation CS202 - Fundamental Structures of Computer Science II 8

  9. AVL Trees -- Single Rotation A single rotation switches the roles of the parent and the child while maintaining the search order. We rotate between a node and its child (left or right). Child becomes parent Parent becomes right child in Case 1 (single right rotation) Parent becomes left child in Case 2 (single left rotation) The result is a binary search tree that satisfies the AVL property. CS202 - Fundamental Structures of Computer Science II 9

  10. Case 1 -- Single Right Rotation Child becomes parent Parent becomes right child CS202 - Fundamental Structures of Computer Science II 10

  11. Case 1 -- Single Right Rotation Child becomes parent Parent becomes right child CS202 - Fundamental Structures of Computer Science II 11

  12. Case 2 Single Left Rotation After Rotation Before Rotation Child becomes parent Parent becomes left child CS202 - Fundamental Structures of Computer Science II 12

  13. Case 3 -- Double Right-Left Rotation The height of B (or C) is the same as the height of D First perform single right rotation on k2 and k3 Then perform single left rotation on k2 and k1 CS202 - Fundamental Structures of Computer Science II 13

  14. Case 4 -- Double Left-Right Rotation The height of B (or C) is the same as the height of A First perform single left rotation on k2 and k1 Then perform single right rotation on k2 and k3 CS202 - Fundamental Structures of Computer Science II 14

  15. Case 4 -- Double Left-Right Rotation First perform single left rotation on k2 and k1 Then perform single right rotation on k2 and k3 CS202 - Fundamental Structures of Computer Science II 15

  16. AVL Trees -- Insertion It is enough to perform rotation only at the first node Where imbalance occurs On the path from the inserted node to the root. The rotation takes O(1) time. After insertion, only nodes that are on the path from the insertion point to the root can have their balances changed. Hence insertion is O(logN) CS202 - Fundamental Structures of Computer Science II 16

  17. AVL Trees -- Insertion Exercise: Starting with an empty AVL tree, insert the following items 7 6 5 4 3 2 1 8 9 10 11 12 Check the following applet for more exercises. http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html CS202 - Fundamental Structures of Computer Science II 17

  18. AVL Trees -- Deletion Deletion is more complicated. It requires both single and double rotations We may need more than one rebalance operation (rotation) on the path from the deleted node back to the root. Steps: First delete the node the same as deleting it from a binary search tree Remember that a node can be either a leaf node or a node with a single child or a node with two children Walk through from the deleted node back to the root and rebalance the nodes on the path if required Since a rotation can change the height of the original tree Deletion is O(logN) Each rotation takes O(1) time We may have at most h (height) rotations, where h = O(logN) CS202 - Fundamental Structures of Computer Science II 18

  19. AVL Trees -- Deletion For the implementation We have a shorter flag that shows if a subtree has been shortened Each node is associated with a balancefactor left-high the height of the left subtree is higher than that of the right subtree right-high the height of the right subtree is higher than that of the left subtree equal the height of the left and right subtrees is equal In the deletion algorithm Shorter is initialized as true Starting from the deleted node back to the root, take an action depending on The value of shorter The balance factor of the current node Sometimes the balance factor of a child of the current node Until shorter becomes false CS202 - Fundamental Structures of Computer Science II 19

  20. AVL Trees -- Deletion Three cases according to the balance factor of the current node 1. The balance factor is equal no rotation 2. The balance factor is not equal and the taller subtree was shortened no rotation 3. The balance factor is not equal and the shorter subtree was shortened rotation is necessary CS202 - Fundamental Structures of Computer Science II 20

  21. AVL Trees -- Deletion Case 1: The balance factor of p is equal. Change the balance factor of p to right-high (or left-high) Shorter becomes false p p No rotations Height unchanged \ T1 T2 T1 T2 deleted CS202 - Fundamental Structures of Computer Science II 21

  22. AVL Trees -- Deletion Case 2: The balance factor of p is not equal and the taller subtree is shortened. Change the balance factor of p to equal Shorter remains true p p No rotations Height reduced / T1 T2 T1 T2 deleted CS202 - Fundamental Structures of Computer Science II 22

  23. AVL Trees -- Deletion Case 3: The balance factor of p is not equal and the shorter subtree is shortened. Rotation is necessary Let q be the root of the taller subtree of p We have three sub-cases according to the balance factor of q CS202 - Fundamental Structures of Computer Science II 23

  24. AVL Trees -- Deletion Case 3a: The balance factor of q is equal. Apply a single rotation Change the balance factor of q to left-high (or right-high) Shorter becomes false q / p \ q p \ h-1 T3 T1 h T2 T3 h h h-1 deleted T1 T2 h Single rotation Height unchanged CS202 - Fundamental Structures of Computer Science II 24

  25. AVL Trees -- Deletion Case 3b: The balance factor of q is the same as that of p. Apply a single rotation Change the balance factors of p and q to equal Shorter remains true p q \ q \ p h-1 T1 T3 h h-1 h-1 h-1 T2 T3 T1 T2 h deleted Single rotation Height reduced CS202 - Fundamental Structures of Computer Science II 25

  26. AVL Trees -- Deletion Case 3c: The balance factor of q is the opposite of that of p. Apply a double rotation Change the balance factor of the new root to equal Also change the balance factors of p and q Shorter remains true p \ r q / p q h-1 r T1 T4 h-1 h-1 or h-2 T4 T2 T3 h-1 h-1 T1 deleted h-1 or h-2 T2 T3 Double rotation Height reduced CS202 - Fundamental Structures of Computer Science II 26

  27. AVL Trees -- Deletion Exercise: Delete o from the following AVL tree m p e c j n s k d h r u b o a g i l t f Check the following applet for more exercises. http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html CS202 - Fundamental Structures of Computer Science II 27

  28. AVL Trees -- Analysis H 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 30 40 50 minN logN 2,81 3,58 4,32 5,04 5,75 6,46 7,16 7,86 8,55 9,25 9,95 10,64 11,33 12,03 12,72 13,42 14,11 21,05 28,00 34,94 H / logN 7 1,42 1,39 1,39 1,39 1,39 1,39 1,40 1,40 1,40 1,41 1,41 1,41 1,41 1,41 1,41 1,42 1,42 1,42 1,43 1,43 What is the minimum number of nodes in an AVL tree? minN(0) = 0 minN(1) = 1 minN(2) = 2 minN(3) = 4 ... minN(h) = minN(h-1) + minN(h-2) + 1 12 20 33 54 88 143 232 376 609 986 1.596 2.583 4.180 6.764 10.945 17.710 2.178.308 267.914.295 32.951.280.098 Max height of an N-node AVL tree is less than 1.44 log N CS202 - Fundamental Structures of Computer Science II 28

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#