Top Banner
AVL Trees CSC 172 SPRING 2002 LECTURE 17
51

AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Dec 21, 2015

Download

Documents

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 CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AVL Trees

CSC 172

SPRING 2002

LECTURE 17

Page 2: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

A PROBLEM WITH BSTs

Common operations on balanced BST are O(log(n))

Alas, when the tree goes out of balance, performance degrades (worst case : chain O(n))

There are several data structures that modify the BST to maintain balance.

Page 3: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AVL Trees

The first balanced binary search tree

Named after discoverers Adelson-Velskii and Landis.

DEFINITION:An AVL tree is a binary search tree with the additional

balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. (Height of an empty subtree is –1).

Page 4: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

12

8 16

2 6

4 10 14

AN AVL TREE

What if we insert “7”?-1 -1 -1 -1

-1 -1 -1 -1

-1

0 0

001

12What is the height?

3

Page 5: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

12

8 16

2 6

4 10 14

NOT AN AVL TREE

-1 -1

-1 -1

-1 -1 -1 -10 1

002

13

4

-1

7

-1 0

Out of balance

Page 6: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AVL THEOREMAn AVL tree of n element has height O(log n)

In fact, An AVL tree of height H has at least FH+3 –1 nodes, where Fi is the ith Fibonacci number

Let SH be the size of smallest AVL tree of height H.

Clearly S0=1 and S1=2

SH must have subtrees of height H-1 and H-2These subtrees must have fewest nodes for height

So, SH = SH-1 + SH-2 + 1Minimum number AVL trees are Fibonacci trees

Page 7: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Smallest AVL Tree of height H

SH-

1

H-1SH-

2

H-2

H

Page 8: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

SH = FH+3 – 1

FH+3 – 1 >= (3/2)H // do this as exercise

Hint: (3/2)H-1+(3/2)H=(3/2)H-2(3/2 + 1) > (3/2)H-2(9/4)

SH >= (3/2)H

Log2(SH) >= H Log2(3/2)

H <= 1.75 Log2(SH)

Page 9: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Adds can unbalance a BST

Only nodes on the path from the root to the insertion point can have their balances altered

If we restore the unbalance node, we balance the tree

Page 10: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

12

8 16

2 6

4 10 14

AN AVL TREE

What if we insert “15”?

-1 -1 -1 -1

-1 -1 -1 -1

-1

0 0

001

12

3

If we insert 7We unbalance the whole tree

Page 11: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

12

8 16

2 6

4 10 14

AN AVL TREE

What if we insert “15”?

-1 -1 -1 -1

-1 -1 -1

-1

0 0

101

22

3

If we insert 7We unbalance the whole tree

15-1 -1

0

Page 12: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

4 cases

1. Insertion in left sub-tree of left child

2. Insertion in right sub-tree of left child

3. Insertion in left sub-tree of right child

4. Insertion in right sub-tree of right child

1 & 4 are symmetric

2 & 3 are symmetric

Page 13: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 1 & 4

A B

C

k1

k2Insertion extends Tree ‘A’

HH-2

H-2 H-2

Page 14: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 1 & 4

A B

C

k1

k2Insertion extends Tree ‘A’

H-2

H-1 H-2

“Rotation” fixesbalance

Page 15: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 1 & 4

AB C

k2

k1Insertion extends Tree ‘A’

H-2H-1 H-2

“Rotation” fixesbalance

H

Page 16: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

A B

C

k1

k2Insertion extends Tree ‘B’

HH-2

H-2 H-2

Page 17: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

A B

C

k1

k2Insertion extends Tree ‘B’

H-2

H-2 H-1

Page 18: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

AB C

k2

k1Insertion extends Tree ‘B’

H-2H-2 H-1

“Rotation” Does not fixbalance

Page 19: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Double Rotation

If x is out of balance for cases 2&3

1. Rotate between X’s child and grandchild

2. Rotate between X and it’s new child

Page 20: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

A B

C

k1

k2Insertion extends Tree ‘B’

HH-2

H-2 H-2

Page 21: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

A C

D

k1

k2Insertion extends Tree ‘B’ or ‘C’

HH-2

H-2 H-3

