avl tree notes

Upload: mayank-singh

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 AVL Tree Notes

    1/17

    ES 103 Data Structures and Algorithms

    Atul Gupta 1

    AVL Trees

    Atul Gupta

    AVL Trees [G.M. Adelson Vels!" and E.M. Landis#

    $hat is the ma%or disad&antage o' (ST) $hat is the ma%or ad&antage o' (ST)

    Most operations on a (ST ta!e time proportional tothe height o' the tree

    Sel' *alancing *inar" trees sol&e this pro*lem *"per'orming trans'ormations on the tree at !e"times+ in order to reduce the height.

    Although a certain o&erhead is in&ol&ed+ it is %usti'ied in the long run *" ensuring 'aste,ecution o' later operations.

  • 8/10/2019 AVL Tree Notes

    2/17

    ES 103 Data Structures and Algorithms

    Atul Gupta -

    roperties o' AVL Trees

    the heights o' the t/o child su* trees o' an"node di''er *" at most one

    Loo!up search + insertion+ and deletion allta!e 2 log n time in *oth the a&erage and/orst cases

    nsertions and deletions ma" re4uire the treeto *e rebalanced *" one or more treerotations

    (alance 5actor

    The balance factor o' a node , is the height ofits left sub-tree minus the height of its rightsub-tree

    balance factor (x) = h(left-sub-tree) h(right-sub-tree) A node /ith a *alance 'actor 1+ 0+ or 1 is

    considered *alanced

  • 8/10/2019 AVL Tree Notes

    3/17

    ES 103 Data Structures and Algorithms

    Atul Gupta 3

    6omputing the 7eight o' a 8ode in(ST

    height node 9i' node :: null

    return 1else

    return ma, height node.le't +height node.right ; 1right;right : root le'tleft;

    root le't :