AVL Trees: Balanced Binary Search Trees, Study notes of Data Structures and Algorithms

An overview of avl trees, a self-balancing binary search tree. Avl trees maintain a balance property where the height of the left and right subtrees of any node can differ by at most 1. The concept of avl trees, their advantages, and the methods to maintain balance through single and double rotations.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data and File Structure
Data and File Structure
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download AVL Trees: Balanced Binary Search Trees and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data and File Structure

Data and File Structure

Rotations

Rotations

Rotations

Rotations

AVL TREES

AVL TREES

AVL Trees

AVL Trees

Balanced Trees

Balanced Trees

: After insert and delete operations

: After insert and delete operations

we “fix” the tree to keep it (almost) balanced.

we “fix” the tree to keep it (almost) balanced.

AVL Tree:

AVL Tree:

A binary search tree with the following

A binary search tree with the following

additional balance property: For any node in the

additional balance property: For any node in the

tree, the height of the left and right subtrees can

tree, the height of the left and right subtrees can

differ by 1 at most.

differ by 1 at most.

Note that we require this balance property for

Note that we require this balance property for

every node, not just the root.

every node, not just the root.

Example

Example

An AVL tree 1 Not an AVL tree

(look at node 8, 12)

How to maintain balance

How to maintain balance

General rule: after an insert or delete operation,

General rule: after an insert or delete operation,

we fix all nodes that got unbalanced.

we fix all nodes that got unbalanced.

Since the height of any subtree has changed by at

Since the height of any subtree has changed by at

most 1, if a node is not balanced this means that

most 1, if a node is not balanced this means that

one son has a height larger by exactly two than the

one son has a height larger by exactly two than the

other son.

other son.

Next we show the four possible cases that cause a

Next we show the four possible cases that cause a

height difference of 2. In all figures, marked nodes

height difference of 2. In all figures, marked nodes

are unbalanced nodes.

are unbalanced nodes.

Example: Double rotation

Example: Double rotation

Insert 3.

5

3

1

4

8

x

y

A z

B
C

1

3

8

After Rotation

The rotation takes

The rotation takes O

O

time. Notice that the new tree is a legal

time. Notice that the new tree is a legal

search tree.

search tree.

For insert - it must be the case that subtree

For insert - it must be the case that subtree A

A

had been increased,

had been increased,

so after the rotation, the tree has height as before the insert.

so after the rotation, the tree has height as before the insert.

For delete, it must be the case that

For delete, it must be the case that C

C

had been decreased, so after

had been decreased, so after

the rotation, the tree has height shorter by 1.

the rotation, the tree has height shorter by 1.

Single Rotation - Fixing case 1

Single Rotation - Fixing case 1

k

2

k

1

A
B
C

right rotation

k

2

k

1

A B C

Single Rotation for Case 4

Single Rotation for Case 4

k

1

k

2

C
B
A

left rotation

k

1

k

2

A B C

Example (caused by deletion)

Example (caused by deletion)

Deleting X and performing a single rotation:

For the rotation, k

1

is node A, and k

2

is node B. We

make k

2

the root, and k

1

its left son.

Notice that the tree height has changed after the

rotation (since it was a delete operation).

A
B
C
X
B
A C
A
B
C

Example (caused by insertion)

Example (caused by insertion)

k 3 k 1 D A B k 2 Left rotation on 4 k 1

k 3 D A B k 2

Right rotation on 8

k k 3 1 B D k 2 A Note that above is a good friend of case 1

Double Rotation to fix case 2

Double Rotation to fix case 2

After insertion - original height (of the root) stays the same.

After insertion - original height (of the root) stays the same.

k

3

k

1

A
D

k

2

B C

k

3

k

1

A D

k

2

B C

k

3

k

1

A
D

k

2

B

C

left rotation

on k

1

right rotation

on k

3