AVL Tree
AVL Tree
CSE 2002
Data Structures Algorithms
Unit-4
Readings
• Reading
› Section 4.4,
1 5 8 1 4 6 9
12/26/03 AVL Trees - Lecture 8
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
12/26/03 AVL Trees - Lecture 9
Height of an AVL Tree
• N(h) = minimum number of nodes in an
AVL tree of height h.
• Basis
› N(0) = 1, N(1) = 2
h
• Induction
› N(h) = N(h-1) + N(h-2) + 1
• Solution (recall Fibonacci analysis)
› N(h) > φh (φ ≈ 1.62) h-1
h-2
height of node = h
balance factor =
hleft-hright
empty height = -1
12/26/03 AVL Trees - Lecture 12
Node Heights after Insert 7
Tree A (AVL) Tree B (not AVL)
2 balance factor
3 1-(-1) = 2
6 6
1 1 1 2
4 9 4 9
0 0 0 0 0 1 -1
1 5 7 1 5 8
0
7
height of node = h
balance factor =
hleft-hright
empty height = -1
12/26/03 AVL Trees - Lecture 13
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
k h
h
h
Z
X Y
12/26/03 AVL Trees - Lecture 17
AVL Insertion: Outside Case
j Inserting into X
destroys the AVL
property at node j
k h
h+1 h Z
Y
X
12/26/03 AVL Trees - Lecture 18
AVL Insertion: Outside Case
j Do a “right rotation”
k h
h+1 h Z
Y
X
12/26/03 AVL Trees - Lecture 19
Single right rotation
j Do a “right rotation”
k h
h+1 h Z
Y
X
12/26/03 AVL Trees - Lecture 20
Outside Case Completed
“Right rotation” done!
k (“Left rotation” is mirror
symmetric)
h+1
j
h h
X Y Z
AVL property has been restored!
12/26/03 AVL Trees - Lecture 21
AVL Insertion: Inside Case
Consider a valid
AVL subtree
j
k h
h h Z
X Y
12/26/03 AVL Trees - Lecture 22
AVL Insertion: Inside Case
Inserting into Y
destroys the j Does “right rotation”
restore balance?
AVL property
at node j
k h
h h+1 Z
X
Y
12/26/03 AVL Trees - Lecture 23
AVL Insertion: Inside Case
k “Right rotation”
does not restore
balance… now k is
h j out of balance
X h+1
h
Z
Y
12/26/03 AVL Trees - Lecture 24
AVL Insertion: Inside Case
Consider the structure
of subtree Y… j
k h
h h+1 Z
X
Y
12/26/03 AVL Trees - Lecture 25
AVL Insertion: Inside Case
Y = node i and
subtrees V and W
j
k h
h
i h+1 Z
X h or h-1
V W
12/26/03 AVL Trees - Lecture 26
AVL Insertion: Inside Case
j We will do a left-right
“double rotation” . . .
k
i Z
X
V W
12/26/03 AVL Trees - Lecture 27
Double rotation : first rotation
j left rotation complete
i
k Z
W
X V
12/26/03 AVL Trees - Lecture 28
Double rotation : second
rotation
j Now do a right rotation
i
k Z
W
X V
12/26/03 AVL Trees - Lecture 29
Double rotation : second
rotation
right rotation complete
k j
h h
h or h-1
X V W Z
12/26/03 AVL Trees - Lecture 30
Implementation
balance (1,0,-1)
key
left right
V
12/26/03 AVL Trees - Lecture 33 W
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
V W