avl tree lecture

29
AVL Trees CSE, POSTECH

Upload: karen-nguyen

Post on 09-Apr-2015

2.078 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: AVL TREE LECTURE

AVL Trees

CSE, POSTECH

Page 2: AVL TREE LECTURE

Balanced Binary Search Trees

If the height of a binary tree is always O(log n), we can guarantee O(log n) performance for each search tree operation

Trees with a worst-case height of O(log n) are called balanced trees

An example of a balanced tree is AVL (Adelson-Velsky and Landis) tree

Page 3: AVL TREE LECTURE

AVL Tree

Definition Binary tree. If T is a nonempty binary tree with TL and TR as

its left and right subtrees, then T is an AVL tree iff1. TL and TR are AVL trees, and

2. |hL – hR| 1 where hL and hR are the heights of TL and TR, respectively

Page 4: AVL TREE LECTURE

AVL Search Trees

An AVL search tree is a binary search tree that is also an AVL tree

Which trees in Figure 14.1 are AVL trees?– (a) and (b)

Which trees in Figure 14.1 are AVL search trees?– (b) only

Which trees in Figure 14.3 are AVL search trees?– (a) and (b)

Page 5: AVL TREE LECTURE

Indexed AVL Search Trees

An indexed AVL search tree is an indexed binary search tree that is also an AVL tree

Which trees in Figure 14.2 are indexed AVL trees?– (a) and (b)

Which trees in Figure 14.2 are indexed AVL search trees?– (a) and (b)

Page 6: AVL TREE LECTURE

Properties of AVL Tree

1. 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 tree

3. An n-node AVL search tree can be searched in O(height) = O(log n) time

4. 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

Page 7: AVL TREE LECTURE

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 asheight(xleftChild) – height(xrightChild)

Balance factor of each node in an AVL tree must be –1, 0, or 1

See Figure 15.1 for examples

Page 8: AVL TREE LECTURE

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

Page 9: AVL TREE LECTURE

Searching an AVL Search Trees

To search an AVL search tree, we can use Program 14.4 (i.e., the code for searching a binary search tree) without any change

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

Page 10: AVL TREE LECTURE

Inserting into an AVL Search Trees

If we use the strategy of Program 14.5 to insert an element into an AVL search tree, the result may not be an AVL tree

That is, the tree may become unbalanced If the tree becomes unbalanced, we must adjust the tree to

restore balance - this adjustment is called rotation See the example in Figure 15.2 Read the observations about the unbalanced tree that

results from an insertion on pages 568-569

Page 11: AVL TREE LECTURE

Inserting into an AVL Search Tree

90

Insert(9) -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

• Where is 9 going to be inserted into?• After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

Page 12: AVL TREE LECTURE

Imbalance Types

After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types

1. LL: new node is in the left subtree of the left subtree of A

2. LR: new node is in the right subtree of the left subtree of A

3. RR: new node is in the right subtree of the right subtree of A

4. RL: new node is in the left subtree of the right subtree of A

Page 13: AVL TREE LECTURE

Rotation

Definition To switch children and parents among two or three

adjacent nodes to restore balance of a tree. A rotation may change the depth of some nodes,

but does not change their relative ordering.

Page 14: AVL TREE LECTURE

Left Rotation

Definition 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

Page 15: AVL TREE LECTURE

Right Rotation

Definition 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

Page 16: AVL TREE LECTURE

AVL Rotations To balance an unbalanced AVL tree (after an insertion), we

may need to perform one of the following rotations: LL, RR, LR, RL

Figure 15.3 Inserting into an AVL search tree

Page 17: AVL TREE LECTURE

An LL Rotation

Figure 15.4 An LL Rotation

Page 18: AVL TREE LECTURE

An LR Rotation

Figure 15.5 An LR Rotation

Page 19: AVL TREE LECTURE

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)

Page 20: AVL TREE LECTURE

Exercise 15.13 – LR Rotation

• Figure (b) shows the tree after an RR rotation at node B

• Figure (c) shows the result of performing an LL rotation at node A of the tree of Figure (b).

• The resulting tree is the same as that shown in Figure 15.5 (c).

Page 21: AVL TREE LECTURE

AVL search tree insertion See Figure 15.6 for the 4-step AVL-search-tree-insertion

algorithm

Page 22: AVL TREE LECTURE

Inserting into an AVL Search Tree

29

Insert(29)-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

• Where is 29 going to be inserted into? - use the AVL-search-tree-insertion algorithm in Figure 15.6)• After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

Page 23: AVL TREE LECTURE

Inserting into an AVL Search Tree

• What are the new balance factors for 20, 25, 29?• What type of imbalance do we have?• RR imbalance new node is in the right subtree of right subtree of node 20 (node with bf = -2) what rotation do we need?• What would the left subtree of 30 look like after RR rotation?

-2

-1

029

-1

1

0

0 0

0

1

1

0

-1

0

10

40

30 45

20 35

25

60

7

3 8

1 5

Page 24: AVL TREE LECTURE

After RR Rotation-1

1

0

0 0

0

1

1

0

-1

0

10

40

30 45

35 60

7

3 8

1 5 0

00

25

20 29

• After the RR rotation, is the resulting tree an AVL search tree?

• Do Exercise 15.1 - see the solution on the Web http://www.cise.ufl.edu/~sahni/dsaac/

Page 25: AVL TREE LECTURE

Deletion from an AVL Search Tree

To delete an element from an AVL search tree, we can use Program 14.6

Deletion of a node may also produce an imbalance Imbalance incurred by deletion is classified into

the types R0, R1, R-1, L0, L1, and L-1 Rotation is also needed for rebalancing Read the observations after deleting a node from an AVL

search tree Read Section 15.1.6 for deletion from an AVL search tree

Page 26: AVL TREE LECTURE

An R0 Rotation

Figure 15.7 An R0 rotation (single rotation)

Page 27: AVL TREE LECTURE

An R1 Rotation

Figure 15.8 An R1 rotation (single rotation)

Page 28: AVL TREE LECTURE

An R-1 Rotation

Figure 15.9 An R-1 rotation (double rotation)

Page 29: AVL TREE LECTURE

Exercise & Reading

Do Exercise 15.5

- see the solution on the Web http://www.cise.ufl.edu/~sahni/dsaac/

READ Chapter 15.1