k3

B

Page 22: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

A C

D

k1

k2Insertion extends Tree ‘B’ or ‘C’

H-2

H-2 H-2

k3

B

Rotate grandchildWith child

Page 23: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

AC

D

k3

k2Insertion extends Tree ‘B’ or ‘C’

H-2

H-2

H-2

k1

B

Rotate grandchildWith child

Rotate X with new child

Page 24: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Cases 2 & 3

A C D

k2

k3Insertion extends Tree ‘B’ or ‘C’

H-2

H-2

H-2

k1

B

Rotate grandchildWith child

Rotate X with new child

Page 25: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Implementation

Insert

Fixup

Rotate

Need to keep track of balance

Page 26: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Rotationprivate void rotateLeft(Entry p) {

Entry r = p.right;p.right = r.left;if (r.left != null) r.left.parent = p;r.parent = p.parent;if (p.parent == null) {root = r; r.parent = null;}else if (p.parent.left == p) p.parent.left = r;else p.parent.right = r;r.left = p;p.parent = r;

}

Page 27: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

element left right parentroot

50

80

Page 28: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

element left right parentroot

50

80

90

r

Entry r = p.right;

p

Page 29: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

element left right parentroot

50

80

90

r

Entry r = p.right;p.right = r.left;

p

Page 30: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

element left right parentroot

50

80

90

r

Entry r = p.right;p.right = r.left;if (r.left != null) r.left.parent = p;if (p.parent == null)root = r; r.parent = p.parent;

p

Page 31: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

element left right parentroot

50

80

90

r

Entry r = p.right;p.right = r.left;if (r.left != null) r.left.parent = p;r.parent = p.parent;if (p.parent == null) root = r;else if …else …r.left = p;

p

Page 32: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

element left right parentroot

50

80

90

r

Entry r = p.right;p.right = r.left;if (r.left != null) r.left.parent = p;r.parent = p.parent;if (p.parent == null){ root = r; ..}else if …else …r.left = p;p.parent = r;

p

Page 33: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Node (Entry) classprivate static class Entry {

Object element;char balanceFactor = ‘=‘; // new nodes are balanced// we could set this to R or L indicating child with > heightEntry left = null, right = null, parent;Entry (Object element, Entry parent) {

this.element = element;this.parent = parent;

}}

Page 34: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

50

R

20

L

10

=

80

R

70

=

100

=

92

=

50

=

Page 35: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

public boolen add(Object o){

if (root == null) {

root = new Entry(o,null);

size++;

return true;

}// empty tree

else {

Entry temp = root,

ancestor = null; // we keep track of nearest unbalanced ancestor

int comp;

while (true) {

comp = ((Comparable)o).compareTo(temp.element);

if (comp == 0) return false;

Page 36: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

if (comp < 0) {

if (temp.balanceFactor != ‘=‘) ancestor = temp;

if (temp.left != null) temp = temp.left;

else {

temp.left = new Entry(o,temp);

fixAfterInsertion(ancestor,temp.left);

size++;

}

}// comp < 0

Page 37: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

else { // comp > 0

if (temp.balanceFactor != ‘=‘) ancestor = temp;

if (temp.right != null) temp = temp.right;

else {

temp.rig = new Entry(o,temp);

fixAfterInsertion(ancestor,temp.right);

size++;

}

}// comp < 0

}// while

}// root not null

}//method add

Page 38: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Adjusting paths50

=

25

=

15

=

70

=

60

=

30

=

55

=

90

=

Page 39: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

Adjusting paths50

R

25

=

15

=

70

L

60

L

30

=

55

=

90

=

Page 40: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

protected void adjustPath(Entry to, Entry inserted) {

Object o = inserted.element;

Entry temp = inserted.parent;

while (temp != to) {

if (((Comparable)o).compareTo(temp.element)<0)

temp.balanceFactor = ‘L’;

else

temp.balanceFactor = ‘R’;

temp= temp.parent

}// while

} //adjust path

Page 41: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

