lecture 13 avl trees king fahd university of petroleum & minerals college of computer science...

38
Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Post on 19-Dec-2015

219 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Lecture 13

AVL Trees

King Fahd University of Petroleum & MineralsCollege of Computer Science & Engineering

Information & Computer Science Department

Page 2: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

AVL Search Trees

What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations. Insertion into an AVL tree Deletion from an AVL tree

2

Page 3: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

What is an AVL Tree?

An AVL tree is a binary search tree with a height balance property: • For each node v, the heights of the subtrees of v differ by at most 1.

A subtree of an AVL tree is also an AVL tree. For each node of an AVL tree:

Balance factor = height(right subtree) - height(left subtree)

An AVL node can have a balance factor of -1, 0, or 1. Determine whether the trees below are AVL trees or not.

3

1

2

4

10

13

7

3

1

2

10

13

7

3

Page 4: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

AVL Trees Implementation

public class AVLTree extends BinarySearchTree{ protected int height; public AVLTree(){ height = -1;}

public int getHeight(){ return height } ;

protected void adjustHeight(){ if(isEmpty()) height = -1; else height = 1 + Math.max(left.getHeight() , right.getHeight()); }

protected int getBalanceFactor(){ if( isEmpty()) return 0; else return right.getHeight() - left.getHeight(); } // . . .}

4

Page 5: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Why AVL Trees?

Insertion or deletion in an ordinary Binary Search Tree can cause large imbalances.

In the worst case searching an imbalanced Binary Search Tree is O( )

An AVL tree is rebalanced after each insertion or deletion.• The height-balance property ensures that the height of

an AVL tree with n nodes is O( ).• Searching, insertion, and deletion are all O( ).

5

n

log n

log n

Page 6: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

What is a Rotation?

A rotation is a process of switching children and parents among two or three adjacent nodes to restore balance to a tree.

An insertion or deletion may cause an imbalance in an AVL tree.

The deepest node, which is an ancestor of a deleted or an inserted node, and whose balance factor has changed to -2 or +2 requires rotation to rebalance the tree.

45

40

78

50 -1

0

-1 0 45

40

78

50-2

-1

-2 0

350

Insert 35

Deepest unbalanced node

6

Page 7: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

What is a Rotation? (contd.)

There are two kinds of single rotation:

Right Rotation. Left Rotation.

A double right-left rotation is a right rotation followed by a left rotation.

A double left-right rotation is a left rotation followed by a right rotation.

45

40

78

50-2

-1

-2 0

350

45

40 78

50-1

0

0

0

350

rotation

7

Page 8: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation

Single right rotation:• The left child x of a node y becomes y's parent.• y becomes the right child of x.

• The right child T2 of x, if any, becomes the left child of y.

x

T1 T2

y

T3

deepest unbalanced node a right rotation of x about y

y

T3T2

x

T1

Note: The pivot of the rotation is the deepest unbalanced node

8

Page 9: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Left Rotation

Single left rotation:• The right child y of a node x becomes x's parent.• x becomes the left child of y.

• The left child T2 of y, if any, becomes the right child of x.

y

T3T2

x

T1

deepest unbalanced node a left rotation of y about x

x

T1 T2

y

T3

Note: The pivot of the rotation is the deepest unbalanced node

9

Page 10: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation

protected void rightRotate(){ if( isEmpty()) throw new InvalidOperationException(); BinaryTree temp = right; right = left; left = right.left; right.left = right.right; right.right = temp; Object tmpObj = key; key = right.key; right.key = tempObj; getRightAVL().adjustHeight(); adjustHeight();}

10

Page 11: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example)

11

Page 12: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

12

Page 13: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

13

Page 14: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

14

Page 15: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

15

Page 16: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

16

Page 17: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

17

Page 18: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

18

Page 19: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

19

Page 20: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Single Right Rotation Implementation (example) contd

20

Page 21: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Double Right-Left Rotation

z

T4T3

y

T2

x

T1y

T3T2

z

T4

x

T1

z

T4T3

x

T1

y

T2

right rotation of y about z

deepest unbalanced node

left rotation of Y about XNote: First pivot is the right child of the deepest unbalanced node; second pivot is the deepest unbalanced node

21

Page 22: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Double Left-Right Rotation

w

T2 T3

v

T1

x

T4v

T1 T2

w

T3

x

T4

x

T4T3

v

T1

w

T2

left rotation of w about v

deepest unbalanced node

left rotation of W about XNote: First pivot is the left child of the deepest unbalanced node; second pivot is the deepest unbalanced node

22

Page 23: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Double Rotation implementation

