Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

IT623 Algorithms & Data

Structures
AVL Trees
Notes referred from Prof Anish Maturia’s lectures
Balanced Binary Search Trees
• What is the maximum height of a n-node
binary search tree?
• Balanced binary search trees add additional
balance condition
• Maximum height is logarithmic in the number
of nodes
Balance
• Balance t
• height(left subtree) - height(right subtree)
• zero everywhere  perfectly balanced
• small everywhere  balanced enough

Balance between -1 and 1 everywhere 


maximum height of 1.44 log n
AVL Trees
• An AVL tree is a binary search tree in which
• for every node in the tree, the height of the left and right
subtrees differ by at most 1
• Named after Adelson-Velskii and Landis

AVL tree AVL property


violated here
Balance Factors
• balance factor, for a tree node n :
• height of n's left subtree minus height of n's
right subtree
• BFn = Heightn.left - Heightn.right
• convention: height of empty tree is -1
• a binary search tree is an AVL tree if:
• balance factor of each node is 0, 1 or -1
(if no node's two child subtrees differ in height
by more than 1)
More AVL tree examples
-1
0
1 0
0 0 1
0 1 -1
1 0
0 0
0
Not AVL tree examples

1
2 2
1 -2
1 -2 0
0 -1
0 1
0

0
AVL trees: find, insert
• AVL tree find is the same as BST find
• AVL insert: same as BST insert, except that we
might have to “fix” the AVL tree after an insert

-2
Insert(small) S
Insert(middle)
Insert(tall) -1
M
0
T
AVL Tree Insert
• Let x be the deepest node where an imbalance occurs
• Four cases to consider. The insertion is in the
• left subtree of the left child of x
• right subtree of the left child of x
• left subtree of the right child of x
• right subtree of the right child of x
Idea: Cases 1 & 4 are solved by a single rotation
Cases 2 & 3 are solved by a double rotation
Single rotation example
15
x
5 20

3 10 17 21

2 4
1

• Where is 1 going to be inserted into?


• After the insertion, is the tree still an AVL search tree?
• Which is deepest node at which imbalance occurs?
Single rotation example
15
x
5 20

3 10 17 21

2 4
1 15
3 20

2 5 17 21
1 4 10
Basic operation used in AVL trees:
A left child could legally have its parent as its right child !
Single rotation in general
a height h+2
before insert
b
h
Z
h
h+1 Y
X h  -1

X<b<Y<a<Z
height h+2
b after insert
a

h+1 h h
X Y Z
Cases 2 & 3
a height h+2
before insert
b
h
Z
h
X h+1
Y h  -1

b
a
h
X
h+1 h
Z
Y

single rotation fails


Double rotation, step 1
15
8 17

4 10 16

3 6
5
15
8 17

6 10 16
4
3 5
Double rotation, step 2
15
8 17

6 10 16
4

3 5

15
6 17
4 8 16
3 5 10
Double rotation in general
height h+2
before insert
a
b
h0 c h
Z
h h-1
W h Y
X

W < b <X < c < Y < a < Z


height h+2 c
after insert
b a

h h h-1 h
Y
W X Z
A sequence of example insertions
Insert 3, 2, 1, 4, 5, 6, 7
After inserting 1

Rotate right
After inserting 3 After inserting 2

3 3 3

2 2

1 2

1 3

After single right rotation


Example continued 2

1 3
After inserting 4

Rotate left
2 2

1 3 1 4

4 3 5

After inserting 5 5 After single left rotation


between 3 and 4.
Example continued
Rotate left

2 4

1 4 2 5

3 5 1 3 6

6 After singe left rotation


After inserting 6
between 2 and 4.
Example continued
4 Rotate left

2 5

1 3 6

After inserting 7
7
4

2 6

1 3 5 7
After singe left rotation
between 5 and 6.
Example
• Insert 16, 15, 14, 13, 12, 10, and 8, and 9 to the tree
obtained in the previous example

2 6

1 3 5 7

After inserting 16 16
4

After inserting 15 2 6

Case 3
1 3 5 7 k1

16 k3

15 k2
4
After right-left double rotation
among 7, 16, 15
2 6

1 3 5 15

7 16
4

k1
After inserting 14 2 6
Case 3
k3
1 3 5 15

k2 7 16

14
4
After right-left double rotation
among 6,15,7 k2
2 7

k1 k3
1 3 6 15

5 14 16
4 k1
k2
2 7

After inserting 13 Case 4


1 3 6 15

5 14 16

7 13
After single left rotation
between 4 and 7
4 15

2 6 14 16

1 3 5 13
7
After inserting 12

4 15

2 6 14 16
k2

1 3 5 13 k
1

7 After singe right rotation


12 between 14 and 13

4 15

2 6 13 k1 16

1 3 5 12 14 k2
7

4 15 k2
After inserting 11

2 6 13 k1 16

1 3 5 12 14

11 7

4 13

After single right rotation


between 15 and 13. 2 6 12 15

1 3 5 11 14 16
7

13
After inserting 10
4

2 6 12 15

1 3 5 11 14 16

10 7

4 13

After single right rotation 2 6 11 15


Between 12 and 11

1 3 5 10 12 14 16
7

4 13 After inserting 8

2 6 11 15

1 3 5 10 12 14 16

8
7

4 13 After inserting 9

2 6 11 15

1 3 5 10 12 14 16
7

8
4 13

9
2 6 11 15

