AVL Trees
AdeIVelki and Reea A BST is a binary tree - labelled by keys - - - PowerPoint PPT Presentation
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 -
AVL
comes from the authors
AdeIVelki
and
Landis
Reea
A
BST
is
- a binary tree
- with
labelled by keys
- for every two
v.v
: . if heis
in the left
subtree of v
then
key (a)
a key 4)- if
in the right subtree of 4
then key Cu) > key 4)
An
AVL Tree
is A
BST
, thatsatisfies
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) )
Operations
:BST operations , modified to
maintain the balance invariant . Unbalanced
suzb
- trees
using
BST
rotation
- right
rotation
⇒
a④ ¥④⇐
④
⑥ ¥
left
rotation
at
3
RotationspreserreBSTOrderInvariaut.ve
want to useBST rotations to
"repair
" the Art balance- We must be sure
rotations preserve the
BST invariants .Obviously, after a rotation we still
have a binary treelabelled with keys
. To see the- rder invariant
preserved
consider : rightrotate
④
at7¥ 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 .AVLTreeinsertionl.ba
a BST insertion .2
. Let vbe the unbalanced
node of greatestdepth (if
there
is- ne )
- 3. Repair the
balance
condition at
v. "Consider
"4 cases for
"repair " : " oustedVunbatauud
" inside "- *
- ÷±÷÷:
:
*
to= ¥74
Y* =
D1
5IY
- 6
T
- fix the
- utside
.
aes¥fixthe
T
- fix the
¥
tob
Insertion here! I hereP
7T
- fix the
b
III.In!*
.It right:*
( like outside case)
T
- fix the
b
¥¥a¥¥*
.It right:*
.⇒i÷
.T
- fix the
*
to toII:!⇒
.It right:*
÷:::÷÷÷÷i:
'
right4)← a
left(a) ←T3
right(b) ←T2
E
Amaximallyunbalancedheight-5AVLTree-N.SI
¥35
µ
**.
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 ← httN( 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. %) = 2h12Claim
: N Ch)
> 2h12 , for allh 7
I .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
height (htt)t
h - I
- r h
lomplaxityofAVLTreesearchkinser.tt
- r any AVL tree
with
n nodes and height is > 1 , wehave:
N1D ⇒ 2%
solog Nln) > %
- r
log.NL#a2logzn
soh
= Of log n)⇒ search takes time 0( log
n)Insert takes time 0 ( login)
.Aside :
Base of logs in
0L ) .- Why
not
h=0(login)
?
Change of base :
nIogbn
= t%a- = (%) Logan ↳gabe constant Change of base
is multiplication by aconstant
, s. "disappears
" inside a big- 0h
fH=0(login)
⇒ fN=0(login) for any
K7 0KH
.(corrected)
Comparison of
nk
26gin
- Worst-case height of BST
AVL tree .
nRogen
÷
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)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
'⑤
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
- L has
- r I child : delete it
2.
The removal may have caused some nodes to become unbalelanced .- Notice that, in each
leaf
. We usethis fact in describing how to
re - balance .AVLTreeRemoral.IS
. w ← parent of deleted leaff-ofeach node
a = w . . - root- n path from W
- root){
unbalanced
- Let T be the
- rebalance
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 .
Removal of an AVL Tree leaf
causes at most 1 node to become unbalanced .^
\
- I
:÷:÷:÷
::
"(÷¥
.:*¥E:÷:3
unbalanced node nearest to- .
node #
ANAhktreeremoralthatikustrat.es:1
. Need to re - balance after removal- 2. Re - balancing may reduce height of
¥¥ ④I
⑤
② ⑤
/
µ
right
at IoIo
I
5M¥
④ ⑤
⑤ '
⑤
Amtrak)
End
.