Вы находитесь на странице: 1из 60

AVL Trees

AVL TREE

Overview
Binary trees

Binary search trees


Find Insert Delete AVL trees

Hierarchical Data Structures


Tree
Single parent, multiple children

Binary tree
Tree with 02 children per node

Tree

Binary Tree

Trees
Terminology
Root no predecessor Leaf no successor Interior non-leaf Height distance from root to leaf
Root node Interior nodes Height

Leaf nodes
4

Types of Binary Trees


Degenerate only one child

Balanced mostly two children


Complete always two children

Degenerate binary tree

Balanced binary tree

Complete binary tree


5

Binary Trees Properties


Degenerate
Height = O(n) for n nodes Similar to linear list

Balanced
Height = O( log(n) ) for n nodes Useful for searches

Degenerate binary tree

Balanced binary tree


6

Binary Search Trees


Key property
Value at node Smaller values in left subtree
Larger values in right subtree Example X>Y X<Z

Binary Search Trees


Examples
5
10 2 45 5 30 2 25 45 2 25 Binary search trees Non-binary search tree
8

10 45 25 30

30

10

Example Binary Searches


Find ( 2 )
10 10 > 2, left

5
2 30 45

5 > 2, left 2 = 2, found

5
2 25

30
45

5 > 2, left 2 = 2, found

10
25

Binary Search Properties


Time of search
Proportional to height of tree Balanced binary tree O( log(n) ) time
Degenerate tree O( n ) time Like searching linked list / unsorted array

Requires
Ability to compare key values

10

Binary Search Tree Insertion


Algorithm
Perform search for value X
Search will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y

If X > Y, insert new leaf X as new right subtree for Y

Observations
O( log(n) ) operation for balanced tree Insertions may unbalance tree

11

Example Insertion
Insert ( 20 )
10 5 2 25 30 45 10 < 20, right 30 > 20, left

25 > 20, left


Insert 20 on left

20
12

Binary Search Tree Deletion


Algorithm
Perform search for value X
If X is a leaf, delete X Else

// must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree O( log(n) ) operation for balanced tree Deletions may unbalance tree

Observation

13

Example Deletion (Leaf)


Delete ( 25 )
10 10

10 < 25, right

5
2 25

30
45

30 > 25, left


25 = 25, delete 2

30
45

14

Example Deletion (Internal Node)


Delete ( 10 )
10 5 5

5
2 25

30
45 2

5
25

30
45 2

2
25

30
45

Replacing 10 with largest value in left subtree

Replacing 5 with largest value in left subtree

Deleting leaf

15

Example Deletion (Internal Node)


Delete ( 10 )
10 25 25

5
2 25

30
45 2

5
25

30
45 2

30
45

Replacing 10 with smallest value in right subtree

Deleting leaf

Resulting tree

16

AVL - Good but not Perfect Balance


AVL trees are height-balanced binary search trees Balance factor of a node
height(left subtree) - height(right subtree)

An AVL tree has balance factor calculated at every node


For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node

17 AVL TREE

Node Heights
Tree A (AVL) height=2 BF=1-0=1 6 1 0 4 9 0 0 5 1

Tree B (AVL) 2 6 1 4
0 5 0 8

1 9

0 1

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


18 AVL TREE

Node Heights after Insert 7


Tree A (AVL) 2 6 1 4 0 1 0 5 0 7 1 9

Tree B (not AVL) 3 6 1 4 0 0 1 1 5 8


0 7

balance factor 1-(-1) = 2

2 9
-1

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


19 AVL TREE

Insert and Rotation in AVL Trees

Insert operation may cause balance factor to become 2 or 2 for some node
only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or 2, adjust tree by rotation around the node

20 AVL TREE

Single Rotation in an AVL Tree

2 6 1 4 0 1 0 5 0 7 1 8 2 9 0 1 1 4 0 5

2 6 1 8 0 7 0 9

21 AVL TREE

Insertions in AVL Trees


Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms.
22 AVL TREE

AVL Insertion: Outside Case


Consider a valid AVL subtree

j
h

k
h h

Y
23 AVL TREE

AVL Insertion: Outside Case

Inserting into X destroys the AVL property at node j h

k
h+1 h

Y X
24 AVL TREE

AVL Insertion: Outside Case

Do a right rotation

k
h+1 h

Y X
25 AVL TREE

Single right rotation

Do a right rotation

k
h+1 h

Y X
26 AVL TREE

Outside Case Completed

k
h+1

Right rotation done! (Left rotation is mirror symmetric)

j
h
h

Z
27 AVL TREE

AVL property has been restored!

AVL Insertion: Inside Case


Consider a valid AVL subtree

j
h

k
h

