You are on page 1of 36

AVL Trees and Heaps

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

AVL Trees
So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can however be done locally if only a portion of the tree is affected. An example of this is called AVL tree AVL tree was proposed by Adelson, Velskii and Landis

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

AVL Trees
An AVL tree is one in which the height of the left and right sub-trees of every node differ by at most one
For example consider the following tree There are two values in each node: one is the actual value of the node and the other is called balancing factor of the node.
8| +1

15| -1

19| 0

12| 0

The balancing factor of every node in an AVL tree is either 0, +1, or -1. For each node, P, the
balancing factor of P = height of right sub-tree of P height of left sub-tree of P

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

AVL Trees -- Continue


If a new node is inserted to an AVL tree or a node is deleted from an AVL tree, the balancing factor of some nodes changes. In order to make the new modified tree an AVL tree again we may have to rotate some nodes

Insertion or deletion may cause the balance factor of some nodes to be +2 or -2. These nodes have to be tracked and based on appropriate algorithms they should be properly rotated to right/left to make the tree a balanced AVL tree

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Inserting into an AVL Tree


Algorithm: Insert the new node, node N, into the tree the same way you insert N into a binary search tree

Follow the path from node N to the root. This is your insertion path
Insertion of node N may change the balancing factors of some nodes in the insertion path After inserting node N, start updating the balancing factors of the nodes in the insertion path until you reach the first node with balancing factor of + 2 or -2. If such a node does not exist in the insertion path, the tree is already balanced; otherwise mark that node, node P and go to the next step
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 5

R = P -> Right L = P -> Left If the new node that was just inserted, Node N, is in the right sub-tree of R Case 1 Left Rotate R around P else if Node N is in the left sub-tree of R C = R ->Left Right rotate C around R Case 2 Left rotate C around P else if Node N, is in the left sub-tree of L Right Rotate L around P Case 3 else if Node N is in the right sub-tree of L C = L ->Right Case 4 Left rotate C around L Right rotate C around P
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 6

. . . P

Suppose node P is the first node on the insertion path in which its balance factor is changed to +2 or -2

L
C C

Right rotate of L around P


A.R. Hadaegh Dr. Ahmad R. Hadaegh

Left rotate of C around L then, right rotate of C around P

Right rotate of C around R then, left rotate of C around P

Left rotate of R around P


Page 7

California State University San Marcos (CSUSM)

Example of Case 1:
15| 0 8|0 5|0 12 | 0 19| +1 23| 0

Original Tree

Step 1
15| ? 8|0 5|0 19| ?

Inserting the node 30 into the tree


23| ? 30| 0

12 | 0

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 8

15| ?
P 8|0 5|0 12 | 0 19| +2

Stop Here

R 23| +1 30| 0

Following the insertion path to find node P with balancing factor +2 or -2

Step 3
15| 0 R 8|0 P 5|0 12 | 0 19| 0 30| 0 23| 0

R is rotated around P to balance the tree (case 1)

Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 9

Example of Case 2:
15| 0 8|0 5|0 12 | 0 19| +1 23| 0

Original Tree

Step 1

15| ? 8|0 5|0 19| ?

Inserting the node 21 into the tree


23| ?

12 | 0
21| 0

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 10

15| ? P 8|0 5|0 12 | 0 21 | 0 19| +2

Stop Here

R 23| -1 C

Following the insertion path to find node P with balancing factor +2 or -2

Step 3
15| ? 8|0 5|0

P 19| ? C 12 | 0
21| ? R 23| ? 5|0 8|0

15| 0
C 21| 0

P
12 | 0 19| 0

R 23| 0

Step 4 Making a double rotations to balance the tree (case 2)


A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 11

Example of Case 3:
15| -1 8|0 5|0 12 | 0 19| 0

Original Tree

Step 1
15| ? 8|? 5|? 2| 0 19| 0

12 | 0

Inserting the node 2 into the tree

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 12

Step 3
L 8 | -1 5 | -1

P 15| -2

Stop Here

19| 0
12 | 0

Following the insertion path to find node P with balancing factor +2 or -2

2| 0
L 8| 0 P 5 | -1 2|0 12 | 0 15| 0 19| 0

L is rotated around P to balance the tree (case 3)

Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 13

Example of Case 4:
15| -1 8 | -1 5|0 19| 0

Original Tree

Step 1
15| ? 8|? 5|? 19| 0

Inserting the node 6 into the tree

