TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Avl trees final
-
Upload
prakash-ranjan-singh -
Category
Education
-
view
337 -
download
2
Transcript of Avl trees final
AVL Trees
2
Binary Search Tree - Best Time
• All BST operations are O(h), where h is tree height
• minimum h is O(log n) for a binary tree with N nodes– What is the best case tree? O(log n)– What is the worst case tree? O(n)
• So, best case running time of BST operations is O(log N)
3
Binary Search Tree - Worst Time
• Worst case running time is O(N) – What happens when you Insert elements in
ascending order?• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
– Problem: Lack of “balance”: • compare depths of left and right subtree
– Unbalanced degenerate tree
4
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”?
5
Approaches to balancing trees
• Don't balance– May end up with some nodes very deep
• Strict balance– The tree must always be balanced perfectly
• Pretty good balance– Only allow a little out of balance
6
Balancing Binary Search Trees
• Many algorithms exist for keeping binary search trees balanced– Adelson-Velskii and Landis (AVL) trees (height-
balanced trees) – Splay trees and other self-adjusting trees– B-trees and other multiway search trees
7
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
8
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 balance in each node
9
Implementation
balance (1,0,-1)key
rightleft
No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations
Once you have performed a rotation (single or double) you won’t need to go back up the tree
5
3 8
1 4 10
5
3
1 4
AVL Tree
Not AVL Tree
11
Node Heights
1
00
2
0
6
4 9
81 5
1
height of node = hbalance factor = hleft-hright
empty height = -1
0
0
height=2 BF=1-0=1
0
6
4 9
1 5
1
Tree A (AVL) Tree B (AVL)
12
Node Heights after Insert 7
2
10
3
0
6
4 9
81 5
1
height of node = hbalance factor = hleft-hright
empty height = -1
1
0
2
0
6
4 9
1 5
1
07
07
balance factor 1-(-1) = 2
-1
Tree A (AVL) Tree B (not AVL)
13
Insert and Rotation in AVL Trees
• Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to
root node have possibly changed in height– So after the Insert, go back up to the root node by
node, updating heights– If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
Inserting a Node in an AVL Tree• During insertion, the new node is inserted as the leaf node, so it will
always have balance factor equal to zero. • The nodes whose balance factors will change are those which lie on
the path between the root of the tree and the newly inserted node. • The possible changes which may take place in any node on the path
are as follows: Initially the node was either left or right heavy and after insertion has
become balanced. Initially the node was balanced and after insertion has become either
left or right heavy. Initially the node was heavy (either left or right) and the new node has
been inserted in the heavy sub-tree thereby creating an unbalanced sub-tree. Such a node is said to be a critical node.
15
Single Rotation in an AVL Tree
2
10
2
0
6
4 9
81 5
1
07
0
1
0
2
0
6
4
9
8
1 5
1
07
5
3
1 4
Insert 0.8
AVL Tree
8
0.8
5
3
1 4
8 Z
3
51
0.84 8
After Rotation
17
Let the node that needs rebalancing be .
There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of .
The rebalancing is performed through four separate rotation algorithms.
Insertions in AVL Trees
Rotations to Balance AVL Trees• To perform rotation, our first work is to find the critical node. Critical node is
the nearest ancestor node on the path from the root to the inserted node whose balance factor is neither -1, 0 nor 1.
• The second task is to determine which type of rotation has to be done. • There are four types of rebalancing rotations and their application depends on
the position of the inserted node with reference to the critical node. LL rotation: the new node is inserted in the left sub-tree of the left sub-tree of
the critical node RR rotation: the new node is inserted in the right sub-tree of the right sub-tree
of the critical node LR rotation: the new node is inserted in the right sub-tree of the left sub-tree
of the critical node RL rotation: the new node is inserted in the left sub-tree of the right sub-tree
of the critical node
19
j
k
X YZ
Consider a validAVL subtree
AVL Insertion: Outside Case
h
h h
20
j
k
XY
Z
Inserting into Xdestroys the AVL property at node j
AVL Insertion: Outside Case
h
h+1 h
21
j
k
XY
Z
Do a “right rotation”
AVL Insertion: Outside Case
h
h+1 h
22
j
k
XY
Z
Do a “right rotation”
Single right rotation
h
h+1 h
23
jk
X Y Z
“Right rotation” done!(“Left rotation” is mirror symmetric)
Outside Case Completed
AVL property has been restored!
h
h+1
h
24
j
k
X YZ
AVL Insertion: Inside Case Consider a validAVL subtree
h
hh
25
Inserting into Y destroys theAVL propertyat node j
j
k
XY
Z
AVL Insertion: Inside Case
Does “right rotation”restore balance?
h
h+1h
26
jk
X
YZ
“Right rotation”does not restorebalance… now k isout of balance
AVL Insertion: Inside Case
hh+1
h
27
Consider the structureof subtree Y… j
k
XY
Z
AVL Insertion: Inside Case
h
h+1h
28
j
k
XV
Z
W
i
Y = node i andsubtrees V and W
AVL Insertion: Inside Case
h
h+1h
h or h-1
29
j
k
XV
Z
W
i
AVL Insertion: Inside CaseWe will do a left-right “double rotation” . . .
30
j
k
X V
ZW
i
Double rotation : first rotationleft rotation complete
31
j
k
X V
ZW
i
Double rotation : second rotation
Now do a right rotation
32
jk
X V ZW
i
Double rotation : second rotation
right rotation complete
Balance has been restored
hh h or h-1
5
3
1 4
Insert 3.5
AVL Tree
8
3.5
5
3
1 4
8
4
5
0.8
3
3.5
After Rotation
8
34
Insertion in AVL Trees
• Insert at the leaf (as for all BST)– only nodes on the path from insertion point to
root node have possibly changed in height– So after the Insert, go back up to the root node by
node, updating heights– If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
35
Example of Insertions in an AVL Tree
1
0
220
10 30
25
0
350
Insert 5, 40
36
Example of Insertions in an AVL Tree
1
0
220
10 30
25
1
350
50
20
10 30
25
1
355
40
0
0
0 1
2
3
Now Insert 45
37
Single rotation (outside case)
2
0
320
10 30
25
1
352
50
20
10 30
25
1
405
40
0
0
0
1
2
3
45
Imbalance35 45
0 0
1
Now Insert 34
38
Double rotation (inside case)
3
0
320
10 30
25
1
402
50
20
10 35
30
1
405
45
0 1
2
3
Imbalance
450
1
Insertion of 34
35
34
0
0
1 25 340
Extended ExampleInsert 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 3
4
5Fig 6
2
1 4
53
Fig 7 6
2
1 4
53
Fig 8
4
2 5
61 3
Fig 9
4
2 5
61 3
7Fig 104
2 6
71 3
5 Fig 11
4
2 6
71 3
5 16
Fig 12
4
2 6
71 3
5 16
15Fig 13
4
2 6
151 3 516
7Fig 14
5
4
2 7
151 3 6
1614
Fig 16
4
2 6
151 3 516
7
14
Fig 15
Deletions can be done with similar rotations
43
AVL Tree Deletion
• Similar but more complex than insertion– Rotations and double rotations needed to
rebalance– Imbalance may propagate upward so that many
rotations may be needed.
44
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).7. May be OK to have O(N) for a single operation if total run time for
many consecutive operations is fast (e.g. Splay trees).
Pros and Cons of AVL Trees
Deleting a Node from an AVL Tree• Deletion of a node in an AVL tree is similar to that of binary search trees. • But deletion may disturb the AVLness of the tree, so to re-balance the
AVL tree we need to perform rotations. • There are two classes of rotation that can be performed on an AVL tree
after deleting a given node: R rotation and L rotation.• If the node to be deleted is present in the left sub-tree of the critical
node, then L rotation is applied else if node is in the right sub-tree, R rotation is performed.
• Further there are three categories of L and R rotations. The variations of L rotation are: L-1, L0 and L1 rotation. Correspondingly for R rotation, there are R0, R-1 and R1 rotations.
Deleting a Node from an AVL Tree• R0 Rotation Let B be the root of the left or right sub-tree of A (critical node). R0 rotation is applied if the balance factor of B is 0. Consider the AVL tree given below and delete 72 from it.
45
63
36
27 3
972
-1
1
1-1
0
18
0
40
0
0
45
6336
27 39
0
2
1
-1
0
18 400 0
36
4527
1839
1
-1
0-1
1
40
63
0
0
Deleting a Node from an AVL Tree
• R1 Rotation
Let B be the root of the left or right sub-tree of the critical node.
R1 rotation is applied if the balance factor of B is 1.
Consider the AVL tree given below and delete 72 from it.
45
63
36
27 3
972
1
1
10
1
18
00
45
6336
27 39
0
2
1
0
1
18
0
36
45
27
18
39
0
0
0 0
1
63
0
Deleting a Node from an AVL Tree
• R-1Rotation
Let B be the root of the left or right sub-tree of the critical node.
R-1 rotation is applied if the balance factor of B is -1.
Consider the AVL tree given below and delete 72 from it.
45
63
36
27 3
9
-1
1
0 0
-1
37
41
0 0
72
0
45
63
36
27 3
9
0
2
0 0
-1
37
41
0 0
39
45
36
27
37
41
1
1
1 0
1
0
63
0