Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

AVLTrees

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

AVL Trees

Balanced binary tree


 The disadvantage of a binary search tree is that its height
can be as large as N-1
 This means that the time needed to perform insertion and
deletion and many other operations can be O(N) in the
worst case
 We want a tree with small height
 A binary tree with N node has height at least log N
 Thus, our goal is to keep the height of a binary search
tree O(log N)
 Such trees are called balanced binary search trees.
Balanced and unbalanced BST
1
2
3

4
4
5
2 6
6
1 3 5 7 7
Balancing Binary Search Trees
 Many algorithms exist for keeping binary search
trees balanced
 Adelson-Velskii and Landis (AVL) trees (height-
balanced trees) and Red black trees
 Splay trees and other self-adjusting trees
 B-trees and other multiway search trees
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) and maintains a height close to the minimum.
 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.
AVL tree
 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.
AVL property
violated here
Binary Search Trees

(a) an AVL tree; (b) not an AVL tree


AVL Trees
Non-AVL Trees
Properties
 The depth of a typical node in an AVL tree is very
close to the optimal log N.
 Consequently, all searching operations in an AVL
tree have logarithmic worst-case bounds.
 An update (insert or remove) in an AVL tree could
destroy the balance. It must then be rebalanced before
the operation can be considered complete.
 After an insertion, only nodes that are on the path
from the insertion point to the root can have their
balances altered.
Insertion and Deletions in AVL Trees
 They are performed as in binary search trees
 If the balance is destroyed, rotation(s) is
performed to correct balance
 For insertions, one rotation is sufficient
 For deletions, O(log n) rotations at most are
needed
Insertion in an AVL Tree

 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.
 For insertion, once we perform a rotation at a node x, we
won’t need to perform any rotation at any ancestor of x.
AVL Tree Rotations

 First insert 14 and 15:

14

15

 Now insert 16.


AVL Tree Rotations
Single rotations:

 Inserting 16 causes AVL violation:

14

15

16

 Need to rotate.
AVL Tree Rotations
Single rotations:

 Rotation type:

14

15

16
AVL Tree Rotations
Single rotations:

 Rotation restores AVL balance:

15

14 16
Balance Condition Violation
 If condition violated after a node insertion
 Which nodes do we need to rotate?
 Only nodes on path from insertion point to root may have their
balance altered
 Rebalance the tree through rotation at the deepest node
with balance violated
 The entire tree will be rebalanced
Balance Condition Violation

 Violation cases at node k (deepest node)


1. An insertion into left subtree of left child of k
2. An insertion into right subtree of left child of k
3. An insertion into left subtree of right child of k
4. An insertion into right subtree of right child of k
 Cases 1 and 2 equivalent
 Single rotation to rebalance
 Cases 3 and 4 equivalent
 Double rotation to rebalance

18
An insertion into left subtree of left
child of k

Single R – rotation

• Single right rotation


• Rotates the edge connecting the root and its left child in the binary tree
Single Rotation (Case 1)

 Replace node k2 by node k1


 Set node k2 to be right child of node k1
 Set subtree Y to be left child of node k2
 Case 2 is similar 20
An insertion into right subtree of
right child of k

Single L – rotation

• Single left rotation


• Rotates the edge connecting the root and its right child in the binary tree
An insertion into left subtree of right
child of k

Double LR – rotation

• Double left-right rotation


• Combination of two rotations
1. perform left rotation of the
left sub-tree of root k
2. perform right rotation of the
new tree rooted at k
An insertion into right subtree of left
child of k

Double RL – rotation

• Double right-left rotation


• Combination of two rotations
1. perform right rotation of the
right sub-tree of root k
2. perform left rotation of the
new tree rooted at k
When to perform single or double rotation?
IF tree is right heavy
{
IF tree's right subtree is left heavy
Perform Double Left rotation
ELSE
Perform Single Left rotation
}
ELSE IF tree is left heavy
{
IF tree's left subtree is right heavy
Perform Double Right rotation
ELSE
Perform Single Right rotation
}
AVL Balancing : Four Rotations

X1 X1 X3
Double right
X2 X2 X2
Single right X2 X1
X3 X3
X3 X1

X1 X1
X2 X3
Single left Double left
X2 X2
X1 X3 X1 X2

X3
X3
Example

 Inserting 3, 2, 1, and then 4 to 7 sequentially into


empty AVL tree
3
2
2
1 3
1

26
Example (Cont’d)

2
 Inserting 4
1 3

4
Inserting 5
2 2

1 3 4
1
4 5
3
5 27
Example (Cont’d)

4
2
 Inserting 6
2 5
1 4
1 3 6
3 5
 Inserting 7 6
4
4
2 6
2 5
1 3 5 7
1 3 6

7 28
Example
 Continuing the previous example by inserting
 16 down to 10, and then 8 and 9

 Inserting 16 and 15 4
4
2 6
2 6
1 3 5 15
1 3 5 7
7 16
16
15

29
Example (Cont’d)
 Inserting 14
4
4
2 7
2 6
1 3 6 15
1 3 5 15

14 16
7 16 5

14

30
Another Example

Insert 3,2,1,4,5,6,7, 16,15,14 Single rotation

3 2
3
3

2 1
Fig 1 2 3
Fig 4
Fig 2
2 1 2
Single rotation
Fig 3
1
1 3
3
Fig 5 Fig 6 4
4
5
2
2 Single rotation
1
1 4
4
3 5
3 5
Fig 8
Fig 7 6
4 4

Single rotation
2 2
5 5
1 3 6 1 3 6
4

Fig 9 Fig 10 7
2
6
1 3 7

5 Fig 11
4

2
6
1 3 7

5 16
Fig 12