6| 0

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 14

Stop Here

15| ? P 8 | -2 19| 0

L
5 | +1

C
6|0

Following the insertion path to find node P with balancing factor +2 or -2

Step 3 Step 4
P 8|? C 6|? L 5|? 15| ? 15| -1

19| 0
L 5|0

C 6|0 P 19| 0

8|0

Making a double rotations to balance the tree (case 4)


A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 15

Inserting a series of numbers into a AVL Tree


We want to insert numbers 2, 8, 6, 10, 15, 4, 3, 20, 13, 18 Insert 2
2|0

No rotation is required because there is no node with balance factor of +2 or -2

2 | +1

Insert 8
8|0

No rotation is required because there is no node with balance factor of +2 or -2


Page 16

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Insert 6
P
2 | +2 R 8 | -1 6|0 C

Double Rotation is required


P 2 | +2 C 6|0

C
6 | +1 R 8|0

P 2|0 8|0

6 | +1

Insert 10
2|0 8 | +1 10 | 0
A.R. Hadaegh Dr. Ahmad R. Hadaegh

No rotation is required because there is no node with balance factor of +2 or -2

California State University San Marcos (CSUSM)

Page 17

6|?

Insert 15
2|0 8 | +2

6 | +1 P 2|0 P 8|0 15 |0 10 | 0 R

R 10 |+1 15 | 0

Single rotation is required

Insert 4
6|0

2 | +1 4|0

10 | 0 15 |0

No rotation is required because there is no node with balance factor of +2 or -2

8|0

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 18

Insert 3
6|?
6|? P P 2 | +2 R 4 | -1 C 8|0 15 |0 10 | 0

2 | +2
C 3 | +1 R 4|0

10 | 0 15 |0

8|0

3|0
6|0 C 3|0 P 2|0
A.R. Hadaegh Dr. Ahmad R. Hadaegh

10 | 0

R
4|0

8|0

15 |0
Page 19

California State University San Marcos (CSUSM)

Insert 20
3|0 4|0

6 | +1

10|+1
15|+1

No rotation is required because there is no node with balance factor of +2 or -2

2|0

8|0

20 |0

Insert 13
3|0 4|0

6 | +1

10|+1 15|0 13 | 0 20 |0

No rotation is required because there is no node with balance factor of +2 or -2

2|0

8|0

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 20

6 | +1

Insert 18
3|0

P
10|+2

L
2|0 4|0 8|0 15|+1 13 | 0

R
20 |-1

18 | 0

Single rotation is required


6 | +1

3|0 2|0 4|0 10 | 0 8 |-1

15|0 20|-1 13 | 0 18 | 0

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 21

Deleting From an AVL Tree


This can be more time consuming than insertion. First we need to apply a delete method to get rid of the node We can use the delete method that finds either the successor or predecessor node to replace a node that has both left and right children After the node is deleted, the balance factors are updated from the parent of the deleted node up to the root. This is the delete path For each node on the delete path whose balance factor becomes +2 or -2, a single or a double rotations has to be performed to restore the balance of the tree IMPORTANT NOTE: The balancing of the tree DOES NOT STOP after the first node P with balancing factor +2 or -2 found in the delete path. You still need to continue searching for other nodes with balancing factor +2 or -2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 22

Delete Algorithm -- AVL Tree


1. Start from the parent of the deleted node and move up the tree (delete path) 2. If you find a node with balance factor +2 or -2 then stop and do the balancing as follows 3. Mark the node P 4. If the deleted node is in the left sub-tree of P Q = P -> Right If balance factor of Q is +1 or 0 Case 1 rotate Q around P else // the balance factor is -1 R = Q -> Left rotate R around Q Case 2 rotate R around P endif 5. else if the
A.R. Hadaegh Dr. Ahmad R. Hadaegh

. <<SEE

next Slide>>
Page 23

California State University San Marcos (CSUSM)

Delete Algorithm -- AVL Tree Cont.


5. else if the deleted node is in the right sub-tree of P Q = P -> Left if balance factor of Q is -1 or 0 Case 3 rotate Q around P else // the balance factor is +1 R = Q -> Right rotate R around Q Case 4 rotate R around P end if 6. Continue moving up the tree on the delete path. If you find another node with balancing factor +2 or -2 go to step 3; otherwise, you are done

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 24

