07. AVL_Ballanced Binary Tree

download 07. AVL_Ballanced Binary Tree

of 26

Transcript of 07. AVL_Ballanced Binary Tree

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    1/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 11

    DATASTRUCTURES

    MAHESH GOYANIM AHATMA G ANDHI I NSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER

    [email protected]

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    2/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 22

    B ALANCEDB INARY

    TREE

    AVL TREE

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    3/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 33

    BST for 14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17

    14

    154

    9

    7

    183

    5

    16 20

    17

    DEGENERATE B ST

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    4/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 44

    1415

    4

    97

    18

    3

    5

    16

    20

    17Linked List!

    BST for 14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17

    DEGENERATE B ST

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    5/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 55

    B ALANCED B ST

    We should k eep th e tree balanc ed.On e idea would b e to hav e the left and right subtr ee s hav e th e sam e he ight

    Does not forc e th e tree to b e shallow.

    1415

    18

    16

    20

    174

    97

    3

    5

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    6/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 66

    We could insist that eve ry nod e must hav e le ft and right subtr ee s of sam e he ight.

    But this r eq uires that th e tree b e a com ple t e binary tr ee

    To do this, th e re must hav e (2 d+1 1) data it ems, wh e re d is th e dep th of th e tree .

    This is too rigid a condition.

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    7/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 77

    A Tr ee is called Balanc ed Binary Tr ee if eac h nod e poss esses on e of th e following prope rty:

    A nod e is ca lled Le ft h ea vy if th e dep th of L eft sub tr ee is on e long e rthan Right sub tr ee

    A node is ca lled Right h ea vy if th e dep th of Right sub tr ee is o ne longerthan Light sub tr ee

    A node is ca lled b a lance d if th e dep th of both th e sub tr ee is s ame .

    This tr ee is a lso k nown as AVL (Adelson Velskii and Landis) Tree

    An AVL tree is iden tica l to a BST excep t

    he ight of th e le ft an d right subtr ee s can diff e r by a t most 1.

    he ight of an empty tr ee is d e fine d to b e ( 1).

    Ba lance d Bina ry Tr ee is classified in to ca t egories :

    He ight B a lance d

    We ight B a lance d

    AVL tree increa ses th e e fficienc y by r edu cing th e sea rching tim e in com pa re to bi na ry tr ee

    TERMINOLOGY

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    8/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 88

    Graphical Representation

    A

    H

    C

    F

    I

    roo t

    Right sub tr eeLeft sub tr ee

    B

    E

    GG

    D

    B

    R

    L

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    9/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 99

    An AVL Tree An AVL Tree5

    82

    4

    3

    1 7

    level0

    1

    2

    3

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    10/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1010

    Not an AVL treeNot an AVL tree6

    81

    4

    3

    1

    5

    level0

    1

    2

    3

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    11/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1111

    The heig ht of a b ina ry tr ee i s th e maxi mum l eve l of it s lea ves ( a lso ca lled t he dept h).

    The b a lance of a node in a b ina ry t ree i s d e f ine d as t he heig ht of it s le f t sub t ree m inus h eig ht of it s r ig ht sub t ree .

    He re , for exa mple , i s a b a lance d t ree . Eac h nod e has an indicate d b a lance of 1, 0, or1.

    -1

    01

    0

    0

    0

    0

    -1

    0

    1

    00 0

    0 00 0

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    12/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1212

    If w e inse rt ne w node in AVL Tree , possibl e chan ges in st a tus of tr ee are as followi ng:

    The nod e was e ithe r left or h ea vy an d h as now b ecom e b a lance d

    The nod e was b a lance d an d h as now b ec om e left or right h ea vy

    The nod e is h ea vy an d ne w node is inse rt ed in hea vy sub tr ee , this crea t esunb a lance d tr ee , su ch a node is ca lled critica l node

    HEIGHT B ALANCED TREE

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    13/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1313

    HEIGHT B ALANCED TREE

    A We ight b a lance d tr ee is a bina ry tr ee in which additio na l we ight fi e ld is a lso th ere .

    The nod e of th e we ight b a lance d tr ee cont a ine d four fi e lds :

    Da t a Elemen t

    Le ft Child point er

    Right Child Poi nt er

    A Prob ability or We ight Fi e ld

    A node , whi ch is most acce ssible is given max imum priority an d s e t as a root

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    14/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1414

    INSERT NODE

    1. If this is first nod e th an a lloca t e node , s e t its fi e ld an dre tur n

    2. If DATA is a lrea dy presen t in tree th an disca rd DATA, e lse link ne w node an d r e tur n

    3. Sea rch u nb a lance d node

    4. Adjust for u nb a lance f ac tor, if th e re is no critica l nod e ,re tur n

    5. If node is b a lance d an d th an b ecom es h ea vy or th e nod e is h ea vy an d now b ecom es b a lance d th en adjust th e b a lance f ac tor

    an d r e tur n

    6. Reb a lance th e tree an d r e tur n.

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    15/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1515

    AVL TREE CREATION

    Inse rt(1)Inse rt(1)1

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    16/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1616

    AVL TREE CREATION

    Inse rt(2)Inse rt(2)1

    2

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    17/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1717

    AVL TREE CREATION

    Inse rt(3) si ngle le ft rot a tionInse rt(3) si ngle le ft rot a tion1

    2

    3

    -2

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    18/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1818

    AVL TREE CREATION

    Inse rt(3) si ngle le ft rot a tionInse rt(3) si ngle le ft rot a tion1

    2

    3

    -2

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    19/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1919

    AVL TREE CREATION

    Inse rt(3)Inse rt(3)

    1

    2

    3

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    20/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2020

    AVL TREE CREATION

    1

    2

    3

    4

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    21/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2121

    AVL TREE CREATION

    1

    2

    3

    4

    5

    -2

    Inse rt(5)Inse rt(5)

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    22/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2222

    AVL TREE CREATION

    Inse rt(5)Inse rt(5)

    1

    2

    3

    4

    5

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    23/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2323

    AVL TREE CREATION

    Inse rt(6)Inse rt(6)

    1

    2

    3

    4

    5

    6

    -2

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    24/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2424

    AVL TREE CREATION

    Inse rt(6)Inse rt(6)

    1

    2

    3

    4

    5

    6

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    25/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2525

    AVL TREE CREATION

    Inse rt(7)Inse rt(7)

    1

    2

    3

    4

    5

    6

    7

    -2

  • 8/8/2019 07. AVL_Ballanced Binary Tree

    26/26

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2626

    AVL TREE CREATION

    Inse rt(7)Inse rt(7)

    1

    2

    3

    4

    5

    6

    7