Professional Documents
Culture Documents
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
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
1 4
3 5
6
AVL Trees
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;
– K2Right = K1Left;
– K1Left = K2;
– K2Height = Max(Height(K2Right),
Height(K2Left))+1;
– K1Height = Max(Height(K1Right),
K2Height) + 1;
– return K1;
• }
AVL Tree Implementation
• Position RightRotation(Position K2)
• {
– Position K1 = K2 Left;
– K2Left = K1Right;
– K1Right = K2;
– K2Height = Max(Height(K2Left),
Height(K2Right))+1;
– K1Height = Max(Height(K1Left),
K2Height) + 1;
– return K1;
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree
T)
• {
– Condition 1: T == Null
– Condition 2: X < TElement
– Condition 3: X > TElement
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // T== Null
– If (T == Null)
–{
• T = malloc(sizeof(struct AvlNode));
• TElement = X;
• THeight = 0;
• TLeft = Null;
• TRight == Null;
–}
– .. cont
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X < TElement
– If (X < TElement)
–{
• TLeft = Insert(X,TLeft);
• if (Height(TLeft) – Height(TRight) == 2)
• {
– T = RightRotation(T);
• }
–}
– .. cont
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X > TElement
– If (X > TElement)
–{
• TRight = Insert(X,TRight);
• if (Height(TRight) – Height(TLeft) == 2)
• {
– T = LeftRotation(T);
• }
–}
– THeight =
Max(Height(TLeft),Height(TRight))+1;
– return T;
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X > TElement
– If (X > TElement)
–{
• TRight = Insert(X,TRight);
• if (Height(TRight) – Height(TLeft) == 2)
• {
– T = LeftRotation(T);
• }
–}
– THeight =
Max(Height(TLeft),Height(TRight))+1;
– return T;
• }
AVL Tree Implementation
• Position LeftRightRotation(Position K3)
• {
– K3Left = LeftRotation(K3Left)
– return RightRotation(K3);
• }
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 < TElement
– If (X < TElement)
– {
• TLeft = Insert(X,TLeft);
• if (Height(TLeft) – Height(TRight) == 2)
• {
• If (x<TLeftElement)
– T = RightRotation(T);
Else
T = RightLeftRotation(T);
• }
– }
– .. cont
• }
AVL Tree Insertion
• AvlTree Insert(ElementType X, AvlTree T)
• { // X > TElement
– If (X > TElement)
– {
• TRight = Insert(X,TRight);
• if (Height(TRight) – Height(TLeft) == 2)
• {
• If (x>TRightElement)
– T = LeftRotation(T);
Else
T = LeftRightRotation(T);
• }
– }
– THeight = Max(Height(TLeft),Height(TRight))+1;
– return T;
• }