avl trees when bad trees happen to good programmers

36
AVL Trees When bad trees happen to good programmers.

Upload: jazmyn-van

Post on 01-Apr-2015

225 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: AVL Trees When bad trees happen to good programmers

AVL Trees

When bad trees happen to good programmers.

Page 2: AVL Trees When bad trees happen to good programmers

Good tree.

Page 3: AVL Trees When bad trees happen to good programmers

Bad tree.

Page 4: AVL Trees When bad trees happen to good programmers

Bad tree.

Left and right subtrees have the same height!

Page 5: AVL Trees When bad trees happen to good programmers

For every node in the tree, the left and right subtrees of that node have the same height.

Too rigid.

Page 6: AVL Trees When bad trees happen to good programmers

AVL Trees (Adelson-Velskii and Landis) Binary search trees with an additional

property. For every node in the tree, the height of

the left and right subtrees differ by at most 1.

Page 7: AVL Trees When bad trees happen to good programmers

AVL Property

If N is a node in a binary tree, node N has AVL property if the heights of the left sub-tree and right sub-tree are equal or if they differ by 1.

Page 8: AVL Trees When bad trees happen to good programmers

Height of a Tree

Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node.

Height of an empty tree is -1. Height of a single node tree is 0. Recursive definition: height(t) = 0 if number of nodes = 1

= -1 if T is empty = 1+ max(height(LT), height(RT))

otherwise

Page 9: AVL Trees When bad trees happen to good programmers

Checking Balance

0

0 0

00

0000

0 1 1 1

1

1

12

22

3

3

4

5

Page 10: AVL Trees When bad trees happen to good programmers

Examples4

6

5 8

7 9

3

2

7

9

8

4 6

5

3

3

2

6 8

7

5

4

8

6

16 23

18

12

94 5

Page 11: AVL Trees When bad trees happen to good programmers

Computing Height

height( T:bin_tree ){ if T = null then return -1 HL = height( T->Left) HR = height( T->Right) H = max( HL, HR ) + 1

return H}

Page 12: AVL Trees When bad trees happen to good programmers

Closer Inspection of “Balance” A tree is balanced if for every node in the

tree, the height of the left and right subtrees differ by at most 1.

A node is balanced if The left and right subtrees are balanced and The height of the left and right subtrees differ by at

most 1

A tree is balanced if its root node is balanced.

Page 13: AVL Trees When bad trees happen to good programmers

Checking Balance( Height, Boolean ) balance_test( T:bin_tree ){ if T = null then return (-1, true) ( HL, BL ) = balance_test( T->Left) ( HR, BR ) = balance_test( T->Right) H = max( H_L, H_R ) + 1 if (abs(HL-HR)<=1 && BL=true && BR=true) then B=true else B=false

return ( H, B )}

Page 14: AVL Trees When bad trees happen to good programmers

Operations Find, FindMin, FindMax

O(log N) time Insert

Need to maintain balance O(log N) time

Delete a little Complicated

Page 15: AVL Trees When bad trees happen to good programmers

For starters…

binary search tree permutations of 1, 2, 3

Page 16: AVL Trees When bad trees happen to good programmers

Details

Balance Factor the difference between the height of a

node’s left subtree and the height of its right subtree

Height-Balanced Trees – all of its nodes have a balance factor of 1, 0, or -1.

Page 17: AVL Trees When bad trees happen to good programmers

Examples

BF = 0

BF = -1

BF = -2

Page 18: AVL Trees When bad trees happen to good programmers

Examples

BF = 0

BF = 1

BF = 2

Page 19: AVL Trees When bad trees happen to good programmers

Inserting a value into an AVL tree

1. Follow the insertion path (if < go left, otherwise go right).

2. Remember the deepest node with a balance factor of +1 or -1.

(This is called the pivot node.)3. Insert the node at the appropriate point.

Page 20: AVL Trees When bad trees happen to good programmers

4. Recompute balance factors from the pivot node on down, including the pivot node.

5. Has the absolute value of the pivot node’s balance factor increased from 1 to 2?

Inserting a value into an AVL tree

Page 21: AVL Trees When bad trees happen to good programmers

If yes, rebalance the tree!!!

Page 22: AVL Trees When bad trees happen to good programmers

Rebalancing the tree

This has the visual effect of rotating the subtree of the pivot node.

This is also called an AVL rotation. (Betcha didn’t see that one coming! ☺)

Page 23: AVL Trees When bad trees happen to good programmers

AVL Balancing : Four Rotations

Single right

1

2

3

Page 24: AVL Trees When bad trees happen to good programmers

AVL Balancing : Four Rotations

Single right

3

2

1

2

1 3

1

2

3

Single left

2

3 1 3

2

1

Double right3

2 1

3

1 22

3

1

Double left

Page 25: AVL Trees When bad trees happen to good programmers

Let’s take a closer look at the cases

The one selected dependson the direction of the“Guilty” insertion,relative to the pivot node.

Page 26: AVL Trees When bad trees happen to good programmers

Case 1 The insertion that unbalanced the tree

occurred in the left subtree of the left child of the pivot node.

10

5 12

3 7

insert 1BF=1

BF=0BF=0

BF=0BF=0

pivotnode

Page 27: AVL Trees When bad trees happen to good programmers

Case 1

10

5 12

3 7

1

BF=1

BF=0BF=0

BF=0BF=0

BF=2

BF=1

BF=1

BF=0

Page 28: AVL Trees When bad trees happen to good programmers

Case 1 steps for AVL rotation

pivot’s left child becomes new pivot pivots left child retains its left subtree old pivot becomes the right child of new

pivot (it’s old left child) old pivot takes the right subtree of its

old left child (the new pivot now) and makes it the left subtree

old pivot retains its right subtree

Page 29: AVL Trees When bad trees happen to good programmers

Case 110

5 12

3 7

1

pivotnode

pivot’s left child becomes new pivot

Page 30: AVL Trees When bad trees happen to good programmers

Case 15

3

1

newpivotnode

pivots left child retains its left subtreepivot’s left child becomes new pivot

Page 31: AVL Trees When bad trees happen to good programmers

Case 15

3

1

newpivotnode

pivots left child retains its left subtreeold pivot becomes the right child of new pivot (it’s old left child)

10

Page 32: AVL Trees When bad trees happen to good programmers

Case 15

7

3

1

newpivotnode

old pivot becomes the right child of new pivot (it’s old left child)

10

old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree

Page 33: AVL Trees When bad trees happen to good programmers

Case 15

127

3

1

newpivotnode

10

old pivot takes the right subtree of its left child (the new pivot now) and makes it the left subtree

old pivot retains its right subtree

Page 34: AVL Trees When bad trees happen to good programmers

Case 2 The insertion that unbalanced the tree

occurred in the right subtree of the right child of the pivot node.

Case 3 The insertion that unbalanced the tree

occurred in the right subtree of the left child of the pivot node.

3 subcases

Page 35: AVL Trees When bad trees happen to good programmers

Case 4 The insertion that unbalanced the tree

occurred in the left subtree of the right child of the pivot node.

3 subcases

Page 36: AVL Trees When bad trees happen to good programmers

Parting thoughts on AVL trees Performance really depends on the

unreliability of the input data. i.e., you need almost sorted data to make it

worth it! A lot of design time is involved. A lot of work, too! Make sure you need it!