Top Banner
Balanced Search Trees Compiled by : Surya Prakash Pandit 232/05 Gaurav Gupta 20/05 IIIrd I.T.
52
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AVL Trees

Balanced Search Trees

Compiled by : Surya Prakash Pandit 232/05 Gaurav Gupta 20/05IIIrd I.T.

Page 2: AVL Trees

AVL Trees 2

Binary Search Trees (reminder)

Each tree node contains a value. For every node,

its left subtree contains smaller values, and its right subtree contains larger values.

The time complexity of a search operation is proportional to the tree’s depth. The good case: If the tree is balanced, then every operation

takes O(logn) time. The bad case: The tree might get very unbalanced.

For example, when inserting ordered numbers to the tree, the resulting height will be exactly n.

Page 3: AVL Trees

AVL Trees 3

Balanced Search Trees : Balanced Tree: A binary tree in which the heights of the two

sub trees of every node differ by at most 1.

Several Varieties AVL trees Red-Black trees B-Trees (used for searching secondary memory)

Page 4: AVL Trees

AVL Trees 4

Balanced Search Trees (Contd…)

Nodes are added and deleted so that the height of the tree is kept under control.

Insertion and deletion has more work to do, but retrieval never has more than log2 n because height is already controlled in case of retrieval.

Page 5: AVL Trees

AVL Trees 5

AVL Trees :Motivation: We want to guarantee O(log n) running time on searching/insertion/deletion operations.

Idea: Keep the tree balanced after each operation.

Solution: AVL (Adelson-Velskii and Landis) trees.

AVL tree property: For every node in the tree, the height of the left and right subtrees differs by at most 1.

Page 6: AVL Trees

AVL Trees 6

AVL Trees (Contd…) : a BST where each node has a balance factor

balance factor of a leaf node is 0 balance factor of any node:

Height (left subtree) – Height (right subtree)

Insertions or deletions can change the balance factor of one or more nodes.

if a balance factor becomes 2 or -2 the AVL tree must be rebalanced It is done by rotating the nodes

Page 7: AVL Trees

AVL Trees 7

Some AVL Trees :

0

00

-10

-1

-1

1

0

0

Balance Factor : height( left subtree ) - height( right subtree )

Balance value

Page 8: AVL Trees

AVL Trees 8

Depth of an AVL treeTheorem: Any AVL tree with n nodes has height less

than 1.441 log n.

Proof: Given an n-node AVL tree, we want to find an upper bound on the height of the tree.Fix h.

What is the smallest n such that there is an AVL tree of height h with n nodes?

Let Sh be the set of all AVL trees of height h that have as few nodes as possible.

Page 9: AVL Trees

AVL Trees 9

Depth of an AVL tree (contd…)Let wh be the number of nodes in any one of these trees.

w0 = 1, w1 = 2

Suppose T Sh, where h 2. Let TL and TR be T’s left and right sub trees. Since T has height h, either TL or TR has height h-1. Suppose it’s TR.

By definition, both TL and TR are AVL trees. In fact, TR Sh-1 or else it could be replaced by a smaller AVL tree of height h-1 to give an AVL tree of height h that is smaller than T.

Page 10: AVL Trees

AVL Trees 10

Depth of an AVL tree (contd…)Similarly, TL Sh-2.

Therefore, wh = 1 + wh-2 + wh-1 .

Claim: For h 0, wh h , where = (1 + 5) / 2 1.6.Proof: The proof is by induction on h.

Basis step: h=0. w0 = 1 = 0.

h=1. w1 = 2 > 1.Induction step: Suppose the claim is true for 0 m h, where h 1.

Page 11: AVL Trees

AVL Trees 11

Depth of an AVL tree (contd…)Then

wh+1 = 1 + wh-1 + wh

1 + h-1 + h (by the i.h.)= 1 + h-1 (1 + )= 1 + h+1 (1+ = 2)> h+1

Thus, the claim is true.From the claim, in an n-node AVL tree of height h,

n wh h (from the Claim)

h log n= (log n) / (log )< 1.441 log n

Page 12: AVL Trees

AVL Trees 12

AVL trees: During Runtime Search :takes O(log n) time, because height of the

tree is always O(log n). Insertion : O(log n) time because we do a search

(O(log n) time), and then we may have to visit every node on the path back to the root, performing up to 2 single rotations (O(1) time each) to fix the tree.

Deletion : O(log n) time.

Page 13: AVL Trees

AVL Trees 13

AVL Trees: Search, Insertion

AVL tree Search :is the same as BST Search.

AVL tree Insertion: same as BST insertion, except that we might have to fix the AVL tree after an insert.

These operations will take time O(d), where d is the depth of the node being found/inserted.

What is the maximum height of an n-node AVL tree?

Page 14: AVL Trees

AVL Trees 14

AVL Tree : Insertion Follow a search path as for a BST

