Você está na página 1de 24

AVL Tree

CSE 203N
Data Structures
4
4
21 83
7 8
3 1 26 91
2 5 7
15 8
1
4 2
Height-Balance Property
• For every internal node v of T, the heights of
the children of v can differ by at most 1.

AVL Tree
• Any Binary Search Tree (BST) that satisfies the
height-balance property is said to be AVL tree.
• Named after the Inventors –
• Adel’son, Vel’skii and Lendis.
Binary Search Tree
44

17 88

1
4
32 Not an AVL 65Tree 97

28 54 82

76
29

80
Binary Search Tree
4
44

3
2
17 88

1 2 1
32
AVL Tree65 97

1 1
54 82
AVL Tree
• Proposition: The height of an AVL tree T
storing n elements is O(log n)
• Justification:
Let, the minimum number of internal nodes = n(h)
where h = height of the tree.
so, n(1) = 1; n(2) = 2;
n(h) = 1+ n(h-1) + n(h-2); for h3
since, n(h-1) > n(h-2)
n(h) > 2.n(h-2)
> 4.n(h-4)

> 2i.n(h-2i) for integer i, such that (h-2i)  1
AVL Tree
• Proposition: The height of an AVL tree T
storing n elements is O(log n)
• Justification:
 h  1
i=  2  h 
 2   1 h 2
so, n(h) > 2  h  .1 n(h- 2.  2  )
2 

2 .h n(1)
1
2
2
h
 log n(h)  2  1
 h  2 log n(h) + 2  h  O(log n)
AVL Tree

INSERTION
AVL Tree (Insertion)
4
44

3
2
17 88

All 1the nodes are2 Balanced 1


32 65 97

1 1
54 82
AVL Tree (Insertion)
Insert Item 72 4
Step 1: insertItem(72) as done in44the binary search
tree
3
2
17 88

1 2 1
32 65 97

1 1
54 82

72
AVL Tree (Insertion)
Insert Item 72 (w) 5
Unbalanced
Step
Step
4:
3: 2:
find
find
thethe
child
firstnode
nodexy zoffrom
yz which
44 w which
has higher
is
unbalance
height
4 Unbalanced
2
z 88
17
y
1 3 1
32 65 97
x
1 2
54 82

1
72 w
AVL Tree (Insertion)
Insert Item 72 (w) 5
44
Step 5: a, b, c is the inoder listing of nodes x, y and z
4
2
17 z 88

y 1
1 3
32 65 97

1 x
2
54 82

a=y 1
b=x 72 w
c=z
AVL Tree (Insertion)
Insert Item 72 (w) 5
44

4
2
z 88
17
T3
y 1
1 3
32 65 97

1 x
2
54 82

a=y 1
b=x T0 72 w
T2
c=z
T1
AVL Tree (Insertion)
a=z b=y
Single rotation
b=y
c=x
c=x a=z

T0

T1
T0 T1 T2 T3
T2 T3
AVL Tree (Insertion)

c=z b=y
Single rotation
b=y
c=z
a=x
a=x
T3

T2
T0 T1 T2 T3
T0 T1
AVL Tree (Insertion)
a=z b=x
c=y
Double rotation
c=y
a=z
b=x

T0
T3
T0 T1 T2 T3
T1 T2
AVL Tree (Insertion)

c=z b=x
Double rotation
a=y
c=z
a=y
b=x
T3

T0
T0 T1 T2 T3
T1 T2
AVL Tree (Insertion)
Insert Item 72 (w)
44

z 88
17
T3
y
32 65 97

x
54 82

a=y
b=x T0 72 w
T2
c=z
T1
AVL Tree (Insertion)
Insert Item 72 (w)
44

z 88
17
T3
32 82 x 97

y
65
T2
54 72 w

T0
T1
AVL Tree (Insertion)
Insert Item 72 (w) 4
44 3
x
82
2
17

1 The tree is Balanced


y 2 now 2 z
32 65 88

1
1 1 97
54 72

w
T2
T0
T1
T
AVL Tree

DELETION
AVL Tree (Deletion)
Delete Item 32 4
Step 1: deleteItem(32) as done in44the binary search
tree
3
21
17 88

1 2 2
32 65 97

1 1 1
54 82 92
AVL Tree (Deletion)
Delete Item 32 4 z
44

1
3 y
17 88

2 2
65 x 97

1
a=z 1
54 82
1
92
b=y
c=x

After a single rotation


AVL Tree (Deletion)
4 y
88

3 z 2 x
44 97

1
All the
2 nodes are Balanced
1 Again
92
17 65

1 1
54 82
Thank You

Você também pode gostar