AVL Trees AVL Trees K08 - - PowerPoint PPT Presentation

avl trees avl trees
SMART_READER_LITE
LIVE PREVIEW

AVL Trees AVL Trees K08 - - PowerPoint PPT Presentation

AVL Trees AVL Trees K08 / 1 Balanced trees Balanced trees O ( h ) We saw that most of the algorithms in BSTs


slide-1
SLIDE 1

/

AVL Trees AVL Trees

K08 Δομές Δεδομένων και Τεχνικές Προγραμματισμού Κώστας Χατζηκοκολάκης

1

slide-2
SLIDE 2

/

Balanced trees Balanced trees

We saw that most of the algorithms in BSTs are

  • O(h)

But in the worst-case

  • h = O(n)

So it makes sense to keep trees “balanced”

  • Many dierent ways to dene what “balanced” means
  • In all of them:
  • h = O(log n)
  • Eg. complete are one type of balanced tree (see Heaps)
  • But it's hard to maintain both BST and complete properties together
  • AVL: a dierent type of balanced trees
  • 2
slide-3
SLIDE 3

/

AVL Trees AVL Trees

An AVL tree is a BST with an extra property: For all nodes:

  • ∣height(left-subtree) − height(right-subtree)∣ ≤ 1

In other words, no subtree can be much shorter/taller than the other

  • Recall: height is the longest path from the root to some leaf
  • tree with only a root: height 0
  • empty tree: height -1
  • Named after Russian mathematicians Adelson-Velskii and Landis
  • 3
slide-4
SLIDE 4

/

Example – AVL tree Example – AVL tree

4

slide-5
SLIDE 5

/

Example – AVL tree Example – AVL tree

5

slide-6
SLIDE 6

/

Example – AVL tree Example – AVL tree

6

slide-7
SLIDE 7

/

Example – Non-AVL tree Example – Non-AVL tree

7

slide-8
SLIDE 8

/

Example – Non-AVL tree Example – Non-AVL tree

8

slide-9
SLIDE 9

/

Example – Non AVL tree Example – Non AVL tree

9

slide-10
SLIDE 10

/

The The desired property desired property

In an AVL tree:

  • h = O(log n)

Proving this is not hard

  • : minimum number of nodes of an AVL tree with height
  • n(h)

h

We show that

  • h ≤ 2 log n(h)

by induction on

  • h

induction works very well on recursive structures!

  • The base cases hold trivially (why?)
  • n(0) = 1
  • n(1) = 2

10

slide-11
SLIDE 11

/

The desired property The desired property

Inductive step

  • Assume

for all

2 h

log n(h) h < k

Show that it holds for an AVL tree of height

  • h = k

Both subtrees of the root have height at least

  • h − 2

because of the AVL property!

  • So
  • n(k) ≥ 2n(k − 2)

(1)

Induction hypothesis for

  • h = k − 2

2 k−2

log n(k − 2)

From we take

  • n both sides and apply the ind. hypothesis
  • (1)

log

  • log n(k) ≥ 1 + log n(k − 2) ≥ 1 +

=

2 k−2 2 k

11

slide-12
SLIDE 12

/

Balance factor Balance factor

A node can have one of the following “balance factors” Balance factor Meaning

  • Sub-trees have equal heights

/

Left sub-tree is higher

//

Left sub-tree is higher

\

Right sub-tree is higher

\\

Right sub-tree is higher Nodes -, /, \ are AVL. Nodes //, \\ are not AVL.

1 > 1 1 > 1

12

slide-13
SLIDE 13

/

Example AVL Tree Example AVL Tree

13

slide-14
SLIDE 14

/

Example AVL Tree Example AVL Tree

/ −

14

slide-15
SLIDE 15

/

Example AVL Tree Example AVL Tree

\ − / −

15

slide-16
SLIDE 16

/

Example AVL Tree Example AVL Tree

− − − − − \

16

slide-17
SLIDE 17

/

Example AVL Tree Example AVL Tree

