10-avltree

Upload: frankjamison

Post on 03-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 10-AVLTree

    1/36

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 1

    AVL Trees

    and

    Heaps

  • 8/12/2019 10-AVLTree

    2/36

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 2

    AVL Trees

    So far balancing the tree was done globally

    Basically every node was involved in the balance operation

    Tree balancing can however be done locally if only aportion of the tree is affected. An example of this is called

    AVL tree

    AVL tree was proposed by Adelson, Velskii and Landis

  • 8/12/2019 10-AVLTree

    3/36

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 3

    AVL Trees

    An AVL tree is one in which the height of the left and rightsub-trees of every node differ by at most one

    For example consider the following tree

    19| 08| +1

    15| -1

    12| 0

    There are two values in each node: one is

    the actual value of the node and the other is

    called balancing factorof the node.

    The balancing factor of every node in an AVL tree is either 0, +1, or -1.

    For each node, P, the

    balancing factor of P = height of right sub-tree of Pheight of left sub-tree of P

  • 8/12/2019 10-AVLTree

    4/36

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 4

    AVL Trees -- Continue

    If a new node is inserted to an AVL tree or a node is deleted

    from an AVL tree, the balancing factor of some nodes

    changes. In order to make the new modified tree an AVL

    tree again we may have to rotate some nodes

    Insertion or deletion may cause the balance factor of some

    nodes to be +2 or -2. These nodes have to be tracked and

    based on appropriate algorithms they should be properly

    rotated to right/left to make the tree a balanced AVL tree

  • 8/12/2019 10-AVLTree

    5/36

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 5

    Inserting into an AVL Tree

    Algorithm:

    Insert the new node, node N, into the tree the same way youinsert N into a binary search tree

    Follow the path from node N to the root. This is your insertion

    path

    Insertion of node N may change the balancing factors of somenodes in the insertion path

    After inserting node N, start updating the balancing factors ofthe nodes in the insertion path until you reach the first nodewith balancing factor of + 2 or -2. If such a node does not existin the insertion path, the tree is already balanced; otherwisemark that node, node P and go to the next step

  • 8/12/2019 10-AVLTree

    6/36

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 6

    R = P -> Right

    L = P -> Left

    If the new node that was just inserted, Node N, is in theright sub-tree of R

    Left Rotate R around P

    else if Node N is in the left sub-tree of R

    C = R ->Left

    Right rotate C around R Left rotate C around P

    else if Node N, is in the left sub-tree of L

    Right Rotate L around P

    else if Node N is in the right sub-tree of L C = L ->Right

    Left rotate C around L

    Right rotate C around P

    Case1

    Case 2

    Case 3

    Case 4

  • 8/12/2019 10-AVLTree

    7/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 7

    L R

    P

    C C

    .

    .

    .

    Right rotateof C around

    Rthen,

    left rotate ofC around P

    Left rotate

    of R

    around P

    Right

    rotate of L

    around P

    Left rotate ofC around L

    then,right rotate of

    C around P

    Suppose node P is the first

    node on the insertion path

    in which its balance factor

    is changed to +2 or -2

  • 8/12/2019 10-AVLTree

    8/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 8

    Example of Case 1:

    23| 0

    19| +18 | 0

    15| 0

    12 | 05 | 0

    23| ?

    19| ?8 | 0

    15| ?

    12 | 05 | 0

    30| 0

    Step 1

    Step 2

    Original Tree

    Inserting the

    node 30 into the

    tree

  • 8/12/2019 10-AVLTree

    9/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 9

    23| +1

    19| +28 | 0

    15| ?

    12 | 05 | 0

    30| 0

    Stop HereP

    R

    23| 0

    19| 0

    8 | 0

    15| 0

    12 | 05 | 0 30| 0

    R

    P

    Step 3

    Step 4

    Following the

    insertion path to

    find node P withbalancing factor

    +2 or -2

    R is rotated

    around P tobalance the tree

    (case 1)

  • 8/12/2019 10-AVLTree

    10/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 10

    Example of Case 2:

    23| 0

    19| +18 | 0

    15| 0

    12 | 05 | 0

    23| ?

    19| ?8 | 0

    15| ?

    12 | 05 | 0

    21| 0

    Original Tree

    Inserting the

    node 21 into the

    tree

    Step 1

    Step 2

  • 8/12/2019 10-AVLTree

    11/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 11

    23| -1

    19| +28 | 0

    15| ?

    12 | 05 | 0

    21 | 0

    Stop HereP

    R

    C

    21| 0

    19| 0

    8 | 0

    15| 0

    12 | 05 | 0 23| 0

    C

    P R21| ?

    19| ?8 | 0

    15| ?

    12 | 05 | 0

    23| ?R

    P

    C

    Following the

    insertion path to

    find node P withbalancing factor

    +2 or -2

    Making a double rotations to balance the tree (case 2)

    Step 3

    Step 4

  • 8/12/2019 10-AVLTree

    12/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 12

    Example of Case 3:

    19| 08 | 0

    15| -1

    12 | 05 | 0

    19| 08 | ?

    15| ?

    12 | 05 | ?

    2| 0

    Step 1

    Step 2

    Original Tree

    Inserting the

    node 2 into the

    tree

  • 8/12/2019 10-AVLTree

    13/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 13

    P

    L

    5 | -1

    8| 0

    12 | 02 | 0

    P

    Step 3

    Step 4

    Following the

    insertion path to

    find node P withbalancing factor

    +2 or -2

    L is rotated

    around P tobalance the tree

    (case 3)

    19| 08 | -1

    15| -2

    12 | 05 | -1

    2| 0

    Stop Here

    15| 0

    19| 0

    L

  • 8/12/2019 10-AVLTree

    14/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 14

    Example of Case 4:

    19| 08 | -1

    15| -1

    5 | 0

    19| 08 | ?

    15| ?

    5 | ?

    6| 0

    Step 1

    Step 2

    Original Tree

    Inserting the

    node 6 into the

    tree

  • 8/12/2019 10-AVLTree

    15/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 15

    Step 3

    Step 4

    Following the

    insertion path to

    find node P withbalancing factor

    +2 or -2

    P

    L

    19| 08 | -2

    15| ?

    5 | +1

    6 | 0

    Stop

    Here

    C

    PL

    19| 06 | 0

    15| -1

    5 | 0 8 | 0

    C

    Making a double rotations to balance the tree (case 4)

    P

    L

    19| 08 | ?

    15| ?

    6 | ?

    5 | ?

    C

  • 8/12/2019 10-AVLTree

    16/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 16

    Inserting a series of numbers into a AVL Tree

    We want to insert numbers 2, 8, 6, 10, 15, 4, 3, 20, 13, 18

    2 | 0Insert 2

    Insert 8

    2 | +1

    8 | 0

    No rotation is required

    because there is no

    node with balance

    factor of +2 or -2

    No rotation is required

    because there is no

    node with balance

    factor of +2 or -2

  • 8/12/2019 10-AVLTree

    17/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 17

    Insert 6

    Insert 10

    2 | +2

    8 | -1

    6 | 0

    2 | +2

    6 | +1

    8 | 0

    P

    R

    C

    6 | 0

    8 | 02 | 0

    P

    C

    R

    C

    RP

    Double Rotation is required

    6 | +1

    8 | +12 | 0

    10 | 0

    No rotation is required

    because there is no

    node with balance

    factor of +2 or -2

  • 8/12/2019 10-AVLTree

    18/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 18

    Insert 15

    Insert 4

    6 | ?

    8 | +22 | 0

    10 |+1

    15 | 0

    P

    R

    6 | +1

    10 | 02 | 0

    15 |08 | 0

    R

    P

    6 | 0

    10 | 02 | +1

    15 |08 | 04 | 0

    No rotation is required

    because there is nonode with balance

    factor of +2 or -2

    Single rotation is required

  • 8/12/2019 10-AVLTree

    19/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 19

    Insert 3

    6 | ?

    10 | 02 | +2

    15 |08 | 04 | -1

    3 | 0

    P

    R

    C

    6 | ?

    10 | 02 | +2

    15 |08 | 0

    4 | 0

    3 | +1

    P

    R

    C

    6 | 0

    10 | 03 | 0

    15 |08 | 02 | 0 4 | 0

    PR

    C

  • 8/12/2019 10-AVLTree

    20/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 20

    Insert 20 6 | +1

    10|+13 | 0

    15|+18 | 02 | 0 4 | 0

    20 |0

    Insert 13 6 | +1

    10|+13 | 0

    15|08 | 02 | 0 4 | 0

    20 |013 | 0

    No rotation is required

    because there is no node

    with balance factor of +2

    or -2

    No rotation is required

    because there is no node

    with balance factor of +2

    or -2

  • 8/12/2019 10-AVLTree

    21/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 21

    Insert 186 | +1

    10|+23 | 0

    15|+18 | 02 | 0 4 | 0

    20 |-113 | 0

    18 | 0

    RL

    P

    6 | +1

    15|03 | 0

    20|-110 | 02 | 0 4 | 0

    8 |-1 13 | 0 18 | 0

    Single rotation is required

  • 8/12/2019 10-AVLTree

    22/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 22

    Deleting From an AVL Tree

    This can be more time consuming than insertion. First we need toapply a delete method to get rid of the node

    We can use the delete method that finds either the successor orpredecessor node to replace a node that has both left and rightchildren

    After the node is deleted, the balance factors are updated from theparent of the deleted nodeup to the root. This is the delete path

    For each node on the delete path whose balance factor becomes +2or -2, a single or a double rotations has to be performed to restorethe balance of the tree

    IMPORTANT NOTE: The balancing of the tree DOES NOTSTOP after the first node P with balancing factor +2 or -2 found inthe delete path. You still need to continue searching for other nodeswith balancing factor +2 or -2

  • 8/12/2019 10-AVLTree

    23/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 23

    Delete Algorithm -- AVL Tree

    1. Start from the parent of the deleted node and move up the

    tree (delete path)2. If you find a node with balance factor +2 or -2 then stop

    and do the balancing as follows3. Mark the node P4. If the deleted node is in the left sub-tree of P

    Q = P -> RightIf balance factor of Q is +1 or 0

    rotate Q around Pelse // the balance factor is -1

    R = Q -> Left

    rotate R around Qrotate R around P

    endif

    5. else if the .

    Case1

    Case2

  • 8/12/2019 10-AVLTree

    24/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 24

    Delete Algorithm -- AVL TreeCont.

    5. else if the deleted node is in the right sub-tree of P

    Q = P -> Leftif balance factor of Q is -1 or 0

    rotate Q around P

    else// the balance factor is +1

    R = Q -> Right

    rotate R around Q

    rotate R around P

    end if

    6. Continue moving up the tree on the delete path. If youfind another node with balancing factor +2 or -2 go to

    step 3; otherwise, you are done

    Case3

    Case4

  • 8/12/2019 10-AVLTree

    25/36Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 25

    Example of Case 1:

    25| 0

    20| +15 | 0

    10| +1

    8 | 03 | 0

    Step 1

    Original Tree

    Delete 10

    Delete method:

    Find successor of10 which is 15

    15| 0

    30| 023| 0

    25| 0

    20| +15 | 0

    10| +1

    8 | 03 | 0

    Step 2

    15| 0

    30| 023| 0

  • 8/12/2019 10-AVLTree

    26/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 26

    Start from node

    20 (the parent of

    deleted node)

    and find the firstnode with

    balancing factor

    +2 or -2

    25| 0

    20| +25 | 0

    15| ?

    8 | 03 | 0

    Step 4

    30| 023| 0

    25| 0

    20| ?5 | 0

    15| ?

    8 | 03 | 0 10| 0

    30| 023| 0

    Swap 10 and 15

    And

    Delete 10

    P

    Step 3

  • 8/12/2019 10-AVLTree

    27/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 27

    Because thedeleted node wasin the left side of

    P, we have

    Q = P -> Right

    Note: Balancefactor of Q is 0

    Step 6

    25| -1

    23| 0

    5 | 0

    15| +1

    8 | 03 | 0 30| 020|+1

    Q

    25| 0

    20| +25 | 0

    15| ?

    8 | 03 | 0

    30| 023| 0

    P

    Q

    Because the

    balancing factor of

    Q was 0, we rotate

    Q around P

    Step 5

    P

    E l f C 2

  • 8/12/2019 10-AVLTree

    28/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 28

    Example of Case 2:

    25| -1

    20| +15 | 0

    10| +1

    8 | 03 | 0

    Step 1

    Original Tree

    Delete 10

    Delete method:

    Find successor of10 which is 15

    15| 0

    23| 0

    25| -1

    20| +15 | 0

    10| +1

    8 | 03 | 0

    Step 2

    15| 0

    23| 0

  • 8/12/2019 10-AVLTree

    29/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 29

    Start from node

    20 (the parent of

    deleted node)

    and find the firstnode with

    balancing factor

    +2 or -2

    25| -1

    20| +25 | 0

    15| ?

    8 | 03 | 0

    Step 4

    23| 0

    25| -1

    20| ?5 | 0

    15| ?

    8 | 03 | 0 10| 0

    23| 0

    Swap 10 and 15

    And

    Delete 10

    P

    Step 3

  • 8/12/2019 10-AVLTree

    30/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 30

    Because the deletednode was in the leftside of P, we have

    Q = P -> Right

    Note: Balance factorof Q is -1, so

    R = Q -> Left

    Because the balancing factor of Q was -1, we do two rotations

    25| -1

    20| +25 | 0

    15| ?

    8 | 03 | 0

    23| 0

    P

    Q

    Step 5

    R

    Step 6

    20| ?

    25| ?

    5 | 0

    15| ?

    8 | 03 | 0 23| ?

    P

    R

    Q25| 0

    23| 05 | 0

    15| 0

    8 | 03 | 0 20| 0

    R

    QP

  • 8/12/2019 10-AVLTree

    31/36

  • 8/12/2019 10-AVLTree

    32/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 32

    Start from node 5

    (the parent of

    deleted node)

    and find the firstnode with

    balancing factor

    +2 or -2Step 4

    Swap 10 and 8

    and

    Delete 10Step 3

    25| 0

    20| 05 | ?

    8| ?

    10 | 03 | 0 15| 0

    4 | 01 | 0

    25| 0

    20| 05 | -2

    8| ?

    3 | 0 15| 0

    4 | 01 | 0

    P

  • 8/12/2019 10-AVLTree

    33/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 33

    Because thedeleted node wasin the right side

    of P, we have

    Q = P -> Left

    Note: Balancefactor of Q is 0

    Step 6

    Because the

    balancing factor of

    Q was 0, we rotate

    Q around P

    Step 5

    25| 0

    20| 05 | -2

    8| ?

    3 | 0 15| 0

    4 | 01 | 0

    P

    Q

    25| 0

    20| 0

    5 | -1

    8| -1

    3 | +1

    15| 0

    4 | 0

    1 | 0

    P

    Q

    E l f C 4

  • 8/12/2019 10-AVLTree

    34/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 34

    Example of Case 4:

    25| 0

    20| 05 | -1

    10| -1

    8 | 03 | +1

    Step 1

    Original Tree

    Delete 8

    Delete method:

    Because node 8 isa leaf, it can be

    simply deleted

    15| 0

    4 | 0

    Step 2

    25| 0

    20| 05 | ?

    10| ?

    8 | 03 | +1 15| 0

    4 | 0

  • 8/12/2019 10-AVLTree

    35/36

    Dr. Ahmad R. HadaeghA.R. Hadaegh Cali for nia State University San Marcos (CSUSM) Page 35

    Start from node 5

    (the parent of

    deleted node)

    and find the firstnode with

    balancing factor

    +2 or -2

    Step 4

    Step 3

    25| 0

    20| 05 | -2

    10| ?

    3 | +1 15| 0

    P

    25| 0

    20| 05 | -2

    10| ?

    3 | +1 15| 0

    4 | 0

    P

    Q

    Because the deletednode was in the

    right side of P, wehave

    Q = P -> Left

    Note: Balance factorof Q is +1, so

    R = Q -> Right

    4 | 0

    R

  • 8/12/2019 10-AVLTree

    36/36

    25| 0

    20| 05 | ?

    10| ?

    4 | ? 15| 0

    3 | ?

    P

    Q

    R

    25| 0

    20| 04 | 0

    10| 0

    3 | 0 15| 05 | 0PQ

    R

    Because the balancing factor of Q was +1, we do two rotations

    Step 5