1 protected void rotateRightLeft()2 {3 if( isEmpty())4 throw new InvalidOperationException();5 getRightAVL().rotateRight();6 rotateLeft();7 }

1 protected void rotateLeftRight()2 {3 if( isEmpty())4 throw new InvalidOperationException();5 getLeftAVL().rotateLeft();6 rotateRight();7 }

23

Page 24: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

BST ordering property after a rotation

A rotation does not affect the ordering property of a BST (Binary Search Tree).

y

T3T2

x

T1

x

T1 T2

y

T3

a right rotation of x about y

BST ordering property requirement: BST ordering property requirement:

T1 < x < y T1 < x < y

x < T2 < y Similar x < T2 < y

x < y < T3 x < y < T3

• Similarly for a left rotation.

24

Page 25: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion

Insert using a BST insertion algorithm. Rebalance the tree if an imbalance occurs. An imbalance occurs if a node's balance factor changes from -1

to -2 or from+1 to +2. Rebalancing is done at the deepest or lowest unbalanced

ancestor of the inserted node.

There are three insertion cases:1. Insertion that does not cause an imbalance.

2. Same side (left-left or right-right) insertion that causes an imbalance. Requires a single rotation to rebalance.

3. Opposite side (left-right or right-left) insertion that causes an imbalance. Requires a double rotation to rebalance.

25

Page 26: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion: case 1

Example: An insertion that does not cause an imbalance.

Insert 14

26

Page 27: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion: case 2

Case 2a: The lowest node (with a balance factor of -2) had a taller left-subtree and the insertion was on the left-subtree of its left child.

Requires single right rotation to rebalance.

Insert 3

right rotation, with node 10 as pivot

-2

-1

27

Page 28: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion: case 2 (contd)

Case 2b: The lowest node (with a balance factor of +2) had a taller right-subtree and the insertion was on the right-subtree of its right child.

Requires single left rotation to rebalance.

Insert 45left rotation, with node 30 as the pivot

+2

+1

28

Page 29: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion: case 3

Case 3a: The lowest node (with a balance factor of -2) had a taller left-subtree and the insertion was on the right-subtree of its left child.

Requires a double left-right rotation to rebalance.

Insert 7

left rotation, with node 5 as the pivot

right rotation, with node 10 as the pivot

-2

+1

29

Page 30: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion: case 3 (contd)

Case 3b: The lowest node (with a balance factor of +2) had a taller right-subtree and the insertion was on the left-subtree of its right child.

Requires a double right-left rotation to rebalance.

Insert 15

right rotation, with node 16 as the pivot

left rotation, with node 9 as the pivot

+2

-1

30

Page 31: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

AVL Rotation Summary

-1

-2

+1

-2

-1 +1

+2+2

Single right rotation

Double left-right rotation

Single left rotation

Double right-left rotation

31

Page 32: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion Implementation

The insert method of the AVLTree class is:

Recall that the insert method of the BinarySearchTree class is:

public void insert(Comparable comparable){ if(isEmpty()) attachKey(comparable); else { Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) throw new IllegalArgumentException("duplicate key"); else if (comparable.compareTo(key)<0) getLeftBST().insert(comparable); else getRightBST().insert(comparable); }}

public void insert(Comparable comparable){ super.insert(comparable); balance();}

32

Page 33: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion Implementation (contd)

The AVLTree class overrides the attachKey method of the BinarySearchTree class:

public void attachKey(Object obj) { if(!isEmpty()) throw new InvalidOperationException(); else { key = obj; left = new AVLTree(); right = new AVLTree(); height = 0; } }

33

Page 34: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Insertion Implementation (contd)

protected void balance(){ adjustHeight(); int balanceFactor = getBalanceFactor(); if(balanceFactor == -2){ if(getLeftAVL().getBalanceFactor() < 0) rotateRight(); else rotateLeftRight(); } else if(balanceFactor == 2){ if(getRightAVL().getBalanceFactor() > 0) rotateLeft(); else rotateRightLeft(); }}

34

Page 35: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Deletion

Delete by a BST deletion by copying algorithm. Rebalance the tree if an imbalance occurs. There are three deletion cases:

1. Deletion that does not cause an imbalance.2. Deletion that requires a single rotation to rebalance.3. Deletion that requires two or more rotations to rebalance.

Deletion case 1 example:

Delete 14

35

Page 36: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Deletion: case 2 examples

Delete 40right rotation, with node 35 as the pivot

36

Page 37: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Deletion: case 2 examples (contd)

Delete 32left rotation, with node 44 as the pivot

37

Page 38: Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Deletion: case 3 examples

Delete 40

0

right rotation, with node 35 as the pivot

right rotation, with node 30 as the pivot

38