35
AVL Tree The AVL tree is supposed to be balanced if all nodes of the tree have balance factor of -1, 0 and 1. The balance factor of the node depends on the difference of the height of left sub-tree and right sub-tree. Balance factor = Right sub-tree – Left sub-tree When the balance factor comes out to be different from -1, 0 and 1 then the tree is said to be unbalanced. In this situation rotations are performed to balance the tree. There are following type of rotations: 1. LL rotation (Right heavy) 2. RR rotation (Left heavy) 3. LR rotation or Double left (Right sub-tree is left heavy) RR rotations followed by LL rotation 4. RL rotation or Double Right (Left sub-tree is right heavy) LL rotations followed by RR rotation Why -1 is considered?? In AVL every node keeps its height. The height of the tree is H = max (hl, hr) + 1. Hl = height of left sub-tree. Hr = height of right sub-tree. The height of the tree with a single node is 0. So, the formula of height H should satisfy it. So, hl and hr are supposed to carry -1 in this type of scenario. This height needs to be updated with each insertion and deletion. Question: Insert in AVL Tree: 1. A Z B Y C X 2. A Z B Y C X D W E V F 3. A B C D E F G H I J K 4. A V L T R E I S O K

Avl excercise

Embed Size (px)

Citation preview

Page 1: Avl excercise

AVL Tree

The AVL tree is supposed to be balanced if all nodes of the tree have balance factor of -1, 0

and 1.

The balance factor of the node depends on the difference of the height of left sub-tree and

right sub-tree. Balance factor = Right sub-tree – Left sub-tree

When the balance factor comes out to be different from -1, 0 and 1 then the tree is said to

be unbalanced. In this situation rotations are performed to balance the tree. There are

following type of rotations:

1. LL rotation (Right heavy)

2. RR rotation (Left heavy)

3. LR rotation or Double left (Right sub-tree is left heavy)

RR rotations followed by LL rotation

4. RL rotation or Double Right (Left sub-tree is right heavy)

LL rotations followed by RR rotation

Why -1 is considered??

In AVL every node keeps its height. The height of the tree is

H = max (hl, hr) + 1.

Hl = height of left sub-tree.

Hr = height of right sub-tree.

The height of the tree with a single node is 0. So, the formula of height H should satisfy it.

So, hl and hr are supposed to carry -1 in this type of scenario.

This height needs to be updated with each insertion and deletion.

Question: Insert in AVL Tree:

1. A Z B Y C X

2. A Z B Y C X D W E V F

3. A B C D E F G H I J K

4. A V L T R E I S O K

Page 2: Avl excercise

A Z B Y C X

Insert A

0

Insert Z

1

0

Insert B 2

-1

(RR rotation)

0

Tree is unbalance because of insertion of left child of right sub-tree i.e. right sub-tree is left heavy so

LR rotation is needed, which is RR rotation followed by LL rotation.

A

A

Z

A

Z

B

Page 3: Avl excercise

2

(LL rotation)

1

0

Now the tree needs LL rotation which results in following tree:

0

0 0

A

Z

B

Z

B

A

Page 4: Avl excercise

Insert Y

1

0 -1

0

2

Insert C

0 -2

(RR rotation)

-1

0

Z

B

A

Y

Z

B

A

Y

C

Page 5: Avl excercise

Inserting C makes the tree left heavy so RR rotation is needed which results in following tree

structure:

1

0 0

0 0

Insert X

2

0 -1

(RR rotation)

-1 0

0

Z

B

A Y

C

Z

B

A Y

C

X

Page 6: Avl excercise

Tree is unbalance because of insertion of left child of right sub-tree i.e. right sub-tree is left

heavy so LR rotation is needed, which is RR rotation followed by LL rotation.

2

(LL rotation)

0 1

0 1

0

0

-1 0

0 0 0

Y

B

A C

X

Z

Z

C

B Y

X A

Page 7: Avl excercise

A Z B Y C X D W E V F

Till A Z B Y C X, the procedure of node insertion and tree balancing will be carried out like

previous example.

Afterwards, that is for D W E V F, it is explained below.

Insert D

1

-1 -1

0 -1 0

0

Z

C

B Y

X A

D

Page 8: Avl excercise

Insert W

2

-1 -2

0 -1 0

1

(LL rotation)

0

Insertion of W makes the left sub-tree of X is right heavy. So, it needs RL rotation, which is LL

Rotation followed by RR Rotation.

Above structure is explaining the LL Rotation.

Z

C

B Y

X A

D

W

Page 9: Avl excercise

Here is the application of RR Rotation on the tree structure generated because of LL

Rotation on the unbalanced node.

2

-1 -2

0 -2 0

(RR Rotation)

-1

0

Z

C

B Y

X A

W

D

Page 10: Avl excercise

Here is the tree after the RR Rotation.

1

