avl trees. today avl deletes and rotations, then testing your knowledge of these concepts! before i...

16
AVL trees

Upload: herbert-hodges

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

AVL trees

Page 2: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Today• AVL Deletes and rotations, then testing your

knowledge of these concepts!

• Before I get into details, I want to show you some animated operations in an AVL tree.

• I think it’s important to just get the gears turning in your mind.

• We’ll look at some animations again after we study (some) details.

• Interactive web applet

Page 3: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

AVL tree

• Is a binary search tree• Has an additional height constraint:– For each node x in the tree, Height(x.left) differs

from Height(x.right) by at most 1

• I promise:– If you satisfy the height constraint, then the

height of the tree is O(lg n).– (Proof is easy, but no time! =])

Page 4: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

AVL tree

• To be an AVL tree, must always:– (1) Be a binary search tree– (2) Satisfy the height constraint

• Suppose we start with an AVL tree, then delete as if we’re in a regular BST.

• Will the tree be an AVL tree after the delete?– (1) It will still be a BST… that’s one part.– (2) Will it satisfy the height constraint?

• (Not covering insert, since you already did in class)

Page 5: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

BST Delete breaks an AVL tree

77

44

33

99

77

44

33

Delete(9)

h(left) > h(right)+1so NOT an AVL

tree!

Page 6: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Balance factors

• To check the balance constraint, we have to know the height h of each node

• Or do we?• In fact, we can store balance factors instead.• The balance factor bf(x) = h(x.right) – h(x.left)– bf(x) values -1, 0, and 1 are allowed.– If bf(x) < -1 or bf(x) > 1 then tree is NOT AVL

Page 7: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Same example with bf(x), not h(x)

77

44

33

99

77

44

33

Delete(9)

-1

-1

0

0

-2

-1

0

bf < -1so NOT an AVL tree!

Page 8: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

What else can BST Delete break?

• Balance factors of ancestors…

77

44

33

99

77

44Delete(3)

-1

-1

0

0

-1

-1 9900

0

Page 9: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Need a new Delete algorithm

• We are starting to see what our delete algorithm must look like.

• Goal: if tree is AVL before Delete, then tree is AVL after Delete.

• Step 1: do BST delete.– This maintains the BST property,

but can BREAK the balance factors of ancestors!• Step 2: fix the balance constraint.– Do something that maintains the BST property,

but fixes any balance factors that are < -1 or > 1.

Page 10: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Bad balance factors

• Start with an AVL tree, then do a BST Delete.• What bad values can bf(x) take on?– Delete can reduce a subtree’s height by 1.– So, it might increase or decrease h(x.right) –

h(x.left) by 1.– So, bf(x) might increase or decrease by 1.– This means:• if bf(x) = 1 before Delete, it might become 2. BAD.• If bf(x) = -1 before Delete, it might become -2. BAD.• If bf(x) = 0 before Delete, then it is still -1, 0 or 1. OK.

2 cases2 cases

Page 11: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Problematic cases for Delete(a)

• bf(x) = -2 is just symmetric to bf(x) = 2.• So, we just look at bf(x) = 2.

xx2

h+2

h

xx-2

aa aa

Page 12: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Delete(a): 3 subcases for bf(x)=2

• Since tree was AVL before, bf(z) = -1, 0 or 1 Case bf(z) = 0 Case bf(z) = 1

xx

T2T2

T1T1

zz

2

h+1

h 0

T3T3

aah

xx

T2T2

T1T1

zz

2

1

T3T3

aa

Page 13: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Delete(a): final subcase for bf(x)=2Case bf(z) = -1: we have 3 subcases. (More details)

Case bf(y) = 0 Case bf(y) = -1 Case bf(y) = 1

xx

T1T1

zz

2

h

h -1

T3T3

aa yy

T21T21 T22

T22

h-1

0

xx

T1T1

zz

2

-1

T3T3

aa yy

T21T21

T22T22h

-1

xx

T1T1

zz

2

-1

T3T3

aa yy

T21T21

T22T22

1

Page 14: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Fixing case bf(x) = 2, bf(z) = 0• We do a single left rotation• Preserves the BST property, and fixes bf(x) = 2

xx

T2T2

T1T1

zz

2

h+1

h 0

T3T3

zz

T2T2

T1T1

xx

-1

1

T3T3

Page 15: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Fixing case bf(x) = 2, bf(z) = 1• We do a single left rotation (same as last case)• Preserves the BST property, and fixes bf(x) = 2

xx

T2T2

T1T1

zz

2

h+1

h 1

T3T3

zz

T2T2T1T1

xx

0

0

T3T3

h

Page 16: AVL trees. Today AVL Deletes and rotations, then testing your knowledge of these concepts! Before I get into details, I want to show you some animated

Interactive AVL Deletes

• Interactive web applet• Video of this applet being used to show most

cases for insert / delete