Allocate a node and insert the item at the end of the path (as for BST) balance factor of new node is 0

if a balance factor becomes 2 or -2 perform a rotation to bring the AVL tree back in balanced form.

Page 15: AVL Trees

AVL Trees 15

Insertion (contd…) : Let x be the deepest node where an imbalance occurs.

Four cases to consider. The insertion is in the

1. Left sub tree of the Left child of x. (LL) Right Rotation

2. Right sub tree of the Left child of x. (LR) Left - Right

3. Left sub tree of the Right child of x. (RL) Right - Left

4. Right sub tree of the Right child of x. (RR) Left Rotation

Idea: LL & RR - Single rotation.

LR & RL - Double rotation.

Page 16: AVL Trees

LR16

Four Imbalance Cases :

Case 1: The left subtree is higher than the right subtree, and this is caused by the left subtree of the left child.

Case 2: The left subtree is higher than the right subtree, and this is caused by the right subtree of the left child.

Case 4:The symmetric case to case 1

Case 3:The symmetric case to case 2

k2

k1

BC

k1

k2

BA

A

C

k2

k1

RP

Q

k1

k2

PR

Q

LL RR

RL

Page 17: AVL Trees

AVL Trees 17

1) Simple Insertion :

0

-11

0

-1

1

0

0 0

01

0

-1

1

0

000

0

#'s are Balance Factors

No Rotation Required

0

Page 18: AVL Trees

AVL Trees 18

2

2) Insertion with Right Rotation :

0

01

0

-1

1

0

0 0

01

0

-1

0

0

00

0

0

1

Simple Right Rotation Required

0

2

0

Right Rotation

LL

Page 19: AVL Trees

AVL Trees 19

Example #

12

8 16

14104

62

1

k2

k1 C

A B

Right Rotation

12

8

16

14

10

4

6

2

1

k2

k1

C

A

B

LL

Page 20: AVL Trees

AVL Trees 20

3) Insertion with Left Rotation:

0

01

0

-1

0

0

00

0

0

0

0 0

-1

0

-10

0

1

0

-2

-1

-1

-1

0

Simple Left Rotation Required

Left Rotation

RR

Page 21: AVL Trees

AVL Trees 21

4)Insertion with Double Rotation:

0

-10

0

-1

1

0

00

0

1

-1

1

-2 Double Rotation Needed (RL) :

a). Right rotation around right subtree of the unbalanced subtree

b). Left Rotation around root of the unbalanced subtree

Page 22: AVL Trees

22

(i) The Right Rotation :

00

-2

-2

1

0

-11

00

0

-1-1

1

-2

1

0

01

0

Page 23: AVL Trees

23

(ii) The Left Rotation :

00

-2

-2

1

0

-11

00

0

0

0

1

1

0

-11

00

Page 24: AVL Trees

AVL Trees 24

In nutshell :

0

0

0

1

1

0

-11

000

-10

0

-1

1

0

00

0

1

-1

1

-2

On balancing

(RL Insertion)

Page 25: AVL Trees

AVL Trees 25

Example # 12

8 16

14104

62

5

k3

k1

D

A

B

k2

Left rotation on 4

k1

12

8 16

14106

4

5

k3

D

A B

k2

2

Right rotation on 8

12

6 16

1484

52 10

k3k1

DB

k2

A

(LR Insertion)

Page 26: AVL Trees

AVL Trees 26

Extended Example # 1 : Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16

3

2

1

Balancing by Right Rotation (LL)

4

5

Balancing by Left Rotation (RR)

Page 27: AVL Trees

AVL Trees 27

Extended Example # 1 : Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16

2

1 3

4

56

Page 28: AVL Trees

AVL Trees 28

Extended Example # 1 : Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16

2

1 4

53

Balancing node 2 by Left Rotation (RR)

67

Page 29: AVL Trees

AVL Trees 29

Extended Example # 1 : Insert : 3 , 2 , 1 , 4 , 5 , 6 , 7 , 16

4

2 5

63

Balancing node 5 by Left Rotation (RR)

1

716

Ans.

Page 30: AVL Trees

AVL Trees 30

Extended Example # 2 : Insert : 64 , 1 , 14 , 26 , 13 , 110 , 98 , 85

64

1

14

Since node 64 is imbalanced, thus by LR Insertion

Page 31: AVL Trees

9831

Extended Example # 2 : Insert : 64 , 1 , 14 , 26 , 13 , 110 , 98 , 85

64

1

14 2613 110

Page 32: AVL Trees

32

14

164

2613 110

98

85

Inserting 85 :

Node 110 becomes imbalanced.

Performing Right Rotation

(LL)

Ans.

Page 33: AVL Trees

AVL Trees 33

AVL Trees : Deletion AVL tree Deletion: same as BST deletion.

In event of an imbalance due to deletion, one or more rotation are needed to be applied to balance the AVL trees.

