Você está na página 1de 17

ES 103 Data Structures and Algorithms

AVL Trees
Atul Gupta

AVL Trees [G.M. Adelson-Velsky and E.M. Landis]


What is the major disadvantage of BST?
What is the major advantage of BST?
Most operations on a BST take time proportional to
the height of the tree

Self-balancing binary trees solve this problem by


performing transformations on the tree at key
times, in order to reduce the height.
Although a certain overhead is involved, it is
justified in the long run by ensuring fast
execution of later operations.

Atul Gupta

ES 103 Data Structures and Algorithms

Properties of AVL Trees


the heights of the two child sub-trees of any
node differ by at most one
Lookup (search), insertion, and deletion all
take O(log n) time in both the average and
worst cases
Insertions and deletions may require the tree
to be rebalanced by one or more tree
rotations

Balance Factor
The balance factor of a node x is the height of
its left sub-tree minus the height of its right
sub-tree
balance factor (x) = h(left-sub-tree) h(rightsub-tree)
A node with a balance factor 1, 0, or -1 is
considered balanced

Atul Gupta

ES 103 Data Structures and Algorithms

Computing the Height of a Node in


BST
height(node) {
if (node == null)
return -1
else
return max(height(node.left),
height(node.right)) + 1;
}

Node Heights
Tree A (AVL)
height=2 BF=1-0=1

Tree B (AVL)
2

height of node = h
balance factor = hleft-hright
empty height = -1

Atul Gupta

ES 103 Data Structures and Algorithms

Node Heights after Insert 7


Tree A (AVL)
2

Tree B (not AVL)

balance factor
1-(-1) = 2

-1

0
height of node = h
balance factor = hleft-hright
empty height = -1

AVL Trees

AVL Trees: Examples

Atul Gupta

ES 103 Data Structures and Algorithms

Balancing the BST

root
Z

node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}

rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}

Atul Gupta

7
root

2
1

9
5

10

ES 103 Data Structures and Algorithms

rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}

root

7
10
2
1

rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}

Atul Gupta

root

7
10
2
1

ES 103 Data Structures and Algorithms

rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}

root

7
10
2
1

rotate_right(z)
node* rotate_right(z)
{
node root = z->left;
z->left = root->right;
root->right = z;
return root;
}

7
2
1

8
5

root

9
10

Atul Gupta

ES 103 Data Structures and Algorithms

Insert
Insert can cause the balance factor of a node
to become 2 or -2
Only nodes on the path from the insertion
point to the root might have changed
After Insert, traverse up the tree to the root,
updating heights
If a new balance factor is 2 or -2, adjust the
tree by rotation around the node

L
P
if (balance_factor(L) == 2) { //The left column
let P=left_child(L)
if (balance_factor(P) == -1) { //The "Left Right Case"
rotate_left(P //reduce to "Left Left Case"
}
//Left Left Case
rotate_right(L);
} else { // balance_factor(L) == -2, the right column
let P=right_child(L)
if (balance_factor(P) == 1) { //The "Right Left Case"
rotate_right(P) //reduce to "Right Right Case"
}
//Right Right Case
rotate_left(L);
}

Atul Gupta

ES 103 Data Structures and Algorithms

Balancing: One More Time

Atul Gupta

ES 103 Data Structures and Algorithms

AVL Insertion: Left-Left Case

Consider a valid
AVL subtree

AVL Insertion: Left-Left Case

j
k

Inserting into X
destroys the AVL
property at node j
h

h+1

Y
X

Atul Gupta

10

ES 103 Data Structures and Algorithms

AVL Insertion: Left-Left Case

j
k

Do a right rotation

h+1

Y
X

Single right rotation

j
k

Do a right rotation

h+1

Y
X

Atul Gupta

11

ES 103 Data Structures and Algorithms

Left-Left Case Completed


Right rotation done!
(Left rotation is mirror
symmetric)

k
j

h+1

AVL property has been restored!

AVL Insertion: Left-Right Case

Consider a valid
AVL subtree

Atul Gupta

12

ES 103 Data Structures and Algorithms

AVL Insertion: Left-Right Case

Inserting into Y
destroys the
AVL property
at node j

Does right rotation


restore balance?

h+1

AVL Insertion: Left-Right Case

k
j

Right rotation
does not restore
balance now k is
out of balance
h

h+1

Z
Y

Atul Gupta

13

ES 103 Data Structures and Algorithms

AVL Insertion: Left-Right Case

Consider the structure


of subtree Y

h+1

AVL Insertion: Left-Right Case

Y = node i and
subtrees V and W

k
i

h+1

h or h-1

Atul Gupta

14

ES 103 Data Structures and Algorithms

AVL Insertion: Left-Right Case

We will do a left-right
double rotation . . .

k
Z

Double rotation : first rotation

left rotation complete

i
Z

k
W
X

Atul Gupta

15

ES 103 Data Structures and Algorithms

Double rotation : second rotation

Now do a right rotation

i
Z

k
W
X

Double rotation : second rotation


right rotation complete
Balance has been
restored

i
j

k
h

Atul Gupta

h or h-1

16

ES 103 Data Structures and Algorithms

AVL Insertion: Right-Left Case


Inserting into Y
destroys the
AVL property
at node j

A right rotation at k followed


by a left rotation at j will
establish the balance !

j
k

h+1

AVL Tree
Also known as height-balance trees has
average and worse case height as O(log n)
Accordingly, search, insertion and deletion will
be O(log n) [Guaranteed]
Requires more work during insertion, i.e.
check for balancing and possible left and right
rotations. However, this helps in longer run as
the resulting search tree will always be
balanced.

Atul Gupta

17

Você também pode gostar