avl trees - simon fraser universitymitchell/cmpt-225/2020... · avl trees. avl comes from the...
TRANSCRIPT
AVL Trees
AVL comes from the authors
AdeIVelki and Landis
ReeaA 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) > key4)
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) )
Operations : BST operations , modified tomaintain the balance invariant .
Unbalanced suzb-trees are " repaired
"
using BST
rotation-
right rotation
⇒ a
④ ¥④⇐ ④⑥ ¥left rotation
at 3
RotationspreserreBSTOrderInvariaut.vewant 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 binarytree
labelled with keys . To see the order invariant is
preserved consider : rightrotate ④at
7¥ 170i 'E¥
Suppose ux is a key in TI , y in T2 and 2- inT3
.
According to the B5T order invariant , in bothtrees we must have:Xa M L y c u a Z .
AVLTreeinsertionl.baa BST insertion .
2 .
Let v be the unbalanced nodeof greatest
depth (if there is one ) .
3. Repair the balancecondition at v."
Consider
"
4 cases for "repair"
:
"
ousted Vunbatauud" inside
"
-* V o
÷±÷÷: :*to
= ¥74 Y*
=D1 5IY-6
To fix the "outside " case : 1 rotation at V :
.
aes¥fixthe
To fix the " inside , we use tworotatiou¥
to b
Insertion here ! I hereP
7
To fix the " inside , we use,
tworotatioyi.nu
to b
III.In!*. It right:*
( like outside case)
To fix the " inside , we use tworotas :
to b
¥¥a¥¥*. It right:*.⇒i÷.
To fix the " inside , we use tworotas :
*to to
II:!⇒. It right:*
÷:::÷÷÷÷i: 'right4)← aleft (a)←T3
right (b)←T2
E
Amaximallyunbalancedheight-5AVLTree-N.SI¥35µ **.
lomplexityofAVTTreeoperati.NLWe want a bound on 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→To← h or h- I
Nlh) = It N (h - 1) t N (h - 2)
÷:*, D !!> 23N ( h - 6)%
52in ( h - 2 i)0
5£" Nlo) ( since h -hi ⇒ i. %)
= 2h12
Claim : N Ch) > 2h12,for all h 7 I .
H12
ProotthatNlh)z2_By ind. on 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µmIt §4=2
shorter taller subtree+1+2
subtree of of height h= 2T height
(htt)th- I or h
= 2 ✓-
lomplaxityofAVLTreesearchkinser.ttor any
AVL tree with n nodes and height is > 1,
we have: N1D ⇒ 2%so log Nln) > %or in I 2
"
log.NL#a2logznso h = Of log n)
⇒ search takes time 0( log n)
Insert takes time 0 ( login) .
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)
Comparison of n k 26gin-• Worst-case height of BST vs AVL tree .
n Rogen÷100 13
1000 20
104 27106 40108 53
10"
66
1,000,000,000,000 = 10k 80
( 10K bytes a Terabyte , so this is big but not crazy )
AVLTreeRemov1) 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 on L to v
and delete L.
'⑤Esi←target 6 =y¥¥→ '①
Cases : la is at a node v with 2 children , and successorL.
• more key at successor L of K to V• L has eithe 0 or I child : delete itas in case 1 a case2.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 .
AVLTreeRemoral.IS.
w ← parent of deleted leaff-ofeach node a = w . . -
root on 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 .
Removal of an AVL Tree leaf causes at most 1
node to become unbalanced .
^\-
I:÷:÷:÷::" (÷¥. :* ¥E:÷:3unbalanced node nearest to •.
node #
ANAhktreeremoralthatikustrat.es:1. Need to re - balance after removal2. Re-balancing may reduce height of a sub -tree
remove④
¥¥ ④I⑤ ② ⑤
/ µ rightat Io
Io I
5M¥ ④⑤ ⑤ '⑤
Amtrak)
End.