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 - if is u key Cu ) > key 4) then
is A BST satisfies AVL Tree , that An the AVL Tree height invariant : for node v , every - height ( right d) IE1 / height ( left ( We define height ( left to be -1 if not exist ; left G) does sin . for height ( right Cut ) )
⇒ BST operations , modified to Operations : balance invariant . maintain the BST " " repaired Unbalanced - trees suzb using are rotation - rotation right ④ ¥ ④ ⇐ a ④ ⑥ ¥ rotation left at 3
RotationspreserreBSTOrderInvariaut.ve " the Art balance BST rotations to " want to repair use BST invariants rotations preserve the - We must be sure . have a binary tree Obviously , after a rotation we still order invariant labelled with keys . To see the is right consider : preserved rotate ④ 7¥ 170 at 'E¥ i a key in TI , y in T2 and 2- in T3 Suppose is . ux According to the B5T order invariant , in both trees have we must :X L y c Z u a M a .
AVLTreeinsertionl.ba a BST insertion . node of greatest be the unbalanced . Let 2 v one ) depth ( if there is . condition at v. balance 3. Repair the " " 4 cases for " repair " Consider : " ousted " inside " Vunbatauud o V * - ÷±÷÷ : : to = ¥74 * Y* 5IY -6 D1 =
o fix the T V : " case : 1 rotation at " outside . aes¥fixthe
use tworotatiou T o fix the " inside , we ¥ b to Insertion here ! I here P 7
, tworotatioyi.nu T o fix the " inside , we use b to It right :* III. In !* . ( like outside case )
use tworotas : T o fix the " inside , we b to It right :* ¥¥a¥¥* . . ⇒ i÷ .
use tworotas : T o fix the " inside , we * to to It right :* II :! ⇒ . ÷ :::÷÷÷÷i : ' right 4) ← a left (a) ← T3 right (b) ← T2
Amaximallyunbalancedheight-5AVLTree-N.SI E ¥35 µ ** .
lomplexityofAVTTreeoperati.NL AVL Tree . on the height of an n - node We want a bound = min . # of nodes in an AVL tree of height Let Nlh ) h . ← htt Observe : N(o ) = I In → T o ← h or h - I N( 1) = 2 D ! ! = It N ( h - 1) t N ( h - 2) Nlh ) ÷ :* , - 6) > 23N ( h % 52in ( h - 2 i ) 5£ " Nlo ) 0 ⇒ i. %) ( since h - hi = 2h12 > 2h12 h 7 , for all N Ch ) I Claim : .
H12 ProotthatNlh)z2_ on h . By ind . NC0 ) =L 720=1 ; Basis : " - Ma N4 )= 2=2 - II. Suppose that , for some H71 , Nch ) > 2h " . II. Show that N( htt ) > zlht 'k : NlhtDz2Nlh - D ← B " " 7 µ m " > 2.2 § 4=2 It taller subtree shorter subtree of of height h +1+2 height = 2T h - I or h ( htt )t ✓ = 2 -
lomplaxityofAVLTreesearchkinser.tt nodes and height 1 is > with or any AVL tree n , N1D ⇒ 2% have : we log Nln ) > % " so in I 2 log.NL#a2logzn or = Of log n ) h so n ) ⇒ search takes time 0( log Insert takes time 0 ( login ) .
Base of logs in 0L ) Aside : . - ? h=0( login ) hi =0( log n ) not Why : Change of base : = t%a- = ( %) Logan n Iogbn ↳ gab e constant is multiplication by Change of base constant a , a big " " disappears - 0h s . inside . ⇒ fN=0( login ) for any fH=0( login ) K7 0KH . ( corrected )
k Comparison of 26g in n - • Worst-case height of BST AVL tree . vs Rogen n ÷ 13 100 1000 20 104 27 106 40 108 53 10 " 66 10k 80 1,000,000,000,000 = so this is big but not crazy ) a Terabyte , ( 10K bytes
AVLTreeRemov 1) Do BST removal . Cage 1 : key k is at a leaf i delete L . L node V that has 1 child Cage 2 " K is at a L . - the child L must be a leaf . v and delete - move the key L . L to ← target 6 =y¥¥ → on Esi ' ⑤ ' ① Cases : la is at a node v with 2 children , and successor L . • more key at successor L of K to V or I child : delete it eithe • L has 0 2. in case 1 a case as become nodes to The removal may have caused some un bale lanced . we deleted a leaf • Notice that , in each We use case , . this fact in describing how to re - balance .
AVLTreeRemoral.IS . w ← parent of deleted leaf - root ){ on path from W f- of each node . . - root a = w . - . if unbalanced u is subtree rooted at - Let T be the m u using suitable rotations - re balance not get smaller , exit . * . if height of T did g Correctness of the algorithm involves two properties : a) There is at most 1 unbalanced model after deletion Rebalancing may make an ancestor unbalanced . b)
Removal of an AVL Tree leaf causes at most 1 node to become unbalanced . ^ \ - :÷:÷:÷ I : : " ( ÷¥ ¥E:÷ :3 . :* node # unbalanced node nearest to • .
ANAhktreeremoralthatikustrat.es:1 re - balance after removal . Need to 2. Re - balancing may reduce height of a sub - tree remove ④ ¥¥ ④ I ⑤ ⑤ ② at right Io µ / I Io 5M¥ ④ ⑤ ' ⑤ ⑤
Amtrak ) End .
Recommend
More recommend