AVL Tree: Balanced Binary Search Tree - PowerPoint PPT Presentation

About This Presentation
Title:

AVL Tree: Balanced Binary Search Tree

Description:

AVL Tree: Balanced Binary Search Tree. BST Property. At every node X, values in ... BST Property. At every node X, values in. left subtree are smaller than ... – PowerPoint PPT presentation

Number of Views:1433
Avg rating:3.0/5.0
Slides: 38
Provided by: DavidR6
Learn more at: http://www.cs.rpi.edu
Category:
Tags: avl | balanced | binary | bst | search | tree

less

Transcript and Presenter's Notes

Title: AVL Tree: Balanced Binary Search Tree


1
AVL Tree Balanced Binary Search Tree
6
2
8
4
9
1
3
2
AVL Tree Balanced Binary Search Tree
BST Property At every node X, values
in left subtree are smaller than the value in X,
and values in right subtree are larger than the
value in X.
6
2
8
4
9
1
3
3
AVL Tree Balanced Binary Search Tree
BST Property At every node X, values
in left subtree are smaller than the value in X,
and values in right subtree are larger than the
value in X.
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
6
2
8
4
9
1
3
4
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
2
8
4
9
1
3
5
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
Is this is an AVL tree?
2
8
4
9
1
3
6
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
2
8
4
9
0
1
0
0
3
7
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
1
2
8
4
9
0
1
1
0
0
3
8
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
6
1
2
2
8
4
9
0
1
1
0
0
3
9
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
Yes, this is an AVL tree.
1
2
2
8
4
9
0
1
1
0
0
3
10
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
Suppose we delete the 9 node.
1
2
2
8
4
9
0
1
1
0
0
3
11
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
0
2
2
8
4
1
1
0
0
3
12
AVL Tree Balanced Binary Search Tree
AVL Balance Property At every node X, the
height of the left subtree differs from the
height of the right subtree by at most 1.
height(X) max(height(left(X)),
height(right(X))) 1 height(L) -1
3
6
AVL balance property fails at 6 node.
0
2
2
8
4
1
1
0
0
3
13
Tree Rotations to Restore Balance
j
Suppose we have balance . . .
k
Z
X
Y
14
Tree Rotations to Restore Balance
j
Suppose we have balance . . .
k
Z
Y
X
. . . but we insert here, destroying the balance.
15
Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
16
Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
17
Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
18
Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
19
Tree Rotations to Restore Balance
Do a single rotation
j
k
Z
Y
X
20
Tree Rotations to Restore Balance
Do a single rotation
k
j
X
Z
Y
21
Tree Rotations to Restore Balance
Do a single rotation
k
Done!
j
X
Z
Y
22
Tree Rotations to Restore Balance
j
In one step
k
Z
Y
X
23
Tree Rotations to Restore Balance
k
j
X
Z
Y
24
Tree Rotations to Restore Balance
j
Again, suppose we have balance . . .
k
Z
X
Y
25
Tree Rotations to Restore Balance
j
Again, suppose we have balance . . .
k
Z
X
Y
. . . but this time we insert here.
26
Tree Rotations to Restore Balance
j
Again, we do a single rotation . . .
k
Z
X
Y
27
Tree Rotations to Restore Balance
Again, we do a single rotation . . .
k
. . . but it fails to restore balance
j
X
Z
Y
28
Tree Rotations to Restore Balance
j
Go back to where we started . . .
k
Z
X
Y
29
Tree Rotations to Restore Balance
j
Go back to where we started . . .
k
Z
X
. . . and consider structure of Y
Y
30
Tree Rotations to Restore Balance
j
k
Z
i
X
V
W
31
Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
32
Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
33
Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
34
Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
35
Tree Rotations to Restore Balance
j
Now do a double rotation. . .
k
Z
i
X
V
W
36
Tree Rotations to Restore Balance
Now do a double rotation. . .
i
j
k
Z
V
W
X
37
Tree Rotations to Restore Balance
Now do a double rotation. . .
i
j
k
Done!
Z
V
W
X
Write a Comment
User Comments (0)
About PowerShow.com