On deletion of a node X from the AVL tree let A be the closest ancestor node on the path from X to the root node, with balance factor = +2 or -2.

To restore balance the rotation is first classified as L or R depending on whether the deletion occurred on the left or right subtree of A

Page 34: AVL Trees

AVL Trees 34

Now depending on the value of BF(B) where B is the left or right subtree of A, the R or L imbalance is further classified as:

R0, R1 or R-1, L0, L1 or L-1.

The L and R rotations are the mirror images of each other. Thus, L0 R0 L1 R1 L-1 R-1

Here we are considering the Right Rotations, Left Rotations can be done in the similar way.

Deletion : (contd…)

Page 35: AVL Trees

AVL Trees 35

60

1) Deletion by R(0) Rotation : Delete node 60 Deletion is to be done on the right side of

root node A(46) and BF(B)=0

Delete 60

23

20

18

7

46

24

60

54

23

20

18

7

46

24

(+1)

(0)

(+2)

(0)

A

B

54

A

B

6023

20

18

724

Right Rotation R(0)20

18

7

46

23

24

A

B A

B(+2)

(0)

(-1)

(+1)

46

54

54

Page 36: AVL Trees

AVL Trees 36

2) Deletion by R(1) Rotation : Delete node 39 Deletion is to be done on the Right side of

root node A(37) and BF(B)=+1

41

37

(+1)

28

26

18

16

(+1)

39

41

37

(+1)

28

26

18

16

(+1)

39

Delete 39

A

B

A

B

41

28

26

18

16

(+2)

(+1)B

A

Right Rotation R(1)

39

37B

37

26

18

16

(0)

28 41

(0)A

Page 37: AVL Trees

AVL Trees 37

3) Deletion by R(-1) Rotation : Delete node 52 Deletion is to be done on the Right side of

root node A(44) and BF(B)=-1

52

4822

18

44

(+1)

(-1)

28

2329

A

B

52

4822

18

44

(+1)

(-1)

28

2329

A

BDelete 52

52

4822

18

44

(+1)

(-1)

28

2329

A

22

1823

44

2948

28Right Rotation R(-1)

(0) (0)AB

B

Page 38: AVL Trees

AVL Trees 38

120

128

85(0)

(0)

(0)

(-1) (0)

(+1)

Deletion Examples : Delete elements 120,64,130 from the

following search tree.

13598

64

26

110

(-1)(0)

130

(0) 99(0)

Page 39: AVL Trees

AVL Trees 39

Delete 120 :

120

128

85(0)

(0) (-1) (0)

(+1)

13598

64

26

110

(-1)(0)

130

(0) 99(0)

Ans.

Page 40: AVL Trees

AVL Trees 40

99

Delete 64 :

128

85

(0) (-1) (0)

(+1)

13598

64

26

110

(-1)(0)

130

(0) (0)(0)

(1)

Ans.

Page 41: AVL Trees

So we Follow R(0) Rotation.

(+1)

Delete 130 :

(+2)

41

85

99

128

(1) (-1) (0)

13598

26

110

(-1)(0)

130

(0)(0)

Node 128 becomes unbalanced BF(128)=+2

Page 42: AVL Trees

So we Follow R(0) Rotation.

(-1)98

42

85

99

Delete 130 :

128

(1) (-1)

(+2)

13598

26

110

(-1)(0)

(0)(0)

Node 128 becomes unbalanced BF(128)=+2

99

(0) (-1)(0)

12885

26 110

(+1)(+1)

(0)

135

R(0) Rotation

Ans.

Page 43: AVL Trees

AVL Trees 43

Example : Insertion + DeletionQues 1(a). Insert the following keys in order

shown to construct an AVL search treeA, B, C, D, E

Ques 1(b). Then Delete the last two keys in order of Last in First out.

Soln.

Page 44: AVL Trees

AVL Trees 44

Solution. Insert A, B

A

B

Page 45: AVL Trees

AVL Trees 45

Solution. Now Insert C

A

B

C

Page 46: AVL Trees

AVL Trees 46

Solution. Now Insert C

A

B

C

The tree is unbalanced at A

RR Rotation

B

CA

Page 47: AVL Trees

AVL Trees 47

Solution. Now insert D

B

CA

D

Page 48: AVL Trees

AVL Trees 48

Solution. Now insert E

B

CA

D

E

The tree is unbalanced at B and C

Page 49: AVL Trees

AVL Trees 49

B

D

E

By Left Rotation

A

C

Solution.

RR

Page 50: AVL Trees

AVL Trees 50

Delete E :

Solution.

B

D

E

A

C

Page 51: AVL Trees

AVL Trees 51

Delete D :

Solution.

B

DA

CAns.

Page 52: AVL Trees

AVL Trees 52

Special Thanks to:

Prof. Vinay Kumar Pathak

HOD Of CSE Department

HBTI, Kanpur.