AdeIVelki and Reea A BST is a binary tree - labelled by keys - - - PowerPoint PPT Presentation

adeivelki
SMART_READER_LITE
LIVE PREVIEW

AdeIVelki and Reea A BST is a binary tree - labelled by keys - - - PowerPoint PPT Presentation

AVL Trees comes from the authors AVL Landis AdeIVelki and Reea A BST is a binary tree - labelled by keys - with nudes - for every two v.v nodes : subtree of v in the left . if is he a key 4) key (a) then in the right subtree of 4 -


slide-1
SLIDE 1

AVL Trees

slide-2
SLIDE 2

AVL

comes from the authors

AdeIVelki

and

Landis

slide-3
SLIDE 3

Reea

A

BST

is

  • a binary tree
  • with
nudes

labelled by keys

  • for every two
nodes

v.v

: . if he

is

in the left

subtree of v

then

key (a)

a key 4)
  • if
u is

in the right subtree of 4

then key Cu) > key 4)

slide-4
SLIDE 4

An

AVL Tree

is A

BST

, that

satisfies

the AVL Tree height invariant :

for

every

node

v ,

/ height(left

  • height(right d)IE1

( We define height(left

to be -1 if left G) does

not exist;

sin . for height( rightCut) )

slide-5
SLIDE 5

Operations

:

BST operations , modified to

maintain the balance invariant . Unbalanced

suzb

  • trees
are "repaired "

using

BST

rotation

  • right

rotation

a

④ ¥④⇐

⑥ ¥

left

rotation

at

3

slide-6
SLIDE 6

RotationspreserreBSTOrderInvariaut.ve

want to use

BST rotations to

"

repair

" the Art balance
  • We must be sure

rotations preserve the

BST invariants .

Obviously, after a rotation we still

have a binary tree

labelled with keys

. To see the
  • rder invariant
is

preserved

consider : right

rotate

at

7¥ 170

i

'E¥

Suppose

ux is a key in TI , y in T2 and 2- inT3 .

According to the B5T order invariant , in both

trees

we must have

:X

a M L y c u a Z .
slide-7
SLIDE 7

AVLTreeinsertionl.ba

a BST insertion .

2

. Let v

be the unbalanced

node of greatest

depth (if

there

is
  • ne )
.
  • 3. Repair the

balance

condition at

v. "

Consider

"

4 cases for

"repair " : " ousted

Vunbatauud

" inside "
  • *
V
  • ÷±÷÷:

:

*

to

= ¥74

Y* =

D1

5IY

  • 6
slide-8
SLIDE 8

T

  • fix the
"
  • utside
" case : 1 rotation at V :

.

aes¥fixthe

slide-9
SLIDE 9

T

  • fix the
"inside , we use tworotatiou

¥

to

b

Insertion here! I hereP

7
slide-10
SLIDE 10

T

  • fix the
"inside , we use , tworotatioyi.nu to

b

III.In!*

.

It right:*

( like outside case)

slide-11
SLIDE 11

T

  • fix the
"inside , we use tworotas : to

b

¥¥a¥¥*

.

It right:*

.⇒i÷

.
slide-12
SLIDE 12

T

  • fix the
"inside , we use tworotas :

*

to to

II:!⇒

.

It right:*

÷:::÷÷÷÷i:

'

right4)← a

left(a) ←T3

right(b) ←T2

slide-13
SLIDE 13

E

Amaximallyunbalancedheight-5AVLTree-N.SI

¥35

µ

**.

slide-14
SLIDE 14

lomplexityofAVTTreeoperati.NL

We want a bound

  • n the height of an n- node

AVLTree . Let Nlh)

= min . # of nodes in an AVL tree of height h . Observe : N(o) = I ← htt

N( 1)

= 2 In →T
  • ← h
  • r h- I

Nlh)

= It N (h - 1) t N ( h - 2)

÷:*,

D !!

> 23N ( h
  • 6)
%

52in ( h

  • 2 i)

5£" Nlo)

( since

h - hi ⇒ i. %) = 2h12

Claim

: N Ch

)

> 2h12 , for all

h 7

I .
slide-15
SLIDE 15 H12

ProotthatNlh)z2_

By ind .

  • n h .

Basis :

NC0) =L 720=1 ;

N4)= 2=2 "

  • Ma
  • II. Suppose that , for some H71 , Nch) > 2h"
.
  • II. Show that N(htt) > zlht'k :

NlhtDz2Nlh -D ← B""

> 2.2 "

7µm

It

§

4=2

shorter

taller subtree

+1+2 subtree of
  • f height h
= 2T

height (htt)t

h - I

  • r h
= 2
slide-16
SLIDE 16

lomplaxityofAVLTreesearchkinser.tt

  • r any AVL tree

with

n nodes and height is > 1 , we

have:

N1D ⇒ 2%

so

log Nln) > %

  • r
in I 2 "

log.NL#a2logzn

so

h

= Of log n)

⇒ search takes time 0( log

n)

Insert takes time 0 ( login)

.
slide-17
SLIDE 17

Aside :

Base of logs in

0L ) .
  • Why
: hi =0( log n)

not

h=0(login)

?

Change of base :

n

Iogbn

= t%a- = (%) Logan ↳gab

e constant Change of base

is multiplication by a

constant

, s. "

disappears

" inside a big
  • 0h
.

fH=0(login)

⇒ fN=0(login) for any

K7 0KH

.

(corrected)

slide-18
SLIDE 18

Comparison of

n

k

26gin

  • Worst-case height of BST
vs

AVL tree .

n

Rogen

÷

100 13 1000 20 104 27 106 40 108 53 10 " 66 1,000,000,000,000 =

10k

80

( 10K bytes

a Terabyte , so this is big but not crazy)
slide-19
SLIDE 19

AVLTreeRemov

1) Do BST removal .

Cage1 : key k is at a leaf L i delete L . Cage 2 " K is at a node V that has 1 child L .
  • the child L must be a leaf .
  • move the key
  • n
L to v and delete L .

'⑤

Esi

←target 6 =y¥¥→

'

Cases : la is at a node v with 2 children , and successor L .

  • more key at successor L of K to
V
  • L has
eithe
  • r I child : delete it
as in case 1 a case

2.

The removal may have caused some nodes to become unbalelanced .
  • Notice that, in each
case , we deleted a

leaf

. We use

this fact in describing how to

re - balance .
slide-20
SLIDE 20

AVLTreeRemoral.IS

. w ← parent of deleted leaf

f-ofeach node

a = w . . - root
  • n path from W
.
  • root){
. if u is

unbalanced

  • Let T be the
subtree rooted at m
  • rebalance
u using suitable rotations

g

. if height of T did not get smaller , exit .*

Correctness of the algorithm involves

two properties :

a) There is at most 1 unbalanced model after deletion

b)

Rebalancing may make an ancestor unbalanced .

slide-21
SLIDE 21

Removal of an AVL Tree leaf

causes at most 1 node to become unbalanced .

^

\

  • I

:÷:÷:÷

::

"(÷¥

.:*

¥E:÷:3

unbalanced node nearest to
  • .

node #

slide-22
SLIDE 22

ANAhktreeremoralthatikustrat.es:1

. Need to re - balance after removal
  • 2. Re - balancing may reduce height of
a sub -tree remove④

¥¥ ④I

② ⑤

/

µ

right

at Io

Io

I

5M¥

④ ⑤

⑤ '

slide-23
SLIDE 23

Amtrak)

End

.