AVL Trees

Preview:

DESCRIPTION

Learn About AVL trees And rotations

Citation preview

AVL TreesAVL Trees

RecallRecall

Motivation:Motivation: Linked lists and queues work well enough for most applications, but provide slow service for large data sets.

835835835835 83783783783744443333

Ordered insertion takes too long for large sets.

836836836836

Solution: TreesSolution: Trees

4444

6666

7777

9999

1111

999944441111 6666 7777

Binary Trees provide quick access to data

O (logN)vs

O (N)

O (logN)vs

O (N)

0

5

105

10

15

15 20

O(log N)

O(N)

O(N2)

# Items

# S

tep

s

Why itmatters

Why itmatters

Trees: BalanceTrees: Balance4444

6666

7777

9999

1111

12121212

17171717

Consider animbalanced Binary Search Tree. Search performancedegrades back into a linked list.

The promisedO(log N) will approach O(N).

4444

6666

7777

9999

1111

12121212

17171717

BalanceBalance

If we had someway of keeping the tree “in balance”, our searches would be closer to O(log N)

But how does one measure balance?

4444

6666

7777

9999

1111

12121212

17171717

4444

6666

7777

9999

1111

12121212

17171717

1

2

3

4

5

Measuring “Balance”Measuring “Balance”

…but the height alone does notgive us a indication of imbalance

Perhaps wemeasurethe height...

Balance: The KeyBalance: The Key

4444

6666

7777

9999

1111

12121212

17171717

4444

6666

7777

9999

1111

12121212

17171717

Subtree’s rightchild too tall forits sibling

Balance is a relative property, not an absolute measure...

…as withall things.

Measuring “Balance”Measuring “Balance”Adelson-Velskii and Landis suggested that, since trees may be defined recursively, balance may also be determined recursively.

4444

6666

7777

9999

1111

12121212

17171717

Internal nodes are also roots of subtrees.

If each node’s subtree has a similar height, the overall tree is balanced

Measuring BalanceMeasuring Balance

4444

6666

7777

9999

1111

12121212

17171717

9999

HH-1

H-2

Thus, the actual numerical height does not determine balance; rather, it’s the relativerelative height of both subtrees.

Balanced Tree Test:Balanced Tree Test: subtrees differ in height by no more than one. This recursively holds true for all nodes!

Balance FactorBalance FactorTo measure the balance of a node, there are two steps:

Record the node height (NH)--an absolute numeric measure

Record the node’s balance factor (BF)--a relative measure

The node height alone does not indicate balance: we have to compare it to the height of other nodes.

Node / Tree HeightNode / Tree Height

9999 4444

6666

77771111

12121212

17171717

Tree height is easy, Here are three trees.

Height?Height?

H=1

H=2

H=3

STEP 1

STEP 1

Node / Tree Heights for AVLsNode / Tree Heights for AVLs

By convention, a null reference will return aheight of zero (0).

Therefore:The Balance Factor of 12121212

is Ht of minus (0)17171717

= 1 – (0) = +1

12121212

17171717

00

0

Balance FactorBalance Factor

Remember that balance is relativerelative.

So we define the Balance Factor (BF) at:

BF = (right subtree height - left subtree height)

4444

6666

777711119999 12121212

17171717

0

0

+1

-1

0

0

+1

STEP 2

STEP 2

Balance FactorBalance Factor

4444

6666

7777

9999

1111

12121212

171717174444

6666

7777

9999

1111

12121212

17171717

0

-10 0

+1+1

-1

Each node has a balance factor. Remember,it’s based on (right ht - left ht).

(We will note BF outside the node, to avoid confusion with the key values. Some texts don’t even give the keys, because values are irrelevant once you have a proper BST.)

Quiz YourselfQuiz Yourself

Calculate the balance factor for each node ineach tree:

4444

6666

7777

9999

1111

4444

16161616

7777

9999

1111

12121212

17171717

44449999

44449999

12121212

Quiz Yourself- AnsQuiz Yourself- AnsCalculate the balance factor for each node in each tree:

3333

1111

2222

4444

1111

2222

1111

1111

4444

1111

3333

2222

11112222

11112222

1111

-1

0 -1

1

-3

-1

2

1

Here, the height is written inside the node, covering the key value it holds.

Hopefully, this graphical clue helps.

Using Balance FactorsUsing Balance Factors

How do the Balance Factors help?

Recall: Adelson-Velskii and Landisused a recursive definitionof trees. Our BF measureis likewiserecursive.

If any BF is > +1 or < -1, there’s imbalance

4444

6666

7777

9999

1111

12121212

17171717

0

-10 0

+1+1

-1

4444

7777

9999

111100

0

-2

Balanced

NotBalanced

AVL’s “Weak” BalancingAVL’s “Weak” Balancing• A fully balanced tree guarantees that, for nodes on each level, all

left and right subtrees have the same height, and imposes this recursively.

• AVL trees use a weaker balance definition which still maintains the logarithmic depth requirement:

– for any node in an AVL tree, the height of the left and right nodes differs by at most 1.

• For example, this is a valid AVL tree, but not strictly balanced:

8888

6666

10101010

12121212

4444

16161616

1717171722

Addressing ImbalanceAddressing Imbalance

If we build a tree from scratch, and check nodes for imbalance after a new insertion, we can guard against imbalance.

Q:Q: So what do we do when the tree is unbalanced?

A:A: Rotate the tree.

Identifying Offending ScenariosIdentifying Offending ScenariosThe key to fixing an unbalanced tree is knowing what caused it to break. Let’s inductively look at what can cause a good tree to go bad.

Given an AVL balanced tree:

44449999 -1

0Two possible left insertions could break balance:

Insertion toleft subtreeof left child

0

44449999

1111

-1

-2#1

Insertion toright subtreeof left child

0

4444

7777

9999+1

-2#2

PatternsPatternsof Problemsof ProblemsInsertion to

left subtreeof left child 4444

9999

1111

-1

-2

0

#1

Insertion toright subtreeof left child

4444

7777

9999

+1

-2

0

#2

#3

Insertion toleft subtreeof right child

111111119999

10101010

-1

+2

0

… and the mirror of these insertions

111111119999

13131313

+1

+2

0

Insertion toright subtreeof right child

#4

11111111

9999

13131313

+1

+2

0

Insertion toright subtreeof right child

#4#3

Insertion toleft subtreeof right child

11111111

9999

10101010

-1

+2

0

Insertion toleft subtreeof left child 4444

9999

1111

-1

-2

0

#1

Insertion toright subtreeof left child

4444

7777

9999

+1

-2

0

#2

Given our recursive definition of trees, we know these four scenarios are complete. The same insertion into a larger AVL balanced tree does not create a new category.

H

4444

9999

H-1

H+1

-2

Imbalance foundImbalance foundregardless of heightregardless of height

matchesmatches

(H-1) - (H+1) = -2

11111111

9999

13131313

+1

+2

0

Insertion toright subtreeof right child

#4#3

Insertion toleft subtreeof right child

11111111

9999

10101010

-1

+2

0

Insertion toleft subtreeof left child 4444

9999

1111

-1

-2

0

#1

Insertion toright subtreeof left child

4444

7777

9999

+1

-2

0

#2

Let’s look at two Let’s look at two insertions thatinsertions that

cause imbalance. cause imbalance. Recall they are Recall they are

mirrors.mirrors.

They both deal with They both deal with “outside” insertions “outside” insertions on the tree’s exterior on the tree’s exterior face: the left-left and face: the left-left and right-right insertions.right-right insertions.

Single RotationSingle Rotation

111111119999

13131313

+1

+2

0

Insertion toright subtreeof right child

#4

Insertion toleft subtreeof left child

0

44449999

1111

-1

-2#1These insertions can be cured by swapping parent and child . . .

. . . provided you maintain search order. (An AVL tree is a BST)

Single Rotation: RightSingle Rotation: Right

Insertion toleft subtreeof left child 0

44449999

