You are on page 1of 64

The AVL Trees

CS221(A) – Data Structures &


Algorithms
AVL Trees
• Binary search tree with a balance condition.
• The balance condition must be easy to
maintain and it ensures that the dept of the
tree is O(log n).
• Idea is to acquire same height on the left and
right sub-trees.
• Prevent a worst case running time of O(n). i.e.
a left-iest or right-iest tree. A tree structure
looking like a linear list.
AVL Trees
• Balanced Condition
– left height(n) = 0 if n has no left child.
– = 1 + height(left child(n)) for all other
nodes.
– right height(n) = 0 if n has no right child.
– = 1 + height(right child(n)) for all
other nodes.

– Balance(n) = right height(n) – left height(n)


AVL Trees
• Balance condition indicates the relative height
of its right sub-tree compares to its left.
• If the balance is positive the right sub-tree has
greater depth than the left sub-tree.
• If the balance is negative the left sub-tree has
greater depth than the right sub-tree.
• A binary tree is an AVL tree if and only if every
node in the tree has a balance of -1 , 0 or +1.
AVL Trees
balance = right height – left height
= 0 - 0 (AVL Tree)
x balance = right height – left height
0 0 = 2 – 1 = 1 (AVL Tree)
2
1 2
balance = right height – left height
1 5
= 1 – 1 = 0 (AVL Tree) 1 0
0 0
2
1 1
4
1 5 0 0
0 0 0 0
AVL Trees
balance = right height – left height balance = right height – left height
= 1 – 2 = -1 (AVL Tree) = 1 – 3 = -2 (Non-AVL Tree)

3
2 1 5
3 1
1 5 8
0 1 0 0 2 0 0
1 2
2
0 0 1 4
0 0 1 0

3
0 0
AVL Trees
• Height of the two child sub-trees of any node differ by
at most one; therefore it is also said to be height-
balanced.
• Searching, Insert and Delete all take O(log n) in both
average and worst cases. Where n is the number of
nodes in the tree prior to the operation.
• Insertions and deletions may require the tree to be
rebalanced by one or more tree rotation.
• Node with balance factor 1,0 or -1 is considered
balanced.
• Node with any other balance factor is called unbalance
and need to be rotated or rebalanced.
AVL Trees
balance = right height – left height
=?–?=?

2
8

1 4

3
6
AVL Trees

2
8 balance = right height – left height
= 0 – 2 = -2 ( Non-AVL)
1 4

3
6
AVL Trees

2 balance = right height – left height


8 = 0 – 2 = -2 ( Non-AVL)

1 4

3
6
AVL Trees
balance = right height – left height
= 2 – 3 = -1 (AVL Tree)

2
7

1 4

6 8

3
AVL Trees
balance = right height – left height
=?–?=?

1 3

5
AVL Trees

2
balance = right height – left height
3 = 2 – 0 = 2 (Non- AVL)
1

5
AVL Trees

2
balance = right height – left height
3 = 2 – 0 = 2 (Non- AVL)
1

5
AVL Trees
balance = right height – left height
= 2 – 1 = 1 (AVL Tree)

1 4

3 5
AVL Trees
balance = right height – left height
=?–?=?

1 4

3 5

6
AVL Trees

balance = right height – left height


2 = 3 – 1 = 2 (Non AVL)

1 4

3 5

6
AVL Trees

balance = right height – left height


2 = 3 – 1 = 2 (Non AVL)

1 4

3 5

6
AVL Trees
balance = right height – left height
= 2 – 2 = 0 (AVL Tree)

2 5

1
3 6
AVL Trees
balance = right height – left height
=?-?=?

2 5

1
3 6

7
AVL Trees

4
balance = right height – left height
= 2 - 0 = 2 (Non AVL)
2 5

1
3 6

7
AVL Trees

4
balance = right height – left height
= 2 - 0 = 2 (Non AVL)
2 5

1
3 6

7
AVL Trees
balance = right height – left height
= 2 - 2 = 0 (AVL Tree)

2 6

1
3 5 7
AVL Tree Implementation
• typedef struct AvlNode *Position;
• typedef struct AvlNode *AvlTree;
• struct AvlNode
• {
– ElementType Element;
– Avltree Left;
– Avltree Right;
– int Height;
• }
AVL Tree Insertion
• Trace the path from the root node to a leaf
node.
• Insert the new node.
• Retrace the path back up to the root, adjusting
balances along the way.
• If a nodes balance is out of the bound
condition i.e.
-1 > balance >1. rotate and rebalance.
AVL Tree Insertion
• Rotation
– Singly Rotation
• It preserves the Ordered property of the tree.
• It restores all nodes to appropriate AVL balance.
• Preserves the Inorder traversal of the tree i.e. Inorder
traversal will access nodes in the same order after
transformation.
• Only modify three pointers to accomplish the
rebalancing.
AVL Trees Implementation
• Singly Rotation ( Left and Right )
– Right Rotation of Root Q results in Q stepping
down a hierarchy. P becomes Root with Q as right
child and right child of P becomes left child of
Q.
AVL Tree Rotation

