You are on page 1of 26

AVL Trees

Rotations

Muralidhara V N
IIIT Bangalore
AVL Tree
Z be a node where the AVL property is
violated.

Y be the child of Z such that H(Z)=H(Y)+1

X be the child of Y such that H(Y)=H(X)+1

We know that H(Z)>1, hence X and Y


are valid nodes in the AVL Tree
Four Rotations

Z Z Z Z

Y Y Y Y

X X X X
0=00 1=01 2=10 3=11
Replace the Subtree
Z be a node where the AVL property is
violated.

Replace the subtree tree rooted at Z with


the Subtree rooted at X/Y.

if (Z->parent){ if (Z->parent->left==Z) Z->parent->left=X/Y;


else Z->parent->right=X/Y;}
else X/Y become the root AVL Tree

Z X/Y
Zig-Zig (0)
Z Y

Y
T4 Z
X
X
T3

T1 T2 T1 T2 T3 T4

In order Traversal of the both is T1 X T2 Y T3 Z T4


T3=Y->right; Y->right=Z; Z->left=T3;
Y->p=Z->p; Z->p=Y; T3->p=Z;
Zig-Zig (3)
Z Y

Y
T1 Z X

X
T2

T1 T2 T3 T4
T3 T4

In order Traversal of the both is T1 Z T2 Y T3 X T4


T2=Y->left; Y->left=Z; Z->right=T2;
Y->p=Z->p; Z->p=Y; T2->p=Z;
Zig-Zag (1)
Z X

Y
T4 Z
Y
X
T1

T1 T2 T3 T4
T2 T3

In order Traversal of the both is T1 Y T2 X T3 Z T4


T2=X->left; T3=X->right; Y->right=T2; Z->left=T3;
X->p=Z->p; Z->p=X; Y->p=X; T2->p=Y;T3->p=Z;
X->left=Y;X->right=Z;
Zig-Zag (2)
Z X

Y
T1 Z Y

X
T4

T1 T2 T3 T4
T2 T3
In order Traversal of the both is T1 Z T2 X T3 Y T4
T2=X->left; T3=X->right; Y->left=T3; Z->right=T2;
X->p=Z->p; Z->p=X; Y->p=X; T2->p=Z;T3->p=Y;
X->left=Z;X->right=Y;
Deletion in AVL Tree
Goto the parent of the deleted node
1. check if the AVL property is violated.
2. update the height

Stop: If height does not change and AVL


property is not violated.

Z be a node where the AVL property is violated.


Y be the child of Z such that H(Z)=H(Y)+1
X be the child of Y such that H(Y)=H(X)+1
Zig-Zig (0) - Deletion
h Z h/h-1 Y
h-1
Y h-3
T4 Z h-1/h-2
h-2 X
h-2
X h-2/h-3
T3

T1 T2 T1 T2 T3 T4

h-3/h-4 h-3/h-4 h-2/h-3 h-3


Zig-Zag (1)-Deletion
h Z h/h-1 X
h-3
h-1
Y
T4 Z
h-1/h-2 Y h-2

X h-2
T1

h-2/h-3 T1 T2 T3 T4
T2 T3
h-2/h-3 h-3/h-4 h-3
h-3/h-4
Insert in AVL Tree
Goto the parent of the inserted node
1. update the height
2. check if the AVL property is violated.
Stop: If height does not change.

Z be a node where the AVL property is violated.

Y be the child of Z such that H(Z)=H(Y)+1

X be the child of Y such that H(Y)=H(X)+1


Zig-Zig (0) - Insert
h Z h-1 Y
h-1
Y h-3
T4 Z
h-2 X h-2
h-2
X h-3
T3

T1 T2 T1 T2 T3 T4

h-3/h-4 h-3/h-4 h-3 h-3


Zig-Zag (1)-Insert
h Z X
h-1

h-3
h-1
Y
T4 Z
h-2 Y h-2

X h-2
T1

h-3
T1 T2 T3 T4
T2 T3
h-3 h-3/h-4 h-3
h-3/h-4
Summery
Delete: You may have to do O(log n)
Rotations

Insert: You will at most one Rotations

In an AVL Tree Search , Insert and


Deletion can be done in O(log n)
Rank (X)
Implement a Data Structure which supports the following
operations efficiently.

• Search
• Insert
• Delete

• Rank(X) – 1+ number of numbers in


the collection that are >X.
Node count

52 11

4
6 26 73

15
2 3
19 32 64 93 2

1 1

29 39 85
1
1 1
Structure of the BBST

struct bst
{
    long long int key;
long long int count;
int height;
    struct bst *left, *right, *parent;
};
Count of a node
long long int Count (struct BST *node){
if (node){
if (node->left && node->right)
return 1 + node->left->count + node->right->count;

else if (node->left) return 1 + node->left->count;

else if (node->right) return 1 + node->right->count;


else return 1;
}
else
return 0;
}
Rank (X)
long long int Rank (struct BST *node, long long int X){
long long int rank = 1;
while (node) {
if (X == node->key){
if (node->right) rank += node->right->count;
return rank;}
else if (X < node->key){
rank++;
if (node->right) rank += node->right->count;
node = node->left;}
else
node = node->right; }
return rank;
}
Find Rank (rank)

Given rank , find an number X, such


Rank(X)= r
Node count

52 11

4
6 26 73

15
2 3
19 32 64 93 2

1 1

29 39 85
1
1 1
Find Rank (rank)
struct BST * FindRank (struct BST *node, long long int rank){
long long int r;
if (node && rank > 0 && rank < node->count + 1) {
while (node) {
if (node->right) r = node->right->count + 1;
else r = 1;
if (r == rank) return node;
else if (rank > r) {
node = node->left;
rank = rank - r;
}
else node = node->right;
} }
return NULL;
}
Rangecount(l,r)
long long int
Rangecount (struct BST *node, long long int l, long long int r)
{
long long int count;
count = Rank (node, l) - Rank (node, r);

if (Search (node, l)) count++;


return count;
}
Other operations

• PrefixSum(X) = find the sum of all


the numbers smaller than X.
Other operations

• PrefixSum(X) = find the sum of all


the numbers smaller than X.
• RangeSum(X,Y) = sum of numbers
between X and Y.
• Average(X,Y) = Average of numbers
between X and Y.
• Morethanaverage(X,Y) = the no of
numbers between X and Y bigger
than Average(X,Y).

You might also like