− / − − / − − − − −

17

slide-18
SLIDE 18

/

Example non-AVL Tree Example non-AVL Tree

// / −

18

slide-19
SLIDE 19

/

Example non-AVL Tree Example non-AVL Tree

\\ \\ \ − −

19

slide-20
SLIDE 20

/

Example non-AVL Tree Example non-AVL Tree

// \\ \ − − − /

20

slide-21
SLIDE 21

/

Example non-AVL Tree Example non-AVL Tree

\\ − − −

21

slide-22
SLIDE 22

/

Operations in an AVL Tree Operations in an AVL Tree

Same as those of a BST

  • Except that we need to restore the AVL property
  • after inserting a node
  • r deleting a node
  • We do this using rotations
  • 22
slide-23
SLIDE 23

/

Recursive AVL restore Recursive AVL restore

Restoring the AVL property is a recursive operation

  • It happens during an insert or delete
  • Which are both recursive
  • When their recursive calls are unwinding towards the root
  • So when we restore a node , its children are already restored AVL trees
  • r

23

slide-24
SLIDE 24

/

AVL restore after insert AVL restore after insert

Assume became \\ after an insert (the case // is symmetric)

  • r

Let be the root of the right subtree

  • x

The new value was inserted under (since is \\)

  • x

r

What can be the balance factor of ?

  • x

\\ and // are not possible since the child is already restored

  • x

Case 1: is \

  • x

A left-rotation on restores the property!

  • r

Both and become - (easily seen in a drawing)

  • r

x

24

slide-25
SLIDE 25

/

Insert: single left rotation at r Insert: single left rotation at r

r x \ \\ T

1

T

2

h h h h T

1

T

2

h h x r − − T ree height h+3 T ree height h+2 T

3

New node T

3

New node

25

slide-26
SLIDE 26

/

AVL restore after insert AVL restore after insert

Case 2: is /

  • x

This is more tricky

  • A left-rotation on (as before) might cause to become //
  • r

x

We need to do a double right-left rotation

  • First right-rotation on
  • x

Then left-rotation on

  • r

The left-child

  • f becomes the new root
  • w

x

becomes -

  • w

becomes - or /

  • r

becomes - or \

  • x

26

slide-27
SLIDE 27

/

Insert: double right-left rotation at x and Insert: double right-left rotation at x and r

r x / \\ T

1

T

2

h h-1

  • r

h h T

1

h w r One of T

2 or T 3 has the new node and height h

T ree height h+3 T ree height h+2 w − x T

4

h-1

  • r

h h T

4

T

3

T

2

T

3

27

slide-28
SLIDE 28

/

AVL restore after insert AVL restore after insert

Case 3: is -

  • x

This in fact cannot happen!

  • Assume both subtrees of have height
  • x

h

Then the left subtree of also must have height ( )

  • r

h

Otherwise AVL would be violated before the insert (see the drawings)

  • 28
slide-29
SLIDE 29

/

Symmetric case Symmetric case

The case when becomes // is symmetric

  • x

We need to consider the BF of its left-child

  • x

is / : we do a single right rotation at

  • x

r

is \ : we do a double left-right rotation at and

  • x

x r

is - : impossible

  • x

29

slide-30
SLIDE 30

/

Insert: single right rotation at r Insert: single right rotation at r

r x / // T

2

T

3

h h h T

2

T

3

h h x r T ree height h+3 T ree height h+2 − − New node T

1

h T

1

30

slide-31
SLIDE 31

/

Insert: double left-right rotation at x and Insert: double left-right rotation at x and r

r x \ // T

1

h h T

1

h w r One of T

2 or T 3 has the new node and height h

T ree height h+3 T ree height h+2 w − x T

4

h T

4

h-1

  • r

h T

3

T

2

h-1

  • r

h T

3

T

2

31

slide-32
SLIDE 32

/

Insert example Insert example

32

slide-33
SLIDE 33

/

Insert example Insert example

Inserting BRU, causes single right-rotate at ORY

32

slide-34
SLIDE 34

/

Insert example Insert example

Inserting DUS

32

slide-35
SLIDE 35

/

Insert example Insert example

Inserting ZRH

32

slide-36
SLIDE 36

/

Insert example Insert example

Inserting MEX

32

slide-37
SLIDE 37

/

Insert example Insert example

Inserting ORD

32

slide-38
SLIDE 38

/

Insert example Insert example

Inserting NRT, causes double right-left rotation at ORD and MEX

32

slide-39
SLIDE 39

/

AVL restore after delete AVL restore after delete

Assume became \\ after an insert (the case // is symmetric)

  • r

Let be the root of the right-subtree

  • x

The value was deleted from the left sub-tree (since is \\)

  • r

What can be the balance factor of ?

  • x

\\ and // are not possible since the child is already restored

  • x

Case 1: is \

  • x

A left-rotation on restores the property!

  • r

Both and become - (easily seen in a drawing)

  • r

x

33

slide-40
SLIDE 40

/

Delete: single left-rotation at r Delete: single left-rotation at r

r \\ Deleted node T

2

r Height reduced T

3

x h-1 h-1 h \ T

1

T

3

h h-1 h-1 − − x T

2

T

2

T

1

34

slide-41
SLIDE 41

/

AVL restore after delete AVL restore after delete

Case 2: is -

  • x

After a delete this is possible!

  • A left-rotation on again restores the property
  • r

becomes \, becomes /

  • r

x

35

slide-42
SLIDE 42

/

Delete: single left-rotation at r Delete: single left-rotation at r

r \\ Deleted node T

2

T

2

r Height unchanged T

3

x h-1 h h − T

1

T

3

h h h-1 \ / x T

1

36

slide-43
SLIDE 43

/

AVL restore after delete AVL restore after delete

Case 3: is /

  • x

This is more tricky

  • A left-rotation on (as before) might cause to become //
  • r

x

We need to do a double right-left rotation

  • First right-rotation on
  • x

Then left-rotation on

  • r

The left-child

  • f becomes the new root
  • w

x

becomes -

  • w

becomes - or /

  • r

becomes - or \

  • x

37

slide-44
SLIDE 44

/

Delete: double right-left rotation at r Delete: double right-left rotation at r

r \\ Deleted node T

2

r Height reduced T

4

x h-1 h-1

  • r

h-2 h-1 / T

1

T

4

h-1 h-1

  • r

h-2 h-1 − w T

3

T

2

w x T

3

T

1

38

slide-45
SLIDE 45

/

Delete example Delete example

39

slide-46
SLIDE 46

/

Delete example Delete example

Deleting a, causes single left-rotate at d

39

slide-47
SLIDE 47

/

Delete example Delete example

Deleting m, causes double left-right rotation at d and h

39

slide-48
SLIDE 48

/

Complexity of operations on AVL trees Complexity of operations on AVL trees

Search on BST is

  • O(h)

So for AVL, since

  • O(log n)

h ≤ 2 log n

Insert/delete on BST is

  • O(h)

We add at most on rotation at each step, each rotation is

  • O(1)

So also

  • O(log n)

Interesting fact

  • During insert at most one rotation will be performed!
  • Because both rotations we saw decrease the height of the sub-tree
  • 40
slide-49
SLIDE 49

/

Implementation details Implementation details

We need to keep the height of each subtree

  • to compute the balance factors
  • If we need to save memory we can store only the balance factors
  • Restoring after both insert and delete are similar
  • We can treat them together
  • 41
slide-50
SLIDE 50

/

Readings Readings

  • T. A. Standish. Data Structures, Algorithms and Software Principles in C.

Chapter 9. Section 9.8.

  • R. Kruse, C.L. Tondo and B.Leung. Data Structures and Program Design in
  • C. Chapter 9. Section 9.4.
  • M.T. Goodrich, R. Tamassia and D. Mount. Data Structures and Algorithms

in C++. 2nd edition. Section 10.2

  • 42