k2

k1
Z

Y
X
AVL Tree Rotation
k2

k1
Z

k1

Y
k2
X

X
Y Z
AVL Tree Rotation
k1

k2

X Y
Z
AVL Tree Rotation
k1

k2

X Y k2

k1

Z
X Y
AVL Tree Implementation
• Position LeftRotation(Position K2)
• {
– Position K1 = K2 Right;
– K2Right = K1Left;
– K1Left = K2;
– K2Height = Max(Height(K2Right),
Height(K2Left))+1;
– K1Height = Max(Height(K1Right),
K2Height) + 1;
– return K1;
• }
AVL Tree Implementation
• Position RightRotation(Position K2)
• {
– Position K1 = K2 Left;
– K2Left = K1Right;
– K1Right = K2;
– K2Height = Max(Height(K2Left),
Height(K2Right))+1;
– K1Height = Max(Height(K1Left),
K2Height) + 1;
– return K1;
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree
T)
• {
– Condition 1: T == Null
– Condition 2: X < TElement
– Condition 3: X > TElement
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // T== Null
– If (T == Null)
–{
• T = malloc(sizeof(struct AvlNode));
• TElement = X;
• THeight = 0;
• TLeft = Null;
• TRight == Null;
–}
– .. cont
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X < TElement
– If (X < TElement)
–{
• TLeft = Insert(X,TLeft);
• if (Height(TLeft) – Height(TRight) == 2)
• {
– T = RightRotation(T);
• }
–}
– .. cont
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X > TElement
– If (X > TElement)
–{
• TRight = Insert(X,TRight);
• if (Height(TRight) – Height(TLeft) == 2)
• {
– T = LeftRotation(T);
• }
–}
– THeight =
Max(Height(TLeft),Height(TRight))+1;
– return T;
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X > TElement
– If (X > TElement)
–{
• TRight = Insert(X,TRight);
• if (Height(TRight) – Height(TLeft) == 2)
• {
– T = LeftRotation(T);
• }
–}
– THeight =
Max(Height(TLeft),Height(TRight))+1;
– return T;
• }
AVL Tree Implementation
• Position LeftRightRotation(Position K3)
• {
– K3Left = LeftRotation(K3Left)
– return RightRotation(K3);
• }

• Position RightLeftRotation(Position K3)


• {
– K3Right = RightRotation(K3Right)
– return LeftRotation(K3);
• }
AVL Trees
balance = right height – left height
= 2 - 2 = 0 (AVL Tree)

2 6

1
3 5 7
AVL Trees
4

2 6

1
3 5 7

16
AVL Trees
4

2 6

1
3 5 7

16

15
AVL Trees
4

2 6

1
3 5 7

16

15
AVL Trees
4

2 6

1
3 5 7

15

16
AVL Trees
4

2 6

1
3 5 15

7 16
4
AVL Trees
2 6

1
3 5
15

7 16

14
4
AVL Trees
2 6

1
3 5
15

7 16

14
AVL Trees
4

2
7

1
3
6 15

16
5 14
AVL Trees
4

2
7

1
3
6 15

16
5 14

Insert 13
AVL Trees
4

2
7

1
3
6 15

16
5 14

13
AVL Trees
7

4
15
2
6

14 16
1 3

5
13

Insert 12
AVL Trees
7

4
15
2
6

14 16
1 3

5
13

12
AVL Trees
7

4
15
2
6

16
1 3 13

5
12 14

Insert 11
AVL Trees
7

4
15
2
6

16
1 3 13

5
12 14

11
AVL Trees
7

13
2
6

1 3 15
12

5
11 14 16

Insert 10
AVL Trees
7

13
2
6

1 3 15
12

5
11 14 16

10
AVL Trees
7

13
2
6

1 3 15
11

5
12 14 16
10

Insert 8
AVL Trees
7

13
2
6

1 3 15
11

5
12 14 16
10

Insert 9 8
AVL Trees
7

4
13
2
6

1
3 5 11 15

10
12 14 16

9
AVL Trees
7

4
13
2
6

1
3 5 11 15

10
12 14 16

9
AVL Trees
7

4
13
2
6

1
3 5 11 15

10
12 14 16

8
AVL Trees
7

4
13
2
6

1
3 5 11 15

9
12 14 16

8
10
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X < TElement
– If (X < TElement)
– {
• TLeft = Insert(X,TLeft);
• if (Height(TLeft) – Height(TRight) == 2)
• {
• If (x<TLeftElement)
– T = RightRotation(T);
Else
T = RightLeftRotation(T);
• }
– }
– .. cont
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X > TElement
– If (X > TElement)
– {
• TRight = Insert(X,TRight);
• if (Height(TRight) – Height(TLeft) == 2)
• {
• If (x>TRightElement)
– T = LeftRotation(T);
Else
T = LeftRightRotation(T);
• }
– }
– THeight = Max(Height(TLeft),Height(TRight))+1;
– return T;
• }

You might also like