Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

AVL Trees

Adelson-Velsky and Landis


(3 Russian Scintists)

1
Height of a Node
3

1 0 2

0 0 0 0
1

0 0

• Hight is max number of nodes to reach


leaves
AVL Trees
• A self-balancing binary search
• AVL trees requires a relaxed balanced
condition
• Define “balanced factor” of a node to be
the absolute difference in heights between
left and right sub trees

3
AVL Trees
• AVL tree is defined as a tree such that, for
each node
• Balanced factor 1, 0, -1
• Duplicate elements are not allowed in BST
• BST (1,2,3,4-> right skewed)
• BST (4,3,2,1-> left skewed)
• Both above cases O(n)
• Make them AVL and now O(log n)
4
AVL Trees
• LL (rotate clock wise)(Single Rotation)
• RR (rotate anti clock wise) (Single Rotation)
• LR->LL-> (Two Rotation)
• RL->RR-> (Two Rotation)
• BT contains BST contains AVL tree
• Balanced BST -> O(log n)
• Make BST balanced -> AVL Tree

5
AVL-Trees
G. M. Adelson-Velskii and E. M. Landis, 1962

1 or less

6
AVL-Trees
An AVL-tree is a BST with the property that at
every node the difference in the depth of the left
and right subtree is at most 1.

not OK
OK

7
Problem

Insertion in an AVL-tree is more complicated:


inserting a new element as a leaf may break the
balance of the tree.

But we can't just place the new element somewhere


else, we have to maintain the BST property.

Solution: insert in standard place, but then


rebalance the tree.

8
But How?

The magic concept is rotation. A rotation rearranges


a BST, but preserve the BST property.

Can be done out in O(1) steps.

To fix up an AVL tree, we have to perform several


rotations, but only along a branch: total damage is
O(log #nodes).

9
But How?
rotate right on y

x
y
y
x

C
A
B B C
A

rotate left on x 10
So?

Rotation does not change the levelling, so we still


have a BST.

But the depth of the leaves change by -1 in A, stay


the same in B, and change by +1 in C.

11
Well, not quite

Unfortunately, there are other cases to consider,


depending on where exactly the balance condition
is violated.

To fix some of them requires a double rotation.

12
Tree Balance Rotations
• Note that after an insertion only the nodes
in the path from root to the node have their
balance information altered
• Find the node that violates the AVL
condition and rebalance the tree.
• Assume that X is the node that has
violated the AVL condition
– I.e The left and right sub trees of X is differ by
2 in height.

13
Four Cases – Case I
Case I – The left subtree of the left child of X violates the
property. Rotate RIGHT
y
X

y X

C
B
C
A A
B
14
Four Cases – Case 2
Case 2 – The right subtree of the right child of X violates
the property. Rotate LEFT
X y

X
y

C
B
A A
C B
15
Four Cases – Case 3
Case 3 – The right subtree of the left child of X violates the
property X
X Rotate the tree LEFT
about X’s child and
Grandchild

Z
y
y A
Z A

D B
D C
C B
Ctd.. 16
Four Cases – Case 3 ctd..

Rotate the tree RIGHT


about X and its new Z
X child

y X
Z

y A
D C
B A
B
D C
17
Four Cases – Case 4
Case 4 – The left subtree of the right child of X violates the
property
Rotate the tree RIGHT X
X about Y and Z

y D Z
D
y
Z
A
C B A
C B
18
Ctd..
Four Cases – Case 4 ctd..

Rotate the tree LEFT


X about X and Z Z

X y
Z
D
y
D
C B A
C

B A

19
AVL Tree Examples
• Insert 12, 8, 7, 14, 18, 10, 20 with AVL
rotations

20
AVL Summary
• Insert a new node in left or right subtree.
• Test the height information along the path
of the insertion. If not changed, we are
done
• Otherwise do a single or double rotation
based on the four cases

21
Homework/Reading Assignment

22
Depth of a Node
0

1 1 1

2 2 2

Often convenient to add another field to node structure


for additional information such as:
depth, height, visited, cost, father, number of visits,
number of nodes below, etc.
1/1+1/2+…1/n is O(log n)
• General Trick: sum approximates integral
and vice versa
• Area under function 1/x is given by log(x).

1 2 3 4
AVL Tree
• Recall height of empty tree = -1
• In AVL tree, For all nodes, height of left and right subtrees
differ by at most 1.
• AVL trees have logarithmic height
• Fibonacci numbers: F[1]=1; F[2]= 1; F[3]=2; F[4]=3;
• Induction Strikes: Thm: S[h] >= F[h+3]-1
Let S[i] = size of smallest AVL tree of height i
S[0] = 1; S[1]=2; why?
So S[1] >= F[4]-1
S[h]=S[h-1]+S[h-2]+1 >=F[h+2]-1+F[h+1]-1+1
= F[h+3]-1.
• Hence number of nodes grows exponential with height.
On Insertion, what can go
wrong?
• Tree balanced before insertion

1 2
0 1 1
1

H-1
H
Insertion
• After insertion, there are 4 ways tree can
be unbalanced. Check it out.
• Outside unbalanced: handled by single
rotations
• Inside unbalanced: handled by double
rotations. 2 2

1 1
c r

p
b
a

q
Maintaining Balance
• Rebalancing: single and double rotations
• Left rotation: after insertion

1
2
2
1
c

b a b c
a
2 Another View 1
2
a
1 c Left
b c
a b

1 Right 2
a 2 1
c
a b
b c

Notice what happens to heights


2 Another View 1
2
a
1 c Left
b c
a b

1 Right 2
a 2 1
c
a b
b c

Notice what happens to heights, (LEFT) in general:


a goes up 1, b stays the same, c goes down 1
Single (left) rotation
• Switches parent and child
• In diagram: static node leftRotate(node 2)
1 = 2.left
2.left = 1.right
1.right = 2
return 1
• Appropriate test question
– do it, i.e. given sequence of such as 6, 2, 7,1, -1 etc
show the succession on trees after inserts, rotations.
• Similar for right rotation
Double
3 Rotation (left)
1

Out of balance: split

2
3

1 3
1

2
3
In Steps 3

d 2 d
1
c
2 1
a
a b
b c

2
3
1
c d
b
a
Double Rotation Code (left-
right)
• Idea: rotate left child with its right child
• Then node with new left child
• static BinaryNode doubleLeft( BinaryNode n)
n.left = rotateRight(n.left);
return rotateLeft(n)
• Analogous code for other middle case
• All rotations are O(1) operations
• Out-of-balance checked after insertion and after
deletions. All O(1).
• For AVL, d is O(logN) so all operations O(logN).

You might also like