Professional Documents
Culture Documents
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
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
Page
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
Page
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
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| ?
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
Step 3
15| 0 R 8|0 P 5|0 12 | 0 19| 0 30| 0 23| 0
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
12 | 0
21| 0
Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 10
Stop Here
R 23| -1 C
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
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
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
2| 0
L 8| 0 P 5 | -1 2|0 12 | 0 15| 0 19| 0
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
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
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
2 | +1
Insert 8
8|0
Insert 6
P
2 | +2 R 8 | -1 6|0 C
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
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
Insert 4
6|0
2 | +1 4|0
10 | 0 15 |0
8|0
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
Insert 20
3|0 4|0
6 | +1
10|+1
15|+1
2|0
8|0
20 |0
Insert 13
3|0 4|0
6 | +1
10|+1 15|0 13 | 0 20 |0
2|0
8|0
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
15|0 20|-1 13 | 0 18 | 0
Page 21
. <<SEE
next Slide>>
Page 23
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
Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 25
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
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
Q 25| -1 P 20|+1
30| 0
23| 0
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
Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 28
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
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| ?
15| 0
8|0
8|0
Example of Case 3:
10| -1 5 | -1 3|0 8|0 20| 0 15| 0 25| 0
Original Tree
1|0
3|0
1|0
4| 0
Step 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 31
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
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
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
Step 4
A.R. Hadaegh Dr. Ahmad R. Hadaegh
R = Q -> Right
Page 35
3|?
10| 0
R 4|0
Step 5
Q 3|0