AVL Trees: Initially Prepared by Dr. İlyas Çiçekli Improved by Various Bilkent CS202 Instructors
AVL Trees: Initially Prepared by Dr. İlyas Çiçekli Improved by Various Bilkent CS202 Instructors
AVL Trees: Initially Prepared by Dr. İlyas Çiçekli Improved by Various Bilkent CS202 Instructors
Initially prepared by Dr. lyas iekli; improved by various Bilkent CS202 instructors.
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
Then, starting from the insertion point, check for balance at each node
The result is a binary search tree that satisfies the AVL property.
The height of
B (or C) is the
same as the
height of D
The height of
B (or C) is the
same as the
height of A
After insertion, only nodes that are on the path from the insertion
point to the root can have their balances changed.
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)
Spring 2017 CS202 - Fundamental Structures of Computer Science II 18
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
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
p \ p No rotations
Height unchanged
T1 T2 T1 T2
deleted
/ p
p No rotations
Height reduced
T1 T2 T1 T2
deleted
h T2 h T3
deleted h-1 T1 T2
h
Single rotation
Height
Spring 2017
unchanged CS202 - Fundamental Structures of Computer Science II 24
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
h T3
h-1 h-1
T2 T3 h-1 T1 T2
h
deleted
Single rotation
Height
Spring 2017
reduced CS202 - Fundamental Structures of Computer Science II 25
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-1 T2 T3 h-1 T4
deleted T1 h-2
h-1 or
T2 h-2 T3
Double rotation
Height
Spring 2017
reduced CS202 - Fundamental Structures of Computer Science II 26
AVL Trees -- Deletion
Exercise: Delete o from the following AVL tree
m
p
e
c j n s
d h k r u
b o
a g i l t