CSE 326: Data Structures AVL Trees Richard Anderson, Steve Seitz - - PowerPoint PPT Presentation

cse 326 data structures avl trees
SMART_READER_LITE
LIVE PREVIEW

CSE 326: Data Structures AVL Trees Richard Anderson, Steve Seitz - - PowerPoint PPT Presentation

CSE 326: Data Structures AVL Trees Richard Anderson, Steve Seitz Winter 2014 1 Announcements HW 2 due now HW 3 out today 2 Balanced BST Complexity of operations depend on tree height For a BST with n nodes Want height to be ~


slide-1
SLIDE 1

1

CSE 326: Data Structures AVL Trees

Richard Anderson, Steve Seitz Winter 2014

slide-2
SLIDE 2

2

Announcements

  • HW 2 due now
  • HW 3 out today
slide-3
SLIDE 3

28

Balanced BST

Complexity of operations depend on tree height For a BST with n nodes

  • Want height to be ~ log n
  • “Balanced”

But balancing cost must be low

slide-4
SLIDE 4

4

How about complete trees?

This worked for heaps

  • balance maintained via percolate up/down
  • Let’s try with BST

(add 14 in rightmost leaf, percolate up)

17 7 3 15 5 10 1 4 13 6

slide-5
SLIDE 5

5

Balancing Trees

  • Many algorithms exist for keeping trees balanced

– Adelson-Velskii and Landis (AVL) trees – Splay trees and other self-adjusting trees – B-trees and other multiway search trees (for very large trees)

  • Today we will talk about AVL trees…
slide-6
SLIDE 6

6

The AVL Tree Data Structure

4 12 10 6 2 11 5 8 14 13 7 9 Ordering property – Same as for BST Structural properties

  • 1. Binary tree property

(0,1, or 2 children)

  • 2. Heights of left and right

subtrees of every node differ by at most 1 Result: worst case height: O(log n) 15

slide-7
SLIDE 7

7

Recursive Height Calculation

Recall: height is max number

  • f edges from root to a leaf

What is the height at A? Define: height(null) = -1

A hleft hright

slide-8
SLIDE 8

8

11 1 8 4 6 3 11 7 1 8 4 6 2 5 AVL trees or not? 10 12 7

slide-9
SLIDE 9

9

Goal

h ∈ O(log n)

  • we will do this by showing: n + 1 > φh
  • What’s φ?

φ is the golden ratio, (1+ √5)/2

–Since the Renaissance, many artists and architects have proportioned their work (e.g., length:height) to approximate the golden ratio φ

The golden section:

slide-10
SLIDE 10
slide-11
SLIDE 11

11

Minimum Size of an AVL Tree

  • n > m(h) = minimum # of nodes in an AVL tree of height h.
  • Base cases:

– m(0) = m(1) =

  • Inductive case:

– m(h) =

  • Can prove:

– m(h) > φh - 1

h-1 h-1 h h-1 h-2 h

slide-12
SLIDE 12

12

Proof that m(h) > φh -1

  • Base cases h=0,1:

m(0) = 1 > φ0 -1 = 0 m(1) = 2 > φ1-1 ≈ 0.62

  • Assume true for h-2 and h-1:

m(h-2) > φh-2 – 1 m(h-1) > φh-1 – 1

  • Induction step:

m(h) = m(h-1) + m(h-2) + 1 > (φh-1 - 1) + (φh-2 - 1) + 1 (φh-1 - 1) + (φh-2 - 1) + 1 = φh-2 (φ +1) – 1 = φh-2 (φ2) – 1 = φh - 1

  • m(h) > φh - 1
slide-13
SLIDE 13

13

Maximum Height of an AVL Tree

Suppose we have n nodes in an AVL tree of height h. We can now say:

m(h) > φh – 1

What does this say about n? What does this say about the complexity of h?

slide-14
SLIDE 14

14

Testing the Balance Property

20 9 2 15 5 10 7 We need to be able to:

  • 1. Track Balance
  • 2. Detect Imbalance
  • 3. Restore Balance

Is this AVL tree balanced? How about after insert(30)?

slide-15
SLIDE 15

15

An AVL Tree

20 9 2 15 5 10 30 7

1 2 1 3

10 3

data height children

slide-16
SLIDE 16

16

AVL trees: find, insert

  • AVL find:

– same as BST find.

  • AVL insert:

– same as BST insert, except may need to “fix” the AVL tree after inserting new value.

We will consider the 4 fundamental insertion cases…

slide-17
SLIDE 17

17

Case #1: left-left insertion (zig)

a Z Y b X

h h h

a Z Y b

h h

X Insert on left child’s left

h+1 h+2

slide-18
SLIDE 18

18

Case #1: repair with single rotation

a b X < b < Y < a < Z

h+1

a Z Y b

h h

X

h+2 h+3

single rotation Height of tree before/after? Effect on Ancestors? Cost?

slide-19
SLIDE 19