-1 -1

0 0 0

0 0

Z

C

B Y

W A

D X

Page 11: Avl excercise

Insert E

2

-1 -2 (Left heavy so RR rotation)

0 -1 0

1 0

0

Z

C

B Y

W A

D X

E

Page 12: Avl excercise

Tree after RR Rotation

1

-1 0

0 1 0

0

0 0

Y

C

B W

D A

E X Z

Page 13: Avl excercise

Insert V

2

-1 -1

0 2

(Right heavy so LL rotation) 1 0 0

0

Y

C

B W

D A

E X Z

V

Page 14: Avl excercise

After LL Rotation

1

-1 0

0 0 0

0

0 0 0

Y

C

B W

E A

V X Z

D

Page 15: Avl excercise

Insert F

2

-1 -1

(RR rotation)

0 1 0

-1

0 0 0

0

Insertion of F makes C unbalanced because its Right sub-tree becomes left heavy, so LR

rotation is needed, which is RR rotation followed by LL rotation.

Y

C

B W

E A

V X Z

D

F

Page 16: Avl excercise

RR Rotation is shown in earlier structure.

Here is the explanation of LL Rotation.

2

(LL Rotation)

-1 1

0 0 0

-1 0

0

0 0

W

C

B E

D A

V Y

X Z F

Page 17: Avl excercise

The final balanced tree structure is given in following diagram.

0

0

-1

-1 0

-1 0

0

0 0 0

E

C W

D B

V Y

X Z F A

Page 18: Avl excercise

A B C D E F G H I J K

Insert A

0

Insert B

1

0

Insert C

2

(Right heavy so LL Rotation)

1

0

A

A

B

A

B

C

Page 19: Avl excercise

Here is the tree structure after LL rotation.

0

0 0

Insert D

1

0 1

0

B

C A

B

C A

D

Page 20: Avl excercise

Insert E

2

0 2

(Right heavy so LL Rotation)

1

0

1

0 0

0

0

B

C A

D

E

B

D A

E C

Page 21: Avl excercise

Insert F

2

(Right heavy so LL Rotation)

0 1

1

0

0

The tree needs to go through LL Rotation for balancing.

Following is the tree structure that gets generated because of this rotation.

B

D A

E C

F

Page 22: Avl excercise

0

0 1

0

0

0

Insert G

1

0 2

(Right heavy so LL Rotation) 1

0

0

0

D

E B

F C A

D

E B

F C A

G

Page 23: Avl excercise

0

0 0

0 0 0 0

Insert H

1

0 1

0 0 0 1

0

D

F B

G C A E

D

F B

G C A E

H

Page 24: Avl excercise

Insert I

2

0 2

0 0 0 2

(Right heavy so LL Rotation)

1

0

D

F B

G C A E

H

I

Page 25: Avl excercise

Following is the tree that is obtained in order to balance the tree with the use of LL

Rotation:

1

0 1

0 0 0 0

0 0

D

F B

H C A E

I G

Page 26: Avl excercise

Insert J

2

0 2

(Right heavy so LL Rotation)

0 0 0 1

0 1

0

D

F B

H C A E

I G

J

Page 27: Avl excercise

1

0 0

0 0 0 1

0 0 0

D

H B

I C A F

J G E

Page 28: Avl excercise

Insert K

2

0 1

0 0 0 2

(Right heavy so LL Rotation)

0 0 1

0

D

H B

I C A F

J G E

K

Page 29: Avl excercise

Here is the final tree after the insertion of each desired nodes.

1

0 0

0

0 0 0

0

0 0

D

H B

J

C A F

K

G E

I

Page 30: Avl excercise

A V L T R E I S O K

Insert A

0

Insert V

1

0

Insert L

2

(Right sob-tree is left heavy, So LR Rotation

i.e. RR Rotation followed by LL rotation)

-1

(RR Rotation)

0

A

A

V

A

V

L

Page 31: Avl excercise

2

(LL Rotation)

1

0

0 0

Insert T

1

0 -1

0

A

L

V

L

V A

L

V A

T

Page 32: Avl excercise

Insert R

2

0 -2

(Left heavy so RR Rotation)

-1

0

1

0 0

0 0

L

V A

T

R

L

T A

R V

Page 33: Avl excercise

Insert E

0

1 0

0

0 0

Insert I

-1

2 0

1 0

0

Right heavy, so LL Rotation

L

T A

R V E

L

T A

R V E

I

Page 34: Avl excercise

0

0 0

0 0 0 0

Insert S

1

0 -1

0 0 1 0

0

L

T E

R V I

A

L

T E

R V I A

S

Page 35: Avl excercise

Insert O 1

0 -1

0 0

0 0

0 0

0

Insert K

1 -1

0 1 0 0

0 0 0

L

T

E

R V

I A

S O

L

T E

R V

I A

S O K