protected void fixAfterInsertion(Entry ancestor, Entry inserted) {

Object o = inserted element;

if (ancestor == null) {

if (((Comparable)o).compareTo(root.element)<0)

root.balanceFactor = ‘L’;

else

root.balanceFactor = ‘R’;

adjustPath(root,inserted);

} // Case 1: all ancestor of inserted element have ‘=‘ balanceFactor

Page 42: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

if ((ancestor.balanceFactor == ‘L’ &&

((Comparable)o).compareTo(ancestor.element)>0)||

(ancestor.balanceFactor == ‘R’ &&

((Comparable)o).compareTo(ancestor.element)<0)){

ancestor.balanceFactor = ‘=’;

adjustPath(ancestor,inserted);

} // Case 2: insertion causes ancestor’s balanceFactor to ‘=‘

Page 43: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

if ((ancestor.balanceFactor == ‘R’ &&((Comparable)o).compareTo(ancestor.right.element)>0){

ancestor.balanceFactor = ‘=’;rotateLeft(ancestor);adjustPath(ancestor.parent,inserted);

} // Case 3: ancestor’s balance factor = ‘R’ // and o > ancestor’s right child

if ((ancestor.balanceFactor == ‘L’ &&((Comparable)o).compareTo(ancestor.left.element)<0){

ancestor.balanceFactor = ‘=’;rotateRight(ancestor);adjustPath(ancestor.parent,inserted);

} // Case 4: ancestor’s balance factor = ‘L’ // and o < ancestor’s right child

Page 44: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

if (ancestor.balanceFactor == ‘L’ &&((Comparable)o).compareTo(ancestor.left.element)>0){

rotateLeft(ancestor.left);rotateRight(ancestor);adjustLeftRight(ancestor,inserted);

} // Case 5: ancestor’s balanceFactor = ‘L’ // and o > ancestor’s left child

else{rotateRight(ancestor.right);rotateLeft(ancestor);adjustRightLeft(ancestor,inserted);

} // Case 6: ancestor’s balanceFactor = ‘R’ // and o < ancestor’s right child

}// fixAfterInsertion

Page 45: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

protected void adjustLeftRight(Entry ancestor, Entry inserted) {

Object o = inserted.element;

if (ancestor.parent == inserted) ancestor.balanceFactor = ‘=‘;

else if (((Comparable)o).compareTo(ancestor.parent.element)<0){

ancestor.balanceFactor = ‘R’;

adjustPath(ancestor.parent.left,inserted);

}// o < ancestor’s parent

else {

ancestor.balanceFactor = ‘=’;

ancestor.parent.left.balanceFactor = ‘L’;

adjustPath(ancestor,inserted);

}// while

} //adjustLeftRight

Page 46: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AdjustLeftRight Case 1

50

L

30

=

40

=

50

=

40

=

30

=

Page 47: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AdjustLeftRight Case250

L

10

=5

=

15

=35

=

40

=30

=

45

=

20

=

90

=

70

=

100

=Rotate Left around 20Right around 50

Page 48: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AdjustLeftRight Case240

=

10

=5

=

15

=

30

R35

=

20

=

50

R

45

=

90

=70

=

100

=

Page 49: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AdjustLeftRight Case350

L

10

=5

=

15

=42

=

40

=30

=

45

=

20

=

90

=

70

=

100

=Rotate Left around 20Right around 50

Page 50: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

AdjustLeftRight Case340

=

10

=5

=

15

=

30

=42

=

20

L

50

=

45

L

90

=70

=

100

=

Page 51: AVL Trees CSC 172 SPRING 2002 LECTURE 17. A PROBLEM WITH BSTs Common operations on balanced BST are O(log(n)) Alas, when the tree goes out of balance,

protected void adjustRightLeft(Entry ancestor, Entry inserted) {

Object o = inserted.element;

if (ancestor.parent == inserted) ancestor.balanceFactor = ‘=‘;

else if (((Comparable)o).compareTo(ancestor.parent.element)>0){

ancestor.balanceFactor = ‘L’;

adjustPath(ancestor.parent.right,inserted);

}// o < ancestor’s parent

else {

ancestor.balanceFactor = ‘=’;

ancestor.parent.right.balanceFactor = ‘R’;

adjustPath(ancestor,inserted);

}// while

} //adjustRightLeft