1111

-1

-2#1

04444

999911110

0

A single rotation round the A single rotation round the unbalanced node restores AVL unbalanced node restores AVL balance in all casesbalance in all cases

XXXX

WWWW

AB

A

C

WWWW

XXXX

BA C

9999

Think of rotations - Imagine the joints at the ‘4’ node starting to flex and articulate.

4444

1111 9999

9999

44449999

1111 9999

4444

1111

#4

011111111

1313131399990

0

111111119999

13131313

+1

+2

0

A similar rotation is used for right-rightinsertions that cause an imbalance

Insertion toright subtreeof right child

Single Rotation: LeftSingle Rotation: Left

XXXXWWWW

AB

C

WWWW

XXXX

CBA

1111

Word of cautionWord of caution

The single rotations are simple, but you have to keep track of child references.

33334444

11116666

7777

999911111111

14141414

5555

X

W

CB

A

44446666

33337777

5555

999911111111

14141414W

AB

C

X

11111111

9999

13131313

+1

+2

0

Insertion toright subtreeof right child

#4#3

Insertion toleft subtreeof right child

11111111

9999

10101010

-1

+2

0

Insertion toleft subtreeof left child 4444

9999

1111

-1

-2

0

#1

Insertion toright subtreeof left child

4444

7777

9999

+1

-2

0

#2

Let’s look at theLet’s look at theother two insertionsother two insertionscausing problems. causing problems.

Recall they are Recall they are mirrors.mirrors.

They both deal with They both deal with “inside” insertions on “inside” insertions on

the tree’s interior:the tree’s interior:right-left and left-rightright-left and left-right

Insertion toright subtreeof left child

0

4444

7777

9999+1

-2#2

#3

Insertion toleft subtreeof right child

111111119999

10101010

-1

+2

0

Double RotationDouble Rotation

These insertions are not solved with a single rotation.

Instead, two single rotations, left-right and right-left are performed.

Why Doesn’t Single Rotation Work?Why Doesn’t Single Rotation Work?

WWWWXXXX

AB

C

XXXX

WWWW

A

BC

SingleSingleRightRight

RotationRotationAttemptedAttempted

Unbalanced insertions into the “interior” of the tree remain unbalanced with only a single rotation

#3

Insertion toleft subtreeof right child

111111119999

10101010

-1

+2

0

Double RotationDouble Rotation

YYYY WWWW

A B C

XXXX

D

010101010

1111111199990

0

(trivial case)

XXXX

A

B C

YYYY

D

WWWW

#4

Insertion toright subtreeof left child

Double RotationDouble Rotation

WWWW YYYY

A B C

XXXX

D

07777

999944440

0

0

4444

7777

9999+1

-2

WWWWXXXX

AB C

YYYY

D

MagicMagic

Step?Step?

Double RotationDouble Rotation

5555

4444

7777

3333

8888

6666

9999

11111111

14141414W

A

B

D

Y

4444

6666

3333

7777

5555

9999

11111111

14141414W

A

B

X

8888D

X

Y

Solved: Move up X! W becomes X’s left child Y becomes X’s right child W’s right child becomes X’s left child X’s right child (if any) becomes Y’s left child

Double Rotation StrategyDouble Rotation Strategy

4444

7777

9999

A

B C

D

44447777

9999

A B

C

D

SLRSLR

at 4at 4 44447777

9999

A B C D

SRRSRR

at 9at 9

On a left-right insert, we first rotate left

This places the tree in a position for which we already have a working solution. (Code reuse!)

Double left rotate =Double left rotate =single left rot + single right rotsingle left rot + single right rot

Double Rotation StrategyDouble Rotation Strategy

11111111

10101010

9999

A

B C

D

10101010

11111111

9999

A

B

C D

SRRSRR

at 11at 11 999910101010

11111111

A B C D

SLRSLR

at 9at 9

Similarly, we perform a right and left rotation oninsertions that went left-right. The first rotation reforms the tree into an exterior imbalance. We already have a way of fixing this: single left rotations

Recommended