23
AVL Search Trees What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations.

AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

AVL Search Trees

• What is an AVL Tree?

• AVL Tree Implementation.

• Why AVL Trees?

• Rotations.

Page 2: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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.

3

1

2

10

13

7 -1

1

3

1

2

4

10

13

7 -1

1

-2 1-1 1

0 00

Not an AVL TreeAVL Tree 00

Page 3: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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();}// . . .

}

Page 4: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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(n).

• 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(log n).• Searching, insertion, and deletion are all O(log n).

Page 5: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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

45

40

78

50

-1

-2Insert 35

-1 -2

0

0 0

350 Deepest unbalanced node

Page 6: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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

-1

-2

45

40 78

50-1rotation

-2 0 00

350 0

350

Page 7: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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

Page 8: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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

Page 9: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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();

}

Page 10: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example)

Page 11: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 12: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 13: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 14: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 15: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 16: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 17: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 18: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 19: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Single Right Rotation Implementation (example) contd

Page 20: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

Double Right-Left Rotation

z

T4T3

y

T2

x

T1y

T3T2

z

T4

x

T1

z

T4T3

x

T1

y

T2

deepest unbalanced node

right rotation of y about z

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

Page 21: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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

Page 22: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

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 }

Page 23: AVL Search Trees• AVL Tree Implementation. • Why AVL Trees? • Rotations. What is an AVL Tree? • An AVL tree is a binary search tree with a height balance property: • For

BST ordering property after a rotation• A rotation does not affect the ordering property of a BST (Binary

Search Tree).

x

T1 T2

y

T3

y

T3T2

x

T1

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 < yx < y < T3 x < y < T3

• Similarly for a left rotation.