Example of Case 1:
10| +1 5|0 3|0 8|0 20| +1 15| 0 25| 0 23| 0 10| +1 5|0 3|0 8|0 20| +1 15| 0 25| 0 23| 0 30| 0 30| 0

Original Tree

Step 1

Delete 10 Delete method: Find successor of 10 which is 15

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 25

15| ? 5|0 3|0 8|0 20| ?

Swap 10 and 15
25| 0

10| 0

And
30| 0

Step 3
15| ? 5|0 3|0 8|0

23| 0

Delete 10
Start from node 20 (the parent of deleted node) and find the first node with balancing factor +2 or -2
Page 26

P
20| +2 25| 0 23| 0 30| 0

Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

15| ? 5|0 3|0 8|0

20| +2
Q

Because the deleted node was in the left side of P, we have Q = P -> Right

25| 0
23| 0 30| 0

Step 5
15| +1
5|0 3|0 8|0

Note: Balance factor of Q is 0

Q 25| -1 P 20|+1

30| 0
23| 0

Because the balancing factor of Q was 0, we rotate Q around P

Step 6
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 27

Example of Case 2:
10| +1 5|0 3|0 8|0 20| +1 15| 0 25| -1 23| 0 10| +1 5|0 3|0 8|0 20| +1 15| 0 25| -1 23| 0

Original Tree

Step 1

Delete 10 Delete method: Find successor of 10 which is 15

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 28

15| ? 5|0 3|0 8|0 20| ?

Swap 10 and 15
25| -1

10| 0

And

Step 3
15| ? 5|0 3|0 8|0

23| 0

Delete 10
Start from node 20 (the parent of deleted node) and find the first node with balancing factor +2 or -2
Page 29

P
20| +2 25| -1 23| 0

Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Step 5
5|0 3|0

15| ?

P 20| +2 Q

Because the deleted node was in the left side of P, we have Q = P -> Right Note: Balance factor of Q is -1, so R = Q -> Left
25| -1

8|0 R

23| 0

Step 6
5|0 3|0

15| ?

P 20| ? R 5|0 23| ? Q 25| ? 3|0

15| 0

R 23| 0 Q 20| 0 25| 0

8|0

8|0

Because the balancing factor of Q was -1, we do two rotations


A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 30

Example of Case 3:
10| -1 5 | -1 3|0 8|0 20| 0 15| 0 25| 0

Original Tree

1|0

4| 0 10 | -1 5 | -1 20| 0 8|0 15| 0 25| 0

Step 1 Delete 10 Delete method: Find predecessor 10 which is 8

3|0

1|0

4| 0

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 31

8| ? 5|? 3|0 1|0 4|0 10 | 0 20| 0

Swap 10 and 8
25| 0

15| 0

and

Step 3
8| ?

Delete 10
Start from node 5 (the parent of deleted node) and find the first node with balancing factor +2 or -2
Page 32

P
5 | -2 3|0 1|0 4|0

20| 0
15| 0

25| 0

Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

8| ? P 5 | -2 Q 3|0 1|0 15| 0 25| 0 20| 0

Because the deleted node was in the right side of P, we have Q = P -> Left Note: Balance factor of Q is 0

4|0

Step 5
8| -1

Q 3 | +1 1|0 P 5 | -1 4|0

20| 0 15| 0 25| 0

Because the balancing factor of Q was 0, we rotate Q around P

Step 6
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 33

Example of Case 4:
10| -1 5 | -1 3 | +1 4|0 10| ? 5|? 3 | +1 8|0 20| 0 15| 0 25| 0 8|0 20| 0 15| 0 25| 0

Original Tree

Step 1 Delete 8 Delete method: Because node 8 is a leaf, it can be simply deleted

4|0

Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 34

10| ?

P 5 | -2 3 | +1
20| 0 15| 0 25| 0

4|0

Step 3
10| ?

Start from node 5 (the parent of deleted node) and find the first node with balancing factor +2 or -2
Because the deleted node was in the right side of P, we have Q = P -> Left

P
5 | -2 20| 0 15| 0 R 4|0 25| 0

Q 3 | +1

Note: Balance factor of Q is +1, so

Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh

R = Q -> Right
Page 35

California State University San Marcos (CSUSM)

10| ? P 5|? R 4|? Q

20| 0 15| 0 25| 0

3|?

10| 0

R 4|0

20| 0 P 5|0 15| 0 25| 0

Step 5

Q 3|0

Because the balancing factor of Q was +1, we do two rotations


A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 36

You might also like