Download - Properties of AVL Tree
![Page 1: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/1.jpg)
![Page 2: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/2.jpg)
Properties of AVL Tree1. The height of an AVL tree with n nodes is O(log n)2. For every value of n, n 0, there exists an AVL tree3. An n-node AVL search tree can be searched in O(height) = O(log
n) time4. A new node can be inserted into an n-node AVL search tree so
that the result is an n+1 node AVL tree and insertion can be done in O(log n) time
5. A node can be deleted from an n-node AVL search tree, n>0, so that the result is an n-1 node AVL tree and deletion can be done in O(log n) time
![Page 3: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/3.jpg)
3
Balancing Operations
• We prefer to use balancing operations after each add or remove element operation
• Semantics of balancing operations– Right rotation– Left rotation– Rightleft rotation– Leftright rotation
![Page 4: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/4.jpg)
4
Balancing Operations• Semantics of Right Rotation
A. Make the left child of the root the new rootB. Make former root the right child of the new rootC. Make right child of the former left child of the former
root the new left child of the former root
13
157
105
3
7
135
103 15
InitialTree Step A Step B Step C
7
135
103 1510
7
135
3 15
![Page 5: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/5.jpg)
5
Balancing Operations• Semantics of Left Rotation
A. Make the right child of the root the new rootB. Make former root the left child of the new rootC. Make left child of the former right child of the former
root the new right child of the former root
5
103
7 13
15InitialTree
5
10
37
13
15
Step A
5
10
37
13
15
Step B
5
10
3 7
13
15
Step C
![Page 6: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/6.jpg)
6
Balancing Operations• Semantics of Rightleft Rotation
A. Right rotation around right child of rootB. Left rotation around root
5
133
10 15
7
InitialTree
5
103
7 13
15
After RightRotation
10
135
7 153
After LeftRotation
![Page 7: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/7.jpg)
7
Balancing Operations• Semantics of Leftright Rotation
A. Left rotation around left child of rootB. Right rotation around root
13
155
73
10
InitialTree
13
157
105
3
After LeftRotation
7
135
103 15
After RightRotation
![Page 8: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/8.jpg)
Balance Factor• AVL trees are normally represented using the linked
representation• To facilitate insertion and deletion, a balance factor (bf) is
associated with each node• The balance factor bf(x) of a node x is defined as
height(xleftChild) – height(xrightChild)• Balance factor of each node in an AVL tree must be –1, 0, or
1
![Page 9: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/9.jpg)
AVL Trees 9
Balanced and unbalanced BST
4
2 5
1 3
1
5
2
4
3
7
6
4
2 6
5 71 3
Is this “balanced”?
![Page 10: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/10.jpg)
AVL Trees 10
Perfect Balance
• Want a complete tree after every operation– tree is full except possibly in the lower right
• This is expensive– For example, insert 2 in the tree on the left and
then rebuild as a complete tree
Insert 2 &complete tree
6
4 9
81 5
5
2 8
6 91 4
![Page 11: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/11.jpg)
AVL Trees 11
AVL - Good but not Perfect Balance
• AVL trees are height-balanced binary search trees
• Balance factor of a node– height(left subtree) - height(right subtree)
• An AVL tree has balance factor calculated at every node– For every node, heights of left and right subtree
can differ by no more than 1– Store current heights in each node
![Page 12: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/12.jpg)
AVL Tree with Balance Factors
• Is this an AVL tree? • What is the balance factor for each node in this AVL tree?• Is this an AVL search tree?
-1
1
0
0 0
0
1
1
-1 0
-1
0
0
10
40
30 45
20 35
25
60
7
3 8
1 5
![Page 13: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/13.jpg)
Searching an AVL Search Trees
• What would be the search time complexity?– O(log n)
![Page 14: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/14.jpg)
Single and Double Rotations• Single rotations: the transformations done to correct LL and
RR imbalances• Double rotations: the transformations done to correct LR and
RL imbalances• The transformation to correct LR imbalance can be achieved
by an RR rotation followed by an LL rotation• The transformation to correct RL imbalance can be achieved
by an LL rotation followed by an RR rotation (do Exercise 15.13)
![Page 15: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/15.jpg)
Left RotationDefinition• In a binary search tree, pushing a node A down and to the left to
balance the tree. • A's right child replaces A, and the right child's left child becomes
A's right child.
Left Rotation
15
229
124
Animated rotation example: http://www.cs.queensu.ca/home/jstewart/applets/bst/bst-rotation.html
9
4 15
12 22
A
![Page 16: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/16.jpg)
Right RotationDefinition• In a binary search tree, pushing a node A down and to the
right to balance the tree.• A's left child replaces A, and the left child's right child
becomes A's left child.9
4 15
12 22
Right Rotation
15
229
124
A
![Page 17: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/17.jpg)
AVL Trees 17
Example of Insertions in an AVL Tree
0
0
0
20
10 30
25
1
350
50
20
10 30
25
1
355
40
0
0
0-1
-1
-1
Now Insert 45
![Page 18: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/18.jpg)
AVL Trees 18
Single rotation (outside case)
-2
0
-2
20
10 30
25
1
35-2
50
20
10 30
25
1
405
40
0
0
0
-1
-1
-1
45
Imbalance35 45
0 0
0
Now Insert 34
![Page 19: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/19.jpg)
AVL Trees 19
Double rotation (inside case)
-2
0
-2
20
10 30
25
1
40+1
50
20
10 35
30
1
405
45
0 0
0
-1
Imbalance
450
-1
Insertion of 34
35
34
0
0
1 25 340
![Page 20: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/20.jpg)
An Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3
Fig 1
3
2
Fig 2
3
2
1
Fig 3
2
1 3Fig 4
2
1 3
4Fig 5
2
1 34
5Fig 6
Single rotation
Single rotation
![Page 21: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/21.jpg)
2
1 453
Fig 7 6
2
1 453
Fig 8
4
2 561 3
Fig 9
4
2 561 3
7Fig 10
4
2 671 3
5 Fig 11
Single rotation
Single rotation
![Page 22: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/22.jpg)
4
2 671 3
5 16
Fig 12
4
2 671 3
5 16
15Fig 13
4
2 6151 3 5
167Fig 14
Double rotation
![Page 23: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/23.jpg)
5
3
1 4
Insert 3.5
AVL Tree
8
3.5
5
3
1 4
8
4
5
1
3
3.5After Rotation
x
y
A z
B
C
8
![Page 24: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/24.jpg)
5
4
2 7
151 3 6
1614
Fig 16
4
2 6151 3 5
167
14
Fig 15
Double rotation
![Page 25: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/25.jpg)
An Insertion
0
-11
0
-1
1
0
0 0
01
0
-1
1
0
00
no rotation required
0
0
#'s are balance factors
![Page 26: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/26.jpg)
Another Insertion
0
01
0
-1
1
0
0 0
01
0
-1
0
0
00
0
0
1
2
simple right rotation required
0
![Page 27: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/27.jpg)
Another Insertion
0
01
0
-1
0
0
00
0
0
0
0 0
-1
0
-10
0
1
0
-2
-1
-1
-1
simple left rotation required
0
![Page 28: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/28.jpg)
Another Insertion
0
-10
0
-1
1
0
00
0
1
-1
1
-2
double rotation needed a. right rotation around right subtree of the unbalanced subtree b. left rotation around root of the unbalanced subtree
![Page 29: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/29.jpg)
Rotation case 1
![Page 30: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/30.jpg)
Rotation case 2
![Page 31: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/31.jpg)
Rotation case 3
![Page 32: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/32.jpg)
Rotation case 4
![Page 33: Properties of AVL Tree](https://reader036.vdocument.in/reader036/viewer/2022062310/568161d9550346895dd1e12a/html5/thumbnails/33.jpg)
AVL Trees 33
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:4. Difficult to program & debug; more space for balance factor.5. Asymptotically faster but rebalancing costs time.6. Most large searches are done in database systems on disk and use other structures
(e.g. B-trees).
Pros and Cons of AVL Trees