Docsity

AVL Trees: Balanced Binary Search Trees, Slides of Computer Science

Toggle sidebar
bg1
AVL Trees
•binary tree
•for every node x, define its balance factor
balance factor of x = height of left subtree of x
– height of right subtree of x
•balance factor of every node xis – 1,0, or 1
•log2(n+1) <= height <= 1.44 log2(n+2)
Example AVL Tree
0 0
00
1
0
-1 0
1
0
-1
1
-1
10
7
83
15
30
40
20
25
35
45
60
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download AVL Trees: Balanced Binary Search Trees and more Slides Computer Science in PDF only on Docsity!

AVL Trees

• binary tree

• for every node x, define its balance factor

balance factor of x = height of left subtree of x

  • height of right subtree of x

• balance factor of every node x is – 1, 0 , or 1

• log 2 (n+1) <= height <= 1.44 log 2 (n+2)

Example AVL Tree

0 0

(^0 )

1

0

-1 0

1 0

1

10 - 7 3 8 (^1 )

30

40

20 25

35

45 60

put(9)

0 0

(^0 )

1

0

-1 0

1 0

1

10 7 3 8 (^1 )

30

40

20 25

35

45 60

put(29)

0 0

(^0 )

1

0

0

1 0

1

10 - 7 3 8 (^1 )

30

40

20 25

35

45 60

29

0

-2^ -

RR imbalance => new node is in

right subtree of right subtree of

white node (node with bf = – 2 )

A-Node

• Let A be the nearest ancestor of the newly

inserted node whose balance factor becomes

+2 or –2 following the insert.

• Balance factor of nodes between new node

and A is 0 before insertion.

Imbalance Types

• RR … newly inserted node is in the right

subtree of the right subtree of A.

• LL … left subtree of left subtree of A.

• RL… left subtree of right subtree of A.

• LR… right subtree of left subtree of A.

LL Rotation

• Subtree height is unchanged.

• No further adjustments to be done.

Before insertion.

1 0

A

B

BL BR

AR

h h

h

A

B

B’L BR

AR

After insertion.

h+1 h

h

B

A

After rotation.

BR

h

AR

h

B’L

h+1 0

0 1

2

LR Rotation (case 1)

  • Subtree height is unchanged.
  • No further adjustments to be done.

Before insertion.

1 0

A

B

A

B

After insertion.

C

C

A

After rotation.

B

0

2 0 0

0

Single & Double Rotations

• Single

ƒ LL and RR

• Double

ƒ LR and RL

ƒ LR is RR followed by LL

ƒ RL is LL followed by RR

LR Is RR + LL

A

B

BL

C’R

AR

h h

h

CL h-

-1 C

2

After insertion.

A

C

CL

C’R

AR

h

h

BL h

B

2

After RR rotation.

h-

C

A

C’R

h

AR

h

B

BL

h

CL

h-

1 0

0

After LL rotation.

Remove An Element

0 0

(^0 )

1

0

-1 0

1 0

1

10 - 7 3 8 (^1 )

30

40

20 25

35

45 60

Remove 8.

Remove An Element

0 0

0

2

0

-1 0

1 0

1

10 - 7 3 (^1 )

30

40

20 25

35

45 60

• Let q be parent of deleted node.

• Retrace path from q towards root.

q

R0 Rotation

  • Subtree height is unchanged.
  • No further adjustments to be done.
  • Similar to LL rotation.

Before deletion.

1 0

A

B

BL BR

AR

h h

h

B

A

After rotation.

BR

h

A’R

h-

BL

h 1

A -

B

BL BR

A’R

After deletion.

h h

(^0) h-

2

R1 Rotation

  • Subtree height is reduced by 1.
  • Must continue on path to root.
  • Similar to LL and R0 rotations.

Before deletion.

1 1

A

B

BL BR

AR

h h-

h

B

A

After rotation.

BR

h-

A’R

h-

BL

h 0

A^0

B

BL BR

A’R

After deletion.

h h-

(^1) h-

2

R-1 Rotation

  • New balance factor of A and B depends on b.
  • Subtree height is reduced by 1.
  • Must continue on path to root.
  • Similar to LR.

1

A

B

BL

CR

AR

h-

h b CL

C

A

B

BL

CR

A’R

h-

h- CL

b C

2 C

A

CR A’R

h-

B

BL

h-

CL

0

Number Of Rebalancing Rotations

• At most 1 for an insert.

• O(log n) for a delete.