Você está na página 1de 37

AVL Trees

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

4 836

835

837

Ordered insertion takes too long for large sets.

Solution: Trees
4 1 6 7 9
O (logN) vs O (N) Binary Trees provide quick access to data

15

O(N2) O(N)

Why it matters

10

O(log N)

10

15

20

# Items

4 1 6

Trees: Balance
7 9 12 17
Consider an imbalanced Binary Search Tree. Search performance degrades back into a linked list.

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

Balance
9 4 1 6
But how does one measure balance?
If we had some way of keeping the tree in balance, our searches would be closer to O(log N)

12 7 17

Measuring Balance
1 2 3 4 5

4 1 6 7 9

Perhaps we measure the height...

9 4 7 6 12 17

1 12 17

but the height alone does not give us a indication of imbalance

Balance: The Key


4 1 6
Balance is a relative property, not an absolute measure...
Subtrees right child too tall for its sibling

9 4 1 7 6 12 17

7 9

12 17

as with all things.

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

Internal nodes are also roots of subtrees. If each nodes subtree has a similar height, the overall tree is balanced

9 4 1 6 7 12 17

Measuring Balance
Thus, the actual numerical height does not determine balance; rather, its the relative height of both subtrees.

9 4
H

9 12 17
H-2 H-1

1 6

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

Balance Factor
To measure the balance of a node, there are two steps: Record the node height (NH) --an absolute numeric measure Record the nodes 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 Height


Tree height is easy, Here are three trees.

H=1

H=3

9
Height? H=2

4 1 6 7

12 17

Node / Tree Heights for AVLs


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

12 0 17 0 0

Therefore: The Balance Factor of

12

is Ht of

17

minus (0)

= 1 (0) = +1

Balance Factor
Remember that balance is relative relative. So we define the Balance Factor (BF) at: BF = (right subtree height - left subtree height) 0 +1 -1

+1

12
0

4 1
0

17

7 6
0

Balance Factor
Each node has a balance factor. Remember, its based on (right ht - left ht).

9 4 1 6 7 12 17
0 +1

9 4 7

-1

12
-1

+1 0

1 6

17

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

Quiz Yourself
Calculate the balance factor for each node in each tree:

9 4 4 9 4 12 1 6 7 9 1 4

9 12 7 17 16

Quiz Yourself- Ans YourselfCalculate the balance factor for each node in each tree:
Here, the height is written inside the node, covering the key value it holds. Hopefully, this graphical clue helps.

2 1

-1 1

4 3 2 1

-3

4 2 1 1 3

2 -1

2 1 1

-1

2 1

Using Balance Factors


How do the Balance Factors help? Recall: Adelson-Velskii and Landis used a recursive definition of trees. Our BF measure +1 is likewise recursive.
0 0

9 4 7

-1

12
-1

+1 0

9 4 7

-2

1 6

17
Balanced

Not Balanced

If any BF is > +1 or < -1, theres imbalance

AVLs 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:

12
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:

8 4 6 10

16 17 2

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

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

A:
Rotate the tree.

Identifying Offending Scenarios


The key to fixing an unbalanced tree is knowing what caused it to break. Lets inductively look at what can cause a good tree to go bad.

Given an AVL balanced tree: Two possible left insertions could break balance: #1
Insertion to left subtree of left child

9 4
0

-1

-2 -1

#2

-2

9 4
0 +1

9 4 7
0
Insertion to right subtree of left child

#1
Insertion to left subtree of left child -1

-2 9 4 0

Patterns of Problems

#2
+1

-2 9 4 0 7 Insertion to right subtree of left child

and the mirror of these insertions #4

#3
Insertion to left subtree of right child

+2

+2

9 11 10
0

-1
Insertion to right subtree of right child

11

+1 0

13

#1
-2 Insertion to left subtree of left child 9 -1 4 0

#2

-2 9 4 0 7

Imbalance found regardless of height

+1

-2

Insertion to right subtree of left child

9
H H+1 H-1

matches

#3
+2 Insertion to left subtree of right child 9 11 -1 10 0

#4

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

+2

11 +1 Insertion to right subtree of right child 13 0

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.

#1
-2 Insertion to left subtree of left child 9 -1 4 0

#2

-2 9 4 0 7

Lets look at two insertions that cause imbalance. Recall they are mirrors.

+1

Insertion to right subtree of left child

#3
+2 Insertion to left subtree of right child 9 11 -1 10 0

They both deal with outside insertions on the trees exterior face: the left-left and leftrightright-right insertions.

#4
+2 9

11 +1 Insertion to right subtree of right child 13 0

Single Rotation
#1
Insertion to left subtree of left child

-2 -1

9 4
0

These insertions can be cured by swapping parent and child . . .

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

+2

9
Insertion to right subtree of right child

11

+1 0

13

#1
Insertion to left subtree of left child

Single Rotation: Right


-2 -1 0

9 4
0

4 1 9

1 X W

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

W X
C

A B

9 4 1 1 4

9 4 9 1

9 9

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

#4

Single Rotation: Left


+2 0

9
Insertion to right subtree of right child

11

+1 0

11 9

13

13

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

X W
A B C A

W X
B C

Word of caution
X W A

9 6
C

11 14

4 5

7
B

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

1
A

9 6
X C

4 3
B

11 14

#1
-2 Insertion to left subtree of left child 9 -1 4 0

#2

-2 9 4 0 7

Lets look at the other two insertions causing problems. Recall they are mirrors.

+1

Insertion to right subtree of left child

#3
+2 Insertion to left subtree of right child 9 11 -1 10 0

They both deal with inside insertions on the trees interior: rightright-left and left-right left-

#4
+2 9

11 +1 Insertion to right subtree of right child 13 0

Double Rotation
#2 These insertions are not solved with a single rotation. +1 -2

9 4 7
0
Insertion to right subtree of left child

#3
Insertion to left subtree of right child

+2

9 11 10
0

-1

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

Why Doesnt Single Rotation Work?

X W
C A B

Single Right Rotation Attempted A

W X
C B

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

#3

Double Rotation
+2 0

9
Insertion to left subtree of right child

-1

11 10 Y W X
0

10 9

11 X

(trivial case)

Y
D A B

W
C D

A B C

#4
Insertion to right subtree of left child

Double Rotation
-2

9
+1

0 0

4 7 Y
0

7 4 X 9

W X
D A B C A

Y
B D

Double Rotation
Y 7 W A 3 B 5 4 6 8 X D 9 11 14 A

Magic
W 3

9 X 4 5 B 6 Y 7 D 8 14 11

Step?

Solved: Move up X! W becomes Xs left child Y becomes Xs right child Ws right child becomes Xs left child Xs right child (if any) becomes Ys left child

Double Rotation Strategy


On a left-right insert, we first rotate left

9 4
D A B

SLR at 4

9 7 4
C D

SRR at 9

7 4
A B C

9
D

7
C A

Double left rotate = single left rot + single right rot

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

Double Rotation Strategy


9
A SRR at 11 A B B C C D

9 10 11

SLR at 9

10 9
A

11 10
D

11
B C D

Similarly, we perform a right and left rotation on insertions 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

Você também pode gostar