AVL Trees - Horowitz Sahni CPP - Lec43

download AVL Trees - Horowitz Sahni CPP - Lec43

of 31

Transcript of AVL Trees - Horowitz Sahni CPP - Lec43

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    1/31

    Dynamic Dictionaries

    Primary Operations: Get(key) =>search

    Insert(key, element) =>insert

    Delete(key) =>delete

    Additional operations: Ascend()

    Get(index)

    Delete(index)

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    2/31

    omplexity O! Dictionary Operations

    Get(), Insert() and Delete()

    Data "tr#ct#re $orst ase %xpected

    &ash 'ale O(n) O()

    *inary "earch

    'ree

    O(n) O(lo+ n)

    *alanced

    *inary "earch'ree

    O(lo+ n) O(lo+ n)

    nis n#mer o! elements in dictionary

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    3/31

    omplexity O! Other Operations

    Ascend(), Get(index), Delete(index)

    Data "tr#ct#re Ascend Get and Delete

    &ash 'ale O(D n lo+ n) O(D n lo+ n)

    Indexed *"' O(n) O(n)

    Indexed

    *alanced *"'

    O(n) O(lo+ n)

    Dis n#mer o! #ckets

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    4/31

    A-. 'ree

    inary tree

    !or e/ery node x, de!ine its alance !actor

    alance !actor o! x = hei+ht o! le!t s#tree o! x

    0 hei+ht o! ri+ht s#tree o! x

    alance !actor o! e/ery nodexis0 ,1, or

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    5/31

    *alance 2actors

    1 1

    11

    1

    3 1

    1

    3

    3

    'his is an A-. tree4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    6/31

    &ei+ht O! An A-. 'ree

    'he hei+ht o! an A-. tree that has nnodes is atmost 455 lo+6(n6)4

    'he hei+ht o! e/ery nnode inary tree is at least

    lo+6(n)4

    lo+6(n) 7= hei+ht 7=455 lo+6(n6)

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    7/31

    Proo! O! 8pper *o#nd On &ei+ht

    .et9h=min o! nodes in an A-. tree

    ;hose hei+ht is h4

    91= 14

    9)= 4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    8/31

    9h, h >

    *oth L andR are A-. trees4 'he hei+ht o! one is h34

    'he hei+ht o! the other is h364

    'he s#tree ;hose hei+ht is h3 has9h3nodes4

    'he s#tree ;hose hei+ht is h36 has9h36nodes4

    "o,9h=9h3 9h36 4

    L R

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    9/31

    2ionacci 9#mers

    21= 1, 2= 4

    2i=2i3 2i36, i > 4

    91= 1, 9= 4 9h=9h3 9h36 , i > 4

    9h=2h60 4

    2i< isrt(?)4

    = (1 + srt(5))/2.

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    10/31

    A-. "earch 'ree

    1 1

    1 1

    1

    3 1

    1

    3

    3

    1

    @

    B

    ?

    B1

    51

    61

    6?

    B?

    5?

    C1

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    11/31

    Insert()

    1 1

    1 1

    1

    3 1

    1

    3

    3

    1

    3

    1

    1

    @

    B

    ?

    B1

    51

    61

    6?

    B?

    5?

    C1

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    12/31

    Insert(6)

    1 1

    1 1

    1

    1

    1

    3

    3

    1

    @

    B

    ?

    B1

    51

    61

    6?

    B?

    5?

    C1

    6

    1

    3

    336

    EE imalance=>ne; node is inri+ht s#tree o! ri+ht s#tree o!

    ;hite node (node ;ith ! =06)

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    13/31

    1 1

    11

    1

    1

    1

    3

    3

    1

    @

    B

    ?

    B1

    51

    6?B?

    5?

    C11

    EE rotation4

    611

    6

    Insert(6)

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    14/31

    Insert

    2ollo;in+ insert, retrace path to;ards root andadF#st alance !actors as needed4

    "top ;hen yo# reach a node ;hose alance

    !actor ecomes 1, 6, or06, or ;hen yo# reachthe root4

    'he ne; tree is not an A-. tree only i! yo#reach a node ;hose alance !actor is either 6or064

    In this case, ;e say the tree has ecome#nalanced4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    15/31

    A39ode

    .et Ae the nearest ancestor o! the ne;ly

    inserted node ;hose alance !actor

    ecomes 6or06!ollo;in+ the insert4 *alance !actor o! nodes et;een ne; node

    and Ais 1e!ore insertion4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    16/31

    Imalance 'ypes

    EE ne;ly inserted node is in the ri+ht

    s#tree o! the ri+ht s#tree o! A4

    .. le!t s#tree o! le!t s#tree o! A4

    E.le!t s#tree o! ri+ht s#tree o! A4

    .Eri+ht s#tree o! le!t s#tree o! A4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    17/31

    .. Eotation

    "#tree hei+ht is #nchan+ed4

    9o !#rther adF#stments to e done4

    *e!ore insertion4

    1

    A

    B

    BL BR

    AR

    h h

    h

    A

    B

    BL BR

    AR

    A!ter insertion4

    h h

    h

    B

    A

    A!ter rotation4

    BRh

    ARh

    BL

    h

    1

    1

    6

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    18/31

    .E Eotation (case )

    "#tree hei+ht is #nchan+ed4

    9o !#rther adF#stments to e done4

    *e!ore insertion4

    1

    A

    B

    A

    B

    A!ter insertion4

    C

    C

    A

    A!ter rotation4

    B

    1

    3

    6

    1 1

    1

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    19/31

    .E Eotation (case 6)

    "#tree hei+ht is #nchan+ed4

    9o !#rther adF#stments to e done4

    C

    A

    CRh3

    ARh

    1

    A

    B

    BL

    CR

    AR

    h

    h3

    h

    1

    CL

    h3

    C

    A

    B

    BL

    CR

    AR

    h

    h3

    h

    CLh

    C

    B

    BLh

    CLh

    3

    6

    1 3

    1

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    20/31

    .E Eotation (case B)

    "#tree hei+ht is #nchan+ed4

    9o !#rther adF#stments to e done4

    1

    A

    B

    BL

    CR

    AR

    h

    h3

    h

    1

    CL

    h3

    C

    A

    B

    BL

    CR

    AR

    h

    h

    h

    CLh3

    C3

    3

    6 C

    A

    CRh

    ARh

    B

    BLh

    CLh3

    1

    1

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    21/31

    "in+le H Do#le Eotations

    "in+le

    .. and EE

    Do#le

    .E and E.

    .E is EE !ollo;ed y ..

    E. is .. !ollo;ed y EE

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    22/31

    .E Is EE ..

    A

    B

    BL

    CR

    AR

    h

    h

    h

    CLh3

    C3

    3

    6

    A!ter insertion4

    A

    C

    CL

    CR

    AR

    h

    h

    BLh

    B

    6

    A!ter EE rotation4

    h3

    C

    A

    CRh

    ARh

    B

    BLh

    CLh3

    1

    1

    A!ter .. rotation4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    23/31

    Delete An %lement

    1 1

    11

    1

    3 1

    1

    3

    3

    1

    @

    B

    ?

    B1

    51

    61

    6?

    B?

    5?

    C1

    Delete 4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    24/31

    Delete An %lement

    1 1

    1

    6

    1

    3 1

    1

    3

    3

    1

    @

    B

    ?

    B1

    51

    61

    6?

    B?

    5?

    C1

    .et e parent o! deleted node4

    Eetrace path !rom to;ards root4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    25/31

    9e; *alance 2actor O!

    Deletion !rom le!t s#tree o! =>!334 Deletion !rom ri+ht s#tree o! =>!4

    9e; alance !actor = or0 =>no chan+e in hei+ht o!

    s#tree rooted at 4

    9e; alance !actor = 1=>hei+ht o! s#tree rooted at

    has decreased y4

    9e; alance !actor = 6or06 =>tree is #nalanced at 4

    q

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    26/31

    Imalance lassi!ication

    .et Ae the nearest ancestor o! the deleted

    node ;hose alance !actor has ecome 6or06!ollo;in+ a deletion4

    Deletion !rom le!t s#tree o! A =>type .4

    Deletion !rom ri+ht s#tree o! A =>type E4 'ype E =>ne;!(A) = 64

    "o, old!(A) = 4

    "o, Ahas a le!t child *4 !(*) = 1 =>E14

    !(*) = =>E4

    !(*) = 0 =>E34

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    27/31

    E1 Eotation

    "#tree hei+ht is #nchan+ed4

    9o !#rther adF#stments to e done4

    "imilar to ..rotation4

    *e!ore deletion4

    1

    A

    B

    BL BR

    AR

    h h

    h

    B

    A

    A!ter rotation4

    BRh

    ARh3

    BL

    h

    3A

    B

    BL BR

    AR

    A!ter deletion4

    h h

    h3

    1

    6

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    28/31

    E Eotation

    "#tree hei+ht is red#ced y 4

    #st contin#e on path to root4

    "imilar to ..and E1rotations4

    *e!ore deletion4

    A

    B

    BL BR

    AR

    h h3

    h

    B

    A

    A!ter rotation4

    BRh3

    ARh3

    BL

    h

    1

    1A

    B

    BL BR

    AR

    A!ter deletion4

    h h3

    h3

    6

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    29/31

    E3 Eotation

    9e; alance !actor o! Aand *depends on4

    "#tree hei+ht is red#ced y 4

    #st contin#e on path to root4

    "imilar to .E4

    3

    A

    B

    BL

    CR

    AR

    h3

    h

    CL

    C

    A

    B

    BL

    CR

    AR

    h3

    h3

    CL

    C

    3

    6 C

    A

    CR ARh3

    B

    BLh3

    CL

    1

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    30/31

    9#mer O! Eealancin+ Eotations

    At most !or an insert4

    O(lo+ n)!or a delete4

  • 7/23/2019 AVL Trees - Horowitz Sahni CPP - Lec43

    31/31

    Eotation 2re#ency

    Insert random n#mers4

    9o rotation ?B45J (approx)4

    ..EE 6B4BJ (approx)4

    .EE. 6B46J (approx)4