Transcript AVL Trees

AVL Trees: Balanced BST
Binary Search Trees Performance
Height Balanced Trees
Rotation
AVL: insert, delete
D. Christozov
COS 221 Intro to CS II
AVL Trees
1
Binary Search Trees: Performance
The number of operations in function ‘search’ is equal to the
length of the path from the root to the leaf.
Therefore the worst case of search for (unbalanced) BST is
O(n)
The worst case of balanced BST is
O(log2n)
D. Christozov
COS 221 Intro to CS II
AVL Trees
2
Height-balanced trees
n-1
n
A height-balanced binary tree has the property that for
each node the difference in the heights of the right and
left child is no larger than one.
Full and Complete binary tree are height-balanced tree.
D. Christozov
COS 221 Intro to CS II
AVL Trees
3
Height-balanced trees
Theorem: The paths in height-balanced binary trees have logarithmic length.
1. The largest number of nodes in a binary tree with n levels (of depth n) is 2n-1.
2. Let us denote with Mn the minimal number of nodes in height-balanced tree
of depth n.
3. M0 = 1M1 = 2Mn+1 = Mn-1 + Mn + 1
4. Similar to Fibonacci numbers: fn+1 = fn-1 + fn and therefore we can
approximate Mn
5. and n ≈ 1.44 log2 Mn
D. Christozov
COS 221 Intro to CS II
AVL Trees
4
Rotation
Algorithms to maintain the Height-Balance Property
in a Binary Search Tree is based on Rotation
Before Rotation:
Let only for the node A the height-balanced tree
property is violated and its depth is n+1.
Let the right child B is heavier – its depth is n.
The depth of X is n-2; and the depth of Y and Z
are either n-1 or n-2.
A
B
X
Y
After Rotation:
We can assume that rotation to left will restore
the height-balance property of the tree.
Z
B
Left rotation is performed with two assignments:
A->right(B->left);
A
Z
B->left(&A)
Y
X
D. Christozov
COS 221 Intro to CS II
AVL Trees
5
Rotation
A
Abf = 2
Bbf = {0, 1, -1}
B
A
B
Z
X
Y
Z
Heights before rotation
Left rotation
Y
X
Heights after rotation
Bbf 0
Bbf < 0
A
n-1
n
n
B
n
n+1
n-2
n-2
X
n-2
n-2
Y
n-2
n-1
Y
n-2
n-1
Z
n-1
n-2
Z
n-1
n-2
Bbf 0
Bbf < 0
A
n+1
n+1
B
n
X
D. Christozov
COS 221 Intro to CS II
AVL Trees
Bbf =
(n-2) - n =-2
6
Adelson-Velskii and Landis  AVL tree:
1. The AVL tree is a Binary Search Tree
2. The AVL tree is a Height-Balanced Tree
AVL Tree
AVL tree node:
The bf = {-1, 0, 1}
for every node in the tree
bf – balance
factor
If bf > 1 and the bf of the root of the
right-sub-tree is >=0  single left
rotation restores the balance.
If bf > 1 and the bf of the root of the
right-sub-tree is <0  to restore the
balance double rotation is required:
1. single right rotation for the right
sub-tree root; and
2. single left rotation for the current
node.
Value =
{Key, Data}
int bf
left
right
for bf <-1 operation are symmetrical.
D. Christozov
COS 221 Intro to CS II
AVL Trees
7
AVL tree: insertion
if(left->balFac != oldbf && left->balFac) balFac--;
new
11:?
9:0
15:0
5:0
3:0
12:-1
12:-
7:1
Update of balance factors
follows the way back of the
path of insertion.
18:0
2
2:0
4:0
Unbalanced:
Double rotation
D. Christozov
8:0
10:0
10:
1
16:0
11:0
COS 221 Intro to CS II
AVL Trees
20:0
Insertion of the new node
as right child increments
the balance factor
8
AVL tree: insertion
Direction of rotation depends on the sign of the balance factor:
positive  left rotation; negative  right rotation.
12:-2
11:0
12:-2
2
10:1
11:-1
10:0
12:0
1
11:0
10:0
Double rotation:
1: Rotation around
the left child
D. Christozov
Double rotation:
2: Rotation around
the unbalanced node.
COS 221 Intro to CS II
AVL Trees
9
AVL tree: removal
1.
2.
3.
Follows the algorithm for removal in a BST
Updates the balance factors
Restores balance, when needed
Further update of balance
factors follows the way back
of the path of node removal.
(the left-most descendent)
Let us remove 15
9:1
9:0
15:1
15.8:0
5:-1
if(left->balFac != oldbf &&
11:-1
left->balFac == 0) balFac--;
3:1
2:0
10:-1
7:1
4:-1
3.5
D. Christozov
8:0
The parent’s
balance factor is
incremented: the
left sub-tree has
one level less
18:0
18:-1
12:0
11.5:0
20:1
16:-1
16:0
15.8:-1
15.9:0
17
15.9:0
The left-most descendent of
the right sub-tree is 15.8
COS 221 Intro to CS II
AVL Trees
25
It’s right
child will
become the
left child of
its parent.
10