4
Double rotation
4
2
6
2
7 6
1 3
1 3 15
5 16 5
16
Fig 13 7
15 Fig 14
4 4
Double rotation

2 2 7
6
15 1 3 15
1 3 5 6
16
7 5 14 16
Fig 15
14 Fig 16
AVL Tree Rotations
Single rotations:

 Now insert 13 and 12:


15

14 16

13

12

 AVL violation - need to rotate.


AVL Tree Rotations
Single rotations:

 Rotation type:
15

14 16

13

12
AVL Tree Rotations
Single rotations:

15

13 16

12 14

 Now insert 11.


AVL Tree Rotations
Single rotations:
15

13 16

12 14

11

 AVL violation – need to rotate


AVL Tree Rotations
Single rotations:
 Rotation type: 15

13 16

12 14

11
AVL Tree Rotations
Single rotations:

13

12 15

14 16
11

 Now insert 10.


AVL Tree Rotations
Single rotations:

13

12 15

14 16
11

10

 AVL violation – need to rotate


AVL Tree Rotations
Single rotations:
 Rotation type:
13

12 15

14 16
11

10
AVL Tree Rotations
Single rotations:

13

11 15

10 12 14 16

 AVL balance restored.


AVL Tree Rotations
Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8
 First insert 1 and 2:
13

11 15

10 12 14 16
AVL Tree Rotations
Double rotations:
 AVL violation - rotate
13

11 15

14 16
10 12

2
AVL Tree Rotations
Double rotations:
 Rotation type:
13

11 15

14 16
10 12

2
AVL Tree Rotations
Double rotations:
 AVL balance restored:
13

11 15

2 12 14 16

1 10

 Now insert 3.
AVL Tree Rotations
Double rotations:
 AVL violation – rotate:
13

11 15

2 12 14 16

1 10

3
AVL Tree Rotations
Double rotations:
 Rotation type:
13

11 15

2 12 14 16

1 10

3
AVL Tree Rotations
Double rotations:
 AVL balance restored:
13

10 15

2 11 14 16

1 3 12

 Now insert 4.
AVL Tree Rotations
Double rotations:
 AVL violation - rotate
13

10 15

2 11 14 16

1 3 12

4
AVL Tree Rotations
Double rotations:
 Rotation type:
13

10 15

2 11 14 16

1 3 12

4
AVL Tree Rotations
Double rotations:

10
2 13

1 11 15
3

4 12 14 16

 Now insert 5.
AVL Tree Rotations
Double rotations:

10
2 13

1 11 15
3

4 12 14 16

 AVL violation – rotate.


AVL Tree Rotations
Single rotations:
 Rotation type:
10
2 13

1 11 15
3

4 12 14 16

5
AVL Tree Rotations
Single rotations:
 AVL balance restored:
10
2 13

1 11 15
4

3 5 12 14 16

 Now insert 7.
AVL Tree Rotations
Single rotations:
 AVL violation – rotate.
10
2 13

1 11 15
4

3 5 12 14 16

7
AVL Tree Rotations
Single rotations:
 Rotation type:
10
2 13

1 11 15
4

3 5 12 14 16

7
AVL Tree Rotations
Double rotations:
 AVL balance restored.
10
4 13

2 11 15
5

7 12 14 16
1 3

 Now insert 6.
AVL Tree Rotations
Double rotations:
 AVL violation - rotate.
10
4 13

2 11 15
5

7 12 14 16
1 3

6
AVL Tree Rotations
Double rotations:
 Rotation type:
10
4 13

2 11 15
5

7 12 14 16
1 3

6
AVL Tree Rotations
Double rotations:
 AVL balance restored.
10
4 13

2 11 15
6

12 14 16
1 3 5 7

 Now insert 9 and 8.


AVL Tree Rotations
Double rotations:
 AVL violation - rotate.
10
4 13

2 11 15
6

12 14 16
1 3 5 7

8
AVL Tree Rotations
Double rotations:
 Rotation type:
10
4 13

2 11 15
6

12 14 16
1 3 5 7

8
AVL Tree Rotations
Final tree:
 Tree is almost perfectly balanced
10
4 13

2 11 15
6

12 14 16
1 3 5 8

7 9
Deletion

 Delete a node x as in ordinary binary search tree. Note


that the last node deleted is a leaf.
 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, perform an appropriate rotation at x.
There are 4 cases as in the case of insertion.
 For deletion, after we perform a rotation at x, we may
have to perform a rotation at some ancestor of x. Thus,
we must continue to trace the path until we reach the
root.
Deletion Example 1

20 20

10 35 15 35

5 15 25 40 10 18 25 40

18 30 38 45 30 38 45

50 50
Single Rotation
Delete 5, Node 10 is unbalanced
Cont’d

20 35

15 35 20 40

10 18 25 40 15 25 38 45

30 38 45 10 18 30 50

Continue to check parents 50


Single Rotation
Oops!! Node 20 is unbalanced!!

For deletion, after rotation, we need to continue tracing


upward to see if AVL-tree property is violated at other node.
Pros and Cons of AVL Trees

Arguments for AVL trees:


1. Search is O(log N) since AVL trees are always balanced.
2. Insertion and deletions are also O(logn)
3. The height balancing adds no more than a constant factor to the
speed of insertion.

Arguments against using AVL trees:


1. Difficult to program & debug; more space for balance factor.
2. Asymptotically faster but rebalancing costs time.
3. Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
Exercises for Class
 Insert 14, 17, 11, 7, 53, 4, 13, 12, 8 into an empty
AVL tree
 Remove 53, 11, 8

 Build an AVL tree with the following values:


15, 20, 24, 10, 13, 7, 30, 36, 25
 Remove 24 and 20 from the AVL tree.

You might also like