lecture 9 : balanced search trees

16
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Upload: christmas

Post on 31-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Lecture 9 : Balanced Search Trees. Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University. Balanced Binary Search Tree. In Binary Search Tree - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 9 : Balanced Search Trees

Lecture 9 : Balanced Search Trees

Bong-Soo Sohn

Assistant ProfessorSchool of Computer Science and Engineering

Chung-Ang University

Page 2: Lecture 9 : Balanced Search Trees

Balanced Binary Search Tree

In Binary Search Tree Average and maximum search times will be

minimized when BST is maintained as a complete tree at all times. : O (lg N)

If not balanced, the search time degrades to O(N)

Idea :Keep BST as balanced as possible

Page 3: Lecture 9 : Balanced Search Trees

AVL tree

"height-balanced“ binary tree the height of the left and right subtrees of

every node differ by at most one

3

2 5

6

4 7

3

5

2 9

1 4

3

12

14

7

8 11

10

Page 4: Lecture 9 : Balanced Search Trees

Non-AVL tree example

6

4 9

3 5 7

8

Page 5: Lecture 9 : Balanced Search Trees

Balance factor

BF = (height of right subtree - height of left subtree) So, BF = -1, 0 or +1 for an AVL tree.

0

0 0

-1

-1 0

0

+1

-1 -2

0+1

0

Page 6: Lecture 9 : Balanced Search Trees

AVL tree rebalancing

When the AVL property is lost we can rebalance the tree via one of four rotations Single right rotation Single left rotation Double left rotation Double right rotation

Page 7: Lecture 9 : Balanced Search Trees

Single Left Rotation (SLR)

when A is unbalanced to the left and B is left-heavy

A

B T3

T1

T2

B

T1

A

T2

T3

SLR at A

Page 8: Lecture 9 : Balanced Search Trees

Single Right Rotation (SRR)

when A is unbalanced to the right and B is right-heavy

A

T1 B

T2

T3

B

A T3

T1

T2

SRR at A

Page 9: Lecture 9 : Balanced Search Trees

Double Left Rotation (DLR)

When C is unbalanced to left And A is right heavy

C

A T4

T1

B

T2

T3

C

B T4

AT3

T1

T2

B

A C

T1 T2 T3 T4

SLR at A SRR at C

Page 10: Lecture 9 : Balanced Search Trees

Double Right Rotation (DRR)

When C is unbalanced to right And A is left heavy

A

T1

B

T2

C

T3

T4

B

A C

T1 T2 T3 T4

A

T1

C

BT4

T2

T3

SRR at C SRR at A

Page 11: Lecture 9 : Balanced Search Trees

Insertion in AVL tree

An AVL tree may become out of balance in two basic situations After inserting a node in the right subtree of the right

child After inserting a node in the left subtree of the right

child

Page 12: Lecture 9 : Balanced Search Trees

insertion

Insertion of a node in the right subtree of the right child

Involves SLR at node P

Page 13: Lecture 9 : Balanced Search Trees

insertion

Insertion of a node in the left subtree of the right child

Involves DRR :

Page 14: Lecture 9 : Balanced Search Trees

insertion

In each case the tree is rebalanced locally insertion requires one single or one

double rotation O(constant) for rebalancing Cost of search for insert is still O(lg N)

Experiments have shown that 53% of insertions do not bring the tree out of balance

Page 15: Lecture 9 : Balanced Search Trees

Deletion in AVL tree

Not covered here Requires O(lg N) rotations in worst case

Page 16: Lecture 9 : Balanced Search Trees

example

Given the following AVL tree, insert the node with value 9:

6

3 12

4 713

10