Lecture08 AVL
Lecture08 AVL
Data Structures
Binary Search Tree - Best
Time
• All BST operations are O(d), where d is
tree depth
• minimum d is d log2N for a binary tree
with N nodes
› What is the best case tree?
› What is the worst case tree?
• So, best case running time of BST
operations is O(log N)
12/26/03 AVL Trees - Lecture 8 2
Binary Search Tree - Worst
Time
• Worst case running time is O(N)
› What happens when you Insert elements in
ascending order?
• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
› Problem: Lack of “balance”:
• compare depths of left and right subtree
› Unbalanced degenerate tree
1 3 5 7 7
12/26/03 AVL Trees - Lecture 8 4
Approaches to balancing trees
• Don't balance
› May end up with some nodes very deep
• Strict balance
› The tree must always be balanced perfectly
• Pretty good balance
› Only allow a little out of balance
• Adjust on access
› Self-adjusting
1 5 8 1 4 6 9
12/26/03 AVL Trees - Lecture 8 7
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 8 8
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-2
h-1
height of node = h
balance factor = hleft-hright
empty height = -1
k h
h
h
Z
X Y
12/26/03 AVL Trees - Lecture 8 16
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 8 17
AVL Insertion: Outside Case
j Do a “right rotation”
k h
h+1 h Z
Y
X
12/26/03 AVL Trees - Lecture 8 18
Single right rotation
j Do a “right rotation”
k h
h+1 h Z
Y
X
12/26/03 AVL Trees - Lecture 8 19
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 8 20
AVL Insertion: Inside Case
Consider a valid
AVL subtree
j
k h
h h Z
X Y
12/26/03 AVL Trees - Lecture 8 21
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 8 22
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 8 23
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 8 24
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 8 25
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 8 26
Double rotation : first rotation
j left rotation complete
i
k Z
W
X V
12/26/03 AVL Trees - Lecture 8 27
Double rotation : second
rotation
j Now do a right rotation
i
k Z
W
X V
12/26/03 AVL Trees - Lecture 8 28
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 8 29
Implementation
balance (1,0,-1)
key
left right
0 45
Now Insert 34
V W