CPSC 221: Data Structures Balanced BST (AVL Trees)

32
CPSC 221: Data Structures Balanced BST (AVL Trees) Alan J. Hu (Using mainly Steve Wolfman’s Slides)

Transcript of CPSC 221: Data Structures Balanced BST (AVL Trees)

CPSC 221: Data Structures

Balanced BST (AVL Trees)

Alan J. Hu(Using mainly Steve Wolfman’s Slides)

Balance

• Balance– height(left subtree) - height(right subtree)– zero everywhere ⇒ perfectly balanced– small everywhere ⇒ balanced enough

t

57

Balance between -1 and 1 everywhere ⇒maximum height of ~1.44 lg n

AVL Tree Dictionary Data Structure

4

131062

115

8

14127 9

• Binary search tree properties– binary tree property– search tree property

• Balance property– balance of every node is:

-1, 0, or 1

– result:• depth is Θ(log n)

15

But, How Do We Stay Balanced?

• What do you do if you pick something up out of balance?

Rotation Intuition: Before

a

XY

b

Z

Rotation Intuition: After

a

X

Y

b

Z

Time Complexity of Rotation?

• O(1)?• O(lg n)?• O(n)?• O(n lg n)?• O(n2)?• All of the above?

AVL Tree Insertion

• Pre-Condition: Inserting an item into a correct AVL tree (binary, search tree, AVL balanced)

• Do a normal BST insert.• This might upset balance property, so do

rotation(s) as needed to restore AVL balance.Turns out, you need at most two rotations!

Before Insertion (Single Rotation)a

X

Y

b

Z

h-1 h - 1

h h - 1

h+1

After Insertion (Single Rotation)a

X

Y

b

Z

h h - 1

h + 1 h - 1

h + 2

← An insert made this BAD!

General Single Rotation

• After rotation, subtree’s height same as before insert!• Height of all ancestors unchanged.

So?

a

X

Y

b

Z

a

XY

b

Zh h - 1

h + 1 h - 1

h + 2

h

h - 1

h

h - 1

h + 1

← An insert made this BAD!

Example: Easy Insert

2092

155

10

3017

Insert(3)

120

0

100

1 2

3

0

Hard Insert (Bad Case #1)

2092

155

10

3017

Insert(33)

3

121

0

100

2 2

3

00

Single Rotation

2092

155

10

30173

12

33

1

0

200

2 3

3

10

0

3092

205

10

333

151

0

110

2 2

3

001712

0

Hard Insert (Bad Case #2)

Insert(18)

2092

155

10

30173

121

0

100

2 2

3

00

Single Rotation (oops!)

2092

155

10

30173

121

1

200

2 3

3

00

3092

205

10

3

151

1

020

2 3

3

01712

0

180

180

When Single Rotation Doesn’t Help

• After rotation, still unbalanced!• What can you do?

a

X

Y

b

Z

a

X

Y

b

Zhh - 1

h + 1 h - 1

h + 2

h - 1

h

h + 1

h - 1

← An insert made this BAD!

h + 2

When Single Rotation Doesn’t Help

• After rotation, still unbalanced!• The problem is Y is too heavy, so rotate stuff out of Y!

a

X

Y

b

Z

a

X

Y

b

Zhh - 1

h + 1 h - 1

h + 2

h - 1

h

h + 1

h - 1

← An insert made this BAD!

h + 2

Double Rotation Part 1

• First, do a single rotation farther down, to split up the big subtree.

a

Z

b

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1a

Zb

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1

Double Rotation Part 1

• First, do a single rotation farther down, to split up the big subtree.

a

Zb

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1

Double Rotation Part 2

• Now, we can do the originally planned rotation, and not have too much height shift over…

a

Zb

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1

Double Rotation Part 2

• Now, we can do the originally planned rotation, and not have too much height shift over…

a

Zb

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1a

Z

b

W

c

X

Y

h

h - 1

h - 1h

h + 1

h - 1?

General Double Rotation

• Height of subtree still the same as it was before insert!• Height of all ancestors unchanged.

a

Z

b

W

c

X Y

a

Z

b

W

c

X Y

h

h - 1?

h - 1

h - 1

h + 2

h + 1

h - 1h - 1

h

h + 1

h

h - 1?

Hard Insert (Bad Case #2)

Insert(18)

2092

155

10

30173

121

0

100

2 2

3

00

Double Rotation (Step #1)

2092

155

10

30173

121

1

200

2 3

3

00

180

1792

155

10

203

121 200

2 3

3

10

300

Look familiar?18

0

Double Rotation (Step #2)

1792

155

10

203

121 200

2 3

3

10

300

180

2092

175

10

303

151

0

110

2 2

3

0012

018

Today’s Outline

• Addressing one of our problems• Single and Double Rotations• AVL Tree Implementation

Insert Algorithm

• Find spot for value• Hang new node• Search back up for imbalance• If there is an imbalance:

case #1: Perform single rotation and exit

case #2: Perform double rotation and exit

Mirrored cases also possible

AVL Algorithm Revisited• Recursive1. Search downward for

spot2. Insert node3. Unwind stack,

correcting heightsa. If imbalance #1,

single rotateb. If imbalance #2,

double rotate

• Iterative1. Search downward for

spot, stackingparent nodes

2. Insert node3. Unwind stack,

correcting heightsa. If imbalance #1,

single rotate andexit

b. If imbalance #2,double rotate andexit

(Parent pointers make iterative version easier.)

Single Rotation Codevoid RotateLeft(Node *& root) {

Node * temp = root->right;root->right = temp->left;temp->left = root;root->height = max(height(root->right),

height(root->left)) + 1;temp->height = max(height(temp->right),

height(temp->left) + 1; root = temp;

}

X

Y

Z

root

temp

(The “height” function returns -1 for a NULL subtreeor the “height” field of a non-NULL subtree.)

Notice that root is a reference parameter. MUST be the “correct” pointer.

Double Rotation Codevoid DoubleRotateLeft(Node *& root) {

RotateRight(root->right);RotateLeft(root);

}

a

Z

b

W

c

XY

a

Z

c

b

X

Y

W

First Rotation

Double Rotation Completed

a

Z

c

b

X

Y

W

a

Z

c

b

XY

W

First Rotation Second Rotation