After left-right double 1 3 5 9 12 14 16


rotation
among 10, 8 and 9
8 10
Insertion algorithm sketch
• First, insert the new key as a new leaf just as in ordinary binary search
tree
• Then trace the path from the new leaf towards the root. For each
node x encountered, check if heights of left(x) and right(x) differ by at
most 1
• If yes, proceed to parent(x)
• If not, restructure by doing either a single rotation or a double rotation
• Note: once we perform a rotation at a node x, we won’t need to
perform any rotation at any ancestor of x
Depth of an AVL tree
Theorem: Any AVL tree with n nodes has height less
than 1.441 log n

Proof: Given an n-node AVL tree, we want to find an


upper bound on the height of the tree.
Fix h. What is the smallest n such that there is an
AVL tree of height h with n nodes?
Let Sh be the set of all AVL trees of height h that
have as few nodes as possible
Let wh be the number of nodes in any one of these
trees
w0 = 1, w1 = 2

Suppose T  Sh, where h  2. Let TL and TR be T’s left


and right subtrees. Since T has height h, either TL
or TR has height h-1. Suppose it’s TR
By definition, both TL and TR are AVL trees. In fact,
TR  Sh-1 or else it could be replaced by a smaller
AVL tree of height h-1 to give an AVL tree of height
h that is smaller than T
Similarly, TL  Sh-2
Therefore, wh = 1 + wh-2 + wh-1

Claim: For h  0, wh  h , where  = (1 + 5) / 2 


1.6
Proof: The proof is by induction on h.
Basis step: h=0. w0 = 1 = 0
h=1. w1 = 2 > 1
Induction step: Suppose the claim is true for 0  m
 h, where h  1
Then
wh+1 = 1 + wh-1 + wh
 1 + h-1 + h (by the i.h.)
= 1 + h-1 (1 + )
= 1 + h+1 (1+ = 2)
> h+1
Thus, the claim is true
From the claim, in an n-node AVL tree of height h,
n  wh  h (from the Claim)
h  log  n
= (log n) / (log ) < 1.441 log n
An AVL Tree
3 10 data
10 3 height

1 2
5 15
0 0 0 1
2 9 12 20
0 0
17 30
Rotation
• Our basic operation for balancing is called rotation:

y right-rotate(y) x
x C A y
left-rotate(x)
A B B C

• Does the rotation preserve the BST property?


• What would the algorithm for right-rotate() actually do?
Rotation
y x
right-rotate(y)
x C A y
A B B C

• Answer: A lot of pointer manipulation


• x keeps its left child
• y keeps its right child
• x’s right child becomes y’s left child
• x’s and y’s parents change
• What is the running time?
Algorithm right-rotate(y):
{ Performs a right rotation on a given node y of a BST, and returns the
root of the subtree after the rotation. Assumes left child of y is not nil. }

x := left[y]
left[y] := right[x] x’s right child becomes y’s left child
p[right[x]] := y
p[x] := p[y] y’s parent becomes x’s parent
right[x] := y y becomes x’s right child
p[y] := x x becomes parent of y
if p[y] ≠ nil Bug
then
if y = right[p[y]] if y is the right (left) child of
then right[p[y]] := x its parent then x becomes right
else left[p[y]] := x (left) child of y’s parent
return x

y x
x C A y
A B B C
Algorithm right-rotate(y):
{ Performs a right rotation on a given node y of a BST. Assumes left
child of y is not nil. }

x := left[y]
left[y] := right[x] x’s right child becomes y’s left child
p[right[x]] := y
p[x] := p[y] y’s parent becomes x’s parent
right[x] := y y becomes x’s right child
if p[y] ≠ nil
then
if y = right[p[y]] if y is the right (left) child of
then right[p[y]] := x its parent then x becomes right
else left[p[y]] := x (left) child of y’s parent
p[y] := x x becomes parent of y

y x
x C A y
A B B C
Algorithm left-rotate(x):
{ Performs a left rotation on a given node x of a BST. Assumes right
child of x is not nil. }

y := right[x]
right[x] := left[y] y’s left child becomes x’s right child
p[left[y]] := x
p[y] := p[x] x’s parent becomes y’s parent
left[y] := x x becomes y’s left child
if p[x] ≠ nil
then
if x = left[p[x]] if x is the left (right) child of
then left[p[x]] := y its parent then y becomes left
else right[p[x]] := y (right) child of x’s parent
p[x] := y y becomes parent of x

y x
x C A y
A B B C
Algorithm balance(x):
{ Takes a subtree rooted at x whose left and right subtrees are height
balanced. Returns the root of the height balanced subtree after the
rotations. }

if |height(right[x]) – height(left[x]))| ≤ 1
then
return x
if height(left[x]) > height(right(x))
then
y := left[x]
if height(right[y]) > height(left[y])
then left-rotate(y)
right-rotate(x)
else
y := right[x]
if height(left[y]) > height(right[y])
then right-rotate(y)
left-rotate(x)
return p[x]
Algorithm avl-insert(x, z):
{ x is a pointer to the root of an AVL tree. z is a pointer to a new
node to be inserted into the tree. Returns a pointer to root of the
tree after insertion. }

if x := nil
then
height[z] := 0
return z

if key[z] < key[x]


then
y := avl-insert(left[x], z)
left[x] := y
else
y := avl-insert(right[x], z)
right[x] := y

parent[y] := x
height[x] := height[y] + 1

return balance(x)

You might also like