19

Single rotation example

10 4 22 8 15 3 6 19 17 20 24 16 10 4 8 15 3 6

slide-20
SLIDE 20

20

Case #2: left-right insertion

a Z Y b X

h+1 h h

a Z b

h h

X Y Insert on left child’s right

h h+2

slide-21
SLIDE 21

21

Case #2: repair with single rotation?

a Z b

h+1 h h

X < b < Y < a < Z Are we better off now? a Z b

h h

X X Y Y

h+1

Single rotation

h+2 h+3

slide-22
SLIDE 22

22

Case #2: trying again

a Z b X c U V

h-1 h h h-1

a Z b X c V

h-1 h h

U Insert on left child’s right (at U or V) Let’s break subtree Y into pieces:

h h+1 h+2

slide-23
SLIDE 23

23

Case #2: trying again

a Z b X c U V

h-1 h h h

Insert on left child’s right (at U or V) Let’s break subtree Y into pieces:

h+1 h+2 h+3

c

slide-24
SLIDE 24

24

Can also do this in two rotations

a Z b X c V

h-1 h h h

a Z b X c V

h-1 h h h

X < b < U < c < V < a < Z U U First rotation

h+1 h+2 h+3

slide-25
SLIDE 25

25

second rotation

a Z b X c V

h-1 h h h

a Z b X c V

h-1 h h h

U U Second rotation

h+1 h+2 h+3 h+1

slide-26
SLIDE 26

26

Double rotation example

15 5 10 4 8 15 3 6 19 17 20 16 22 24 19 17 20 16 22 24

slide-27
SLIDE 27

27

Double rotation, step 1

10 8 15 5 10 4 8 15 3 6 19 17 20 16 22 24 19 17 20 16 22 24

slide-28
SLIDE 28

28

Double rotation, step 2

10 6 8 15 4 3 5 15 19 17 20 16 22 24 19 17 20 16 22 24

slide-29
SLIDE 29

29

Case #3: right-left insertion

a X b Z c

h-1 h h

c Z a X b

h-1 h h h h

V U V U

h+1 h+2 h+3 h+1 h+1 h+2

Double rotation

slide-30
SLIDE 30

30

Case #4: right-right insertion

a X b Y

h+1 h h

Z a X b Y

h h+1 h

Z

h+2 h+3 h+1 h+2

Single rotation

slide-31
SLIDE 31

31

AVL tree case summary

Let a be the node where an imbalance occurs. Four cases to consider. The insertion below a is in the 1. left child’s left subtree. (zig) 2. left child’s right subtree. (zig-zag) 3. right child’s left subtree. (zig-zag) 4. right child’s right subtree. (zig) Cases 1 & 4 are solved by a single rotation: 1. Rotate between a and child Cases 2 & 3 are solved by a double rotation: 1. Rotate between a’s child and grandchild 2. Rotate between a and a’s new child

slide-32
SLIDE 32

32

9 5 2 11 7

  • 1. single rotation?
  • 2. double rotation?
  • 3. no rotation?

Consider inserting one of {1, 4, 6, 8, 10, 12, 14} Which values require:

Single and Double Rotations:

13 3

slide-33
SLIDE 33

33

Insertion procedure

  • 1. Find spot for new key
  • 2. Hang new node there with this key
  • 3. Search back up the path for imbalance
  • 4. If there is an imbalance:

cases #1,#4: Perform single rotation and exit cases #2,#3: Perform double rotation and exit Both rotations restore subtree height to value before insert. Hence only type of rotation is sufficient per insert!

slide-34
SLIDE 34

34

More insert examples

20 9 2 15 5 10 30 17 Insert(33) 12 How to fix? Unbalanced?

1 1 2 3

slide-35
SLIDE 35

35

Single Rotation

20 9 2 15 5 10 30 17 12 33

2 1 3 4 1

9 2 5 10

1 3

slide-36
SLIDE 36

36

More insert examples

Suppose we didn’t do that last insert. Now do: Insert(18) 20 9 2 15 5 10 30 17 12

1 1 2 3

How to fix? Unbalanced?

slide-37
SLIDE 37

37

Single Rotation (oops!)

20 9 2 15 5 10 30 17 12

1 2 1 3 4

9 2 5 10

1 4

18

slide-38
SLIDE 38

38

Double Rotation

20 9 2 15 5 10 30 17 12

1 2 1 3 4

18 9 2 5 10

1 4

slide-39
SLIDE 39

39

More insert examples

20 9 2 17 5 10 30 15

1 1 1 2 3

12 18 Insert(3) How to fix? Unbalanced?

slide-40
SLIDE 40

40

Insert into an AVL tree: 5, 8, 9, 4, 2, 7, 3, 1

slide-41
SLIDE 41

41

AVL complexity

What is the worst case complexity of a find? What is the worst case complexity of an insert? What is the worst case complexity of buildTree?

slide-42
SLIDE 42