csc 172 data structures

56
CSC 172 DATA STRUCTURES CSC172_LEC11_TreesAVL. odp

Upload: jorjanna-gomez

Post on 02-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

CSC 172 DATA STRUCTURES. CSC172_LEC11_TreesAVL.odp. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 172  DATA STRUCTURES

CSC 172 DATA STRUCTURES

CSC172_LEC11_TreesAVL.odp

Page 2: CSC 172  DATA STRUCTURES

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: CSC 172  DATA STRUCTURES

BST Structure : Log & Linear

Page 4: CSC 172  DATA STRUCTURES

Permutations of 1,2,3

Page 5: CSC 172  DATA STRUCTURES

AVL Trees

The first balanced binary search treeNamed 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 6: CSC 172  DATA STRUCTURES

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 7: CSC 172  DATA STRUCTURES

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 8: CSC 172  DATA STRUCTURES

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

Page 9: CSC 172  DATA STRUCTURES

AVL THEOREMLet 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-2

These subtrees must have fewest nodes for height

So, SH = SH-1 + SH-2 + 1

Minimum number AVL trees are Fibonacci trees

Page 10: CSC 172  DATA STRUCTURES

Smallest AVL Tree of height H

SH-

1

H-1SH-

2

H-2

H

Page 11: CSC 172  DATA STRUCTURES

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 12: CSC 172  DATA STRUCTURES

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 13: CSC 172  DATA STRUCTURES

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 14: CSC 172  DATA STRUCTURES

4 cases

1. Insertion in left sub-tree of left child2. Insertion in right sub-tree of left child3. Insertion in left sub-tree of right child4. Insertion in right sub-tree of right child

1 & 4 are symmetric2 & 3 are symmetric

Page 15: CSC 172  DATA STRUCTURES

Cases 1 & 4

A B

C

k1

k2Insertion extends Tree ‘A’

HH-2

H-2 H-2

Page 16: CSC 172  DATA STRUCTURES

Cases 1 & 4

A B

C

k1

k2Insertion extends Tree ‘A’

H-2

H-1 H-2

“Rotation” fixesbalance

Page 17: CSC 172  DATA STRUCTURES

Cases 1 & 4

AB C

k2

k1Insertion extends Tree ‘A’

H-2H-1 H-2

“Rotation” fixesbalance

H

Page 18: CSC 172  DATA STRUCTURES

Cases 2 & 3

A B

C

k1

k2Insertion extends Tree ‘B’

HH-2

H-2 H-2

Page 19: CSC 172  DATA STRUCTURES

Cases 2 & 3

A B

C

k1

k2Insertion extends Tree ‘B’

H-2

H-2 H-1

Page 20: CSC 172  DATA STRUCTURES

Cases 2 & 3

AB C

k2

k1Insertion extends Tree ‘B’

H-2H-2 H-1

“Rotation” Does not fixbalance

Page 21: CSC 172  DATA STRUCTURES

Double Rotation

If x is out of balance for cases 2&31. Rotate between X’s child and grandchild2. Rotate between X and its new child

Page 22: CSC 172  DATA STRUCTURES

Cases 2 & 3

A B

C

k1

k2Insertion extends Tree ‘B’

HH-2

H-2 H-2

Page 23: CSC 172  DATA STRUCTURES

Cases 2 & 3

A C

D

k1

k2Insertion extends Tree ‘B’ or ‘C’

HH-2

H-2 H-3

k3

B

Page 24: CSC 172  DATA STRUCTURES

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 25: CSC 172  DATA STRUCTURES

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 26: CSC 172  DATA STRUCTURES

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 27: CSC 172  DATA STRUCTURES

ImplementationDo this in lab

InsertFixupRotateNeed to keep track of balance

Page 28: CSC 172  DATA STRUCTURES

Rotationprivate void rotateLeft(Node p) {

Node 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 29: CSC 172  DATA STRUCTURES

element left right parentroot

50

80

Page 30: CSC 172  DATA STRUCTURES

element left right parentroot

50

80

90

r

Entry r = p.right;

p

Page 31: CSC 172  DATA STRUCTURES

element left right parentroot

50

80

90

r

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

p

Page 32: CSC 172  DATA STRUCTURES

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 33: CSC 172  DATA STRUCTURES

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 34: CSC 172  DATA STRUCTURES

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 35: CSC 172  DATA STRUCTURES

Node classprivate static class Node {

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

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

}}

Page 36: CSC 172  DATA STRUCTURES

50

R

20

L

10

=

80

R

70

=

100

=

92

=

50

=

Page 37: CSC 172  DATA STRUCTURES

public boolen add(Object o){if (root == null) {

root = new Node(o,null);size++;return true;

}// empty treeelse {

Node temp = root, ancestor = null; // we keep track of nearest unbalanced

ancestorint comp;while (true) {

comp = ((Comparable)o).compareTo(temp.element);if (comp == 0) return false;

Page 38: CSC 172  DATA STRUCTURES

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 39: CSC 172  DATA STRUCTURES

else { // comp > 0 if (temp.balanceFactor != ‘=‘) ancestor = temp;if (temp.right != null) temp = temp.right;else {

temp.rig = new Node(o,temp);fixAfterInsertion(ancestor,temp.right);size++;

}}// comp < 0

}// while }// root not null

}//method add

Page 40: CSC 172  DATA STRUCTURES

Adjusting paths50

=

25

=

15

=

70

=

60

=

30

=

55

=

90

=

Page 41: CSC 172  DATA STRUCTURES

Adjusting paths50

R

25

=

15

=

70

L

60

L

30

=

55

=

90

=

Page 42: CSC 172  DATA STRUCTURES

protected void adjustPath(Entry to, Entry inserted) {Object o = inserted.element;Node 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 43: CSC 172  DATA STRUCTURES

protected void fixAfterInsertion(Node 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 44: CSC 172  DATA STRUCTURES

protected void fixAfterInsertion(Node 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 45: CSC 172  DATA STRUCTURES

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 46: CSC 172  DATA STRUCTURES

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

Page 47: CSC 172  DATA STRUCTURES

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 48: CSC 172  DATA STRUCTURES

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

Page 49: CSC 172  DATA STRUCTURES

else{

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

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

}// fixAfterInsertion

Page 50: CSC 172  DATA STRUCTURES

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 parentelse {

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

}// while} //adjustLeftRight

Page 51: CSC 172  DATA STRUCTURES

AdjustLeftRight Case 1

50

L

30

=

40

=

50

=

40

=

30

=

Page 52: CSC 172  DATA STRUCTURES

AdjustLeftRight Case250

L

10

=5

=

15

=35

=

40

=30

=

45

=

20

=

90

=

70

=

100

=Rotate Left around 20Right around 50

Page 53: CSC 172  DATA STRUCTURES

AdjustLeftRight Case240

=

10

=5

=

15

=

30

R35

=

20

=

50

R

45

=

90

=70

=

100

=

Page 54: CSC 172  DATA STRUCTURES

AdjustLeftRight Case350

L

10

=5

=

15

=42

=

40

=30

=

45

=

20

=

90

=

70

=

100

=Rotate Left around 20Right around 50

Page 55: CSC 172  DATA STRUCTURES

AdjustLeftRight Case340

=

10

=5

=

15

=

30

=42

=

20

L

50

=

45

L

90

=70

=

100

=

Page 56: CSC 172  DATA STRUCTURES

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 parentelse {

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

}// while} //adjustRightLeft