Y
28 AVL TREE

AVL Insertion: Inside Case


Inserting into Y destroys the AVL property at node j

j k
h+1

Does right rotation restore balance? h

Y
29 AVL TREE

AVL Insertion: Inside Case

k
h

j
h+1

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

X Y

Z
30 AVL TREE

AVL Insertion: Inside Case


Consider the structure of subtree Y

j
h h+1

k
h

Y
31 AVL TREE

AVL Insertion: Inside Case


Y = node i and subtrees V and W

j
h

k
h

X V

h+1

h or h-1

W
32 AVL TREE

AVL Insertion: Inside Case

j k X V

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

i
W

33 AVL TREE

Double rotation : first rotation

left rotation complete

i
k

Z W
V
34 AVL TREE

Double rotation : second rotation

Now do a right rotation

i
k

Z W
V
35 AVL TREE

Double rotation : second rotation


right rotation complete

i
k
h h or h-1

Balance has been restored

j
h

Z
36 AVL TREE

Example of Insertions in an AVL Tree

2 20 0 10 0 25 1 30 0 35

Insert 5, 40

37 AVL TREE

Example of Insertions in an AVL Tree

2 20 1 10 0 5 0 25 1 30 0 35 0 5 1 10

20

3 2 30 0 25 1 35

Now Insert 45

0 40

38 AVL TREE

Single rotation (outside case)

3 20 1 10 0 5 0 25 Imbalan ce 2 30 2 35 1 40 0 5 1 10

20

3 2 30 0 25 40 1

0 35 Now Insert 34

0 45

0 45

39 AVL TREE

Double rotation (inside case)

3 20 1 10 0 5 0 Imbalan 25 ce 1 35 Insertion of 34 0 3 30 2 40 0 5 1 10

20

3 2 35 1 30 40 1 34

45 0

0 25

0 45

34
40 AVL TREE

x
C
8

AVL Tree
3

8
4

3.5

Insert 3.5 4 5
3.5 8

After Rotation
41

An Extended Example

Insert 3,2,1,4,5,6,7, 16,15,14


3 3

Single rotation
2

3
Fig 1 2 Fig 2 2 1 Fig 4

1 Fig 3 1

Single rotation
1 Fig 5 4

3
Fig 6

3
4

42

2 1 3 1

Single rotation 4
5 Fig 7 4 3 Fig 8 4

4
5

6 Single rotation
6 Fig 10 7

2 1

5
3
Fig 9 2 1 3 5 6 4 1

5
3

6
7 Fig 11
43

4 2 1 3 5

6
7 16

Fig 12
4

Double rotation
2 1 3

6
2
7

6
3 5 15 16

5
Fig 13 15

16

Fig 14

7
44

4 2 1 3

Double rotation 6
5 15 1 2 3

4
7 6 15

16
7 Fig 15 14 5 Fig 16 14

16

45

Remove Operation in AVL Tree


Removing a node from an AVL Tree is the same as removing from a binary search tree. However, it may unbalance the tree. Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node.

Use the appropriate single or double rotation to balance the tree.


May need to continue searching for unbalanced nodes all the way to the root.
46

Deletion X in AVL Trees


Deletion:
Case 1: if X is a leaf, delete X Case 2: if X has 1 child, use it to replace X Case 3: if X has 2 children, replace X with its inorder predecessor (and recursively delete it)

Rebalancing

47

Delete 55 (case 1)

60 20 10 40 65 70 85

15

30

50
55

80

90

48

Delete 55 (case 1)

60 20 10 40 65 70 85

15

30

50
55

80

90

49

Delete 50 (case 2)

60 20 10 40 65 70 85

15

30

50
55

80

90

50

Delete 50 (case 2)

60 20 10 40 65 70 85

15

30

50

80

90

55

51

Delete 60 (case 3)

60 20 10 40 65 70 85

15

30

50

prev 55

80

90

52

Delete 60 (case 3)

55 20 10 40 65 70 85

15

30

50

80

90

53

Delete 55 (case 3)

55 20 10 40 prev 65 70 85

15

30

50

80

90

54

Delete 55 (case 3)

50 20 10 40 65 70 85

15

30

80

90

55

Delete 50 (case 3)

50 20 10 40 prev 65 70 85

15

30

80

90

56

Delete 50 (case 3)

40 20 10 30 65 70 85

15

80

90

57

Delete 40 (case 3)

40 20 10 30 prev 65 70 85

15

80

90

58

Delete 40 : Rebalancing

30 20 10 Case ? 65 70 85

15

80

90

59

Delete 40: after rebalancing

30 10 5 20 65 70 85

15

80

90

Single rotation is preferred!

60

Вам также может понравиться