properties of avl tree

Post on 22-Feb-2016

37 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Properties of AVL Tree. The height of an AVL tree with n nodes is O(log n ) For every value of n , n  0, there exists an AVL tree An n -node AVL search tree can be searched in O(height) = O(log n ) time - PowerPoint PPT Presentation

TRANSCRIPT

Properties of AVL Tree1. The height of an AVL tree with n nodes is O(log n)2. For every value of n, n 0, there exists an AVL tree3. An n-node AVL search tree can be searched in O(height) = O(log

n) time4. A new node can be inserted into an n-node AVL search tree so

that the result is an n+1 node AVL tree and insertion can be done in O(log n) time

5. A node can be deleted from an n-node AVL search tree, n>0, so that the result is an n-1 node AVL tree and deletion can be done in O(log n) time

3

Balancing Operations

• We prefer to use balancing operations after each add or remove element operation

• Semantics of balancing operations– Right rotation– Left rotation– Rightleft rotation– Leftright rotation

4

Balancing Operations• Semantics of Right Rotation

A. Make the left child of the root the new rootB. Make former root the right child of the new rootC. Make right child of the former left child of the former

root the new left child of the former root

13

157

105

3

7

135

103 15

InitialTree Step A Step B Step C

7

135

103 1510

7

135

3 15

5

Balancing Operations• Semantics of Left Rotation

A. Make the right child of the root the new rootB. Make former root the left child of the new rootC. Make left child of the former right child of the former

root the new right child of the former root

5

103

7 13

15InitialTree

5

10

37

13

15

Step A

5

10

37

13

15

Step B

5

10

3 7

13

15

Step C

6

Balancing Operations• Semantics of Rightleft Rotation

A. Right rotation around right child of rootB. Left rotation around root

5

133

10 15

7

InitialTree

5

103

7 13

15

After RightRotation

10

135

7 153

After LeftRotation

7

Balancing Operations• Semantics of Leftright Rotation

A. Left rotation around left child of rootB. Right rotation around root

13

155

73

10

InitialTree

13

157

105

3

After LeftRotation

7

135

103 15

After RightRotation

Balance Factor• AVL trees are normally represented using the linked

representation• To facilitate insertion and deletion, a balance factor (bf) is

associated with each node• The balance factor bf(x) of a node x is defined as

height(xleftChild) – height(xrightChild)• Balance factor of each node in an AVL tree must be –1, 0, or

1

AVL Trees 9

Balanced and unbalanced BST

4

2 5

1 3

1

5

2

4

3

7

6

4

2 6

5 71 3

Is this “balanced”?

AVL Trees 10

Perfect Balance

• Want a complete tree after every operation– tree is full except possibly in the lower right

• This is expensive– For example, insert 2 in the tree on the left and

then rebuild as a complete tree

Insert 2 &complete tree

6

4 9

81 5

5

2 8

6 91 4

AVL Trees 11

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

AVL Tree with Balance Factors

• Is this an AVL tree? • What is the balance factor for each node in this AVL tree?• Is this an AVL search tree?

-1

1

0

0 0

0

1

1

-1 0

-1

0

0

10

40

30 45

20 35

25

60

7

3 8

1 5

Searching an AVL Search Trees

• What would be the search time complexity?– O(log n)

Single and Double Rotations• Single rotations: the transformations done to correct LL and

RR imbalances• Double rotations: the transformations done to correct LR and

RL imbalances• The transformation to correct LR imbalance can be achieved

by an RR rotation followed by an LL rotation• The transformation to correct RL imbalance can be achieved

by an LL rotation followed by an RR rotation (do Exercise 15.13)

Left RotationDefinition• In a binary search tree, pushing a node A down and to the left to

balance the tree. • A's right child replaces A, and the right child's left child becomes

A's right child.

Left Rotation

15

229

124

Animated rotation example: http://www.cs.queensu.ca/home/jstewart/applets/bst/bst-rotation.html

9

4 15

12 22

A

Right RotationDefinition• In a binary search tree, pushing a node A down and to the

right to balance the tree.• A's left child replaces A, and the left child's right child

becomes A's left child.9

4 15

12 22

Right Rotation

15

229

124

A

AVL Trees 17

Example of Insertions in an AVL Tree

0

0

0

20

10 30

25

1

350

50

20

10 30

25

1

355

40

0

0

0-1

-1

-1

Now Insert 45

AVL Trees 18

Single rotation (outside case)

-2

0

-2

20

10 30

25

1

35-2

50

20

10 30

25

1

405

40

0

0

0

-1

-1

-1

45

Imbalance35 45

0 0

0

Now Insert 34

AVL Trees 19

Double rotation (inside case)

-2

0

-2

20

10 30

25

1

40+1

50

20

10 35

30

1

405

45

0 0

0

-1

Imbalance

450

-1

Insertion of 34

35

34

0

0

1 25 340

An Extended Example

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

3

Fig 1

3

2

Fig 2

3

2

1

Fig 3

2

1 3Fig 4

2

1 3

4Fig 5

2

1 34

5Fig 6

Single rotation

Single rotation

2

1 453

Fig 7 6

2

1 453

Fig 8

4

2 561 3

Fig 9

4

2 561 3

7Fig 10

4

2 671 3

5 Fig 11

Single rotation

Single rotation

4

2 671 3

5 16

Fig 12

4

2 671 3

5 16

15Fig 13

4

2 6151 3 5

167Fig 14

Double rotation

5

3

1 4

Insert 3.5

AVL Tree

8

3.5

5

3

1 4

8

4

5

1

3

3.5After Rotation

x

y

A z

B

C

8

5

4

2 7

151 3 6

1614

Fig 16

4

2 6151 3 5

167

14

Fig 15

Double rotation

An Insertion

0

-11

0

-1

1

0

0 0

01

0

-1

1

0

00

no rotation required

0

0

#'s are balance factors

Another Insertion

0

01

0

-1

1

0

0 0

01

0

-1

0

0

00

0

0

1

2

simple right rotation required

0

Another Insertion

0

01

0

-1

0

0

00

0

0

0

0 0

-1

0

-10

0

1

0

-2

-1

-1

-1

simple left rotation required

0

Another Insertion

0

-10

0

-1

1

0

00

0

1

-1

1

-2

double rotation needed a. right rotation around right subtree of the unbalanced subtree b. left rotation around root of the unbalanced subtree

Rotation case 1

Rotation case 2

Rotation case 3

Rotation case 4

AVL Trees 33

Arguments for AVL trees:1. Search is O(log N) since AVL trees are always balanced.2. Insertion and deletions are also O(logn)3. The height balancing adds no more than a constant factor to the speed of

insertion.

Arguments against using AVL trees:4. Difficult to program & debug; more space for balance factor.5. Asymptotically faster but rebalancing costs time.6. Most large searches are done in database systems on disk and use other structures

(e.g. B-trees).

Pros and Cons of AVL Trees

top related