analysis algorithm 8

Upload: entistde

Post on 02-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Analysis Algorithm 8

    1/60

    AVL Trees

    1AVL TREE

  • 7/27/2019 Analysis Algorithm 8

    2/60

    Overview

    Binary treesBinary search trees

    Find

    Insert

    Delete

    AVL trees

    2

  • 7/27/2019 Analysis Algorithm 8

    3/60

    Hierarchical Data Structures

    Tree

    Single parent, multiple children

    Binary tree

    Tree with 02 children per node

    Tree Binary Tree3

  • 7/27/2019 Analysis Algorithm 8

    4/60

    Trees

    TerminologyRoot no predecessorLeaf no successorInterior non-leaf

    Height distance from root to leaf

    Root node

    Leaf nodes

    Interior nodes Height

    4

  • 7/27/2019 Analysis Algorithm 8

    5/60

    Types of Binary Trees

    Degenerate

    only one childBalanced mostly two children

    Complete always two children

    Degenerate

    binary tree

    Balanced

    binary tree

    Complete

    binary tree5

  • 7/27/2019 Analysis Algorithm 8

    6/60

    Binary Trees Properties

    DegenerateHeight = O(n) for n

    nodes

    Similar to linear list

    BalancedHeight = O( log(n) )

    for n nodes

    Useful for searches

    Degenerate

    binary tree

    Balanced

    binary tree6

  • 7/27/2019 Analysis Algorithm 8

    7/60

    Binary Search Trees

    Key propertyValue at node

    Smaller values in left subtree

    Larger values in right subtree

    Example

    X >Y

    X < Z

    Y

    X

    Z

    7

  • 7/27/2019 Analysis Algorithm 8

    8/60

    Binary Search Trees

    Examples

    Binary

    search trees

    Non-binary

    search tree

    5

    10

    30

    2 25 45

    5

    10

    45

    2 25 30

    5

    10

    30

    2

    25

    45

    8

  • 7/27/2019 Analysis Algorithm 8

    9/60

    Example Binary Searches

    Find ( 2 )

    5

    10

    30

    2 25 45

    5

    10

    30

    2

    25

    45

    10 > 2, left

    5 > 2, left2 = 2, found

    5 > 2, left

    2 = 2, found

    9

  • 7/27/2019 Analysis Algorithm 8

    10/60

    Binary Search Properties

    Time of search

    Proportional to height of tree

    Balanced binary tree

    O( log(n) ) time

    Degenerate tree

    O( n ) time

    Like searching linked list / unsorted array

    RequiresAbility to compare key values

    10

  • 7/27/2019 Analysis Algorithm 8

    11/60

    Binary Search Tree Insertion

    Algorithm

    Perform search for value X

    Search will end at node Y (if X not in tree)

    If X < Y, insert new leaf X as new left subtree for Y

    If X > Y, insert new leaf X as new right subtree for Y

    Observations

    O( log(n) ) operation for balanced tree

    Insertions may unbalance tree

    11

  • 7/27/2019 Analysis Algorithm 8

    12/60

    Example Insertion

    Insert ( 20 )

    5

    10

    30

    2 25 45

    10 < 20, right

    30 > 20, left

    25 > 20, left

    Insert 20 on left

    20

    12

  • 7/27/2019 Analysis Algorithm 8

    13/60

    Binary Search Tree Deletion

    Algorithm

    Perform search for value X

    If X is a leaf, delete X

    Else // must delete internal node

    a) Replace with largest value Y on left subtreeOR smallest value Z on right subtree

    b) Delete replacement value (Y or Z) fromsubtree

    ObservationO( log(n) ) operation for balanced tree

    Deletions may unbalance tree

    13

  • 7/27/2019 Analysis Algorithm 8

    14/60

    Example Deletion (Leaf)

    Delete ( 25 )

    5

    10

    30

    2 25 45

    10 < 25, right

    30 > 25, left

    25 = 25, delete

    5

    10

    30

    2 45

    14

  • 7/27/2019 Analysis Algorithm 8

    15/60

    Example Deletion (Internal Node)

    Delete ( 10 )

    5

    10

    30

    2 25 45

    5

    5

    30

    2 25 45

    2

    5

    30

    2 25 45

    Replacing 10

    with largest

    value in left

    subtree

    Replacing 5

    with largest

    value in left

    subtree

    Deleting leaf

    15

  • 7/27/2019 Analysis Algorithm 8

    16/60

    Example Deletion (Internal Node)

    Delete ( 10 )

    5

    10

    30

    2 25 45

    5

    25

    30

    2 25 45

    5

    25

    30

    2 45

    Replacing 10

    with smallest

    value in right

    subtree

    Deleting leaf Resulting tree

    16

  • 7/27/2019 Analysis Algorithm 8

    17/60AVL TREE

    17

    AVL - Good but not Perfect Balance

    AVL trees are height-balanced binarysearch trees

    Balance factorof a nodeheight(left subtree) - height(right subtree)

    An AVL tree has balance factor calculated atevery node

    For every node, heights of left and right subtreecan differ by no more than 1

    Store current heights in each node

  • 7/27/2019 Analysis Algorithm 8

    18/60AVL TREE

    18

    Node Heights

    1

    00

    2

    0

    6

    4 9

    81 5

    1

    height of node = h

    balance factor = hleft-hrightempty height = -1

    0

    0

    height=2 BF=1-0=1

    0

    6

    4 9

    1 5

    1

    Tree A

    (AVL)Tree B

    (AVL)

  • 7/27/2019 Analysis Algorithm 8

    19/60AVL TREE

    19

    Node Heights after Insert 7

    2

    10

    3

    0

    6

    4 9

    81 5

    1

    height of node = h

    balance factor = hleft-hrightempty height = -1

    1

    0

    2

    0

    6

    4 9

    1 5

    1

    07

    07

    balance factor

    1-(-1) = 2

    -1

    Tree A

    (AVL)Tree B (not

    AVL)

  • 7/27/2019 Analysis Algorithm 8

    20/60AVL TREE

    20

    Insert and Rotation in AVL Trees

    Insert operation may cause balance factor tobecome 2 or2 for some node

    only nodes on the path from insertion point toroot node have possibly changed in height

    So after the Insert, go back up to the root node bynode, updating heights

    If a new balance factor (the difference hleft-hright) is

    2 or

    2, adjust tree by rotat ion around the node

  • 7/27/2019 Analysis Algorithm 8

    21/60AVL TREE

    21

    Single Rotation in an AVL Tree

    2

    10

    2

    0

    6

    4 9

    81 5

    1

    0

    7

    0

    1

    0

    2

    0

    6

    4

    9

    8

    1 5

    1

    07

  • 7/27/2019 Analysis Algorithm 8

    22/60

    AVL TREE

    22

    Let the node that needs rebalancing be .

    There are 4 cases:

    Outside Cases (require single rotation) :

    1. Insertion into left subtree of left child of.2. Insertion into right subtree of right child of.

    Inside Cases (require double rotation) :

    3. Insertion into right subtree of left child of.4. Insertion into left subtree of right child of.

    The rebalancing is performed through four

    separate rotation algorithms.

    Insertions in AVL Trees

  • 7/27/2019 Analysis Algorithm 8

    23/60

    AVL TREE

    23

    j

    k

    X Y

    Z

    Consider a valid

    AVL subtree

    AVL Insertion: Outside Case

    h

    hh

  • 7/27/2019 Analysis Algorithm 8

    24/60

    AVL TREE

    24

    j

    k

    XY

    Z

    Inserting into X

    destroys the AVL

    property at node j

    AVL Insertion: Outside Case

    h

    h+1 h

  • 7/27/2019 Analysis Algorithm 8

    25/60

    AVL TREE

    25

    j

    k

    XY

    Z

    Do a right rotation

    AVL Insertion: Outside Case

    h

    h+1 h

  • 7/27/2019 Analysis Algorithm 8

    26/60

    AVL TREE

    26

    j

    k

    XY

    Z

    Do a right rotation

    Single right rotation

    h

    h+1 h

  • 7/27/2019 Analysis Algorithm 8

    27/60

    AVL TREE

    27

    j

    k

    X Y Z

    Right rotation done!

    (Left rotation is mirror

    symmetric)

    Outside Case Completed

    AVL property has been restored!

    h

    h+1

    h

  • 7/27/2019 Analysis Algorithm 8

    28/60

    AVL TREE

    28

    j

    k

    X Y

    Z

    AVL Insertion: Inside Case

    Consider a valid

    AVL subtree

    h

    hh

  • 7/27/2019 Analysis Algorithm 8

    29/60

    AVL TREE

    29

    Inserting into Y

    destroys the

    AVL property

    at node j

    j

    k

    X Y

    Z

    AVL Insertion: Inside Case

    Does right rotation

    restore balance?

    h

    h+1h

  • 7/27/2019 Analysis Algorithm 8

    30/60

    AVL TREE30

    j

    k

    X

    Y Z

    Right rotation

    does not restore

    balance now k is

    out of balance

    AVL Insertion: Inside Case

    hh+1

    h

  • 7/27/2019 Analysis Algorithm 8

    31/60

    AVL TREE31

    Consider the structure

    of subtree Y j

    k

    X Y

    Z

    AVL Insertion: Inside Case

    h

    h+1h

  • 7/27/2019 Analysis Algorithm 8

    32/60

    AVL TREE32

    j

    k

    XV

    Z

    W

    i

    Y = node i and

    subtrees V and W

    AVL Insertion: Inside Case

    h

    h+1h

    h or h-1

  • 7/27/2019 Analysis Algorithm 8

    33/60

    AVL TREE33

    j

    k

    XV

    Z

    W

    i

    AVL Insertion: Inside Case

    We will do a left-right

    double rotation . . .

  • 7/27/2019 Analysis Algorithm 8

    34/60

    AVL TREE34

    j

    k

    X V

    Z

    W

    i

    Double rotation : first rotation

    left rotation complete

  • 7/27/2019 Analysis Algorithm 8

    35/60

    AVL TREE35

    j

    k

    X V

    Z

    W

    i

    Double rotation : second

    rotationNow do a right rotation

  • 7/27/2019 Analysis Algorithm 8

    36/60

    AVL TREE36

    jk

    X V ZW

    i

    Double rotation : second

    rotationright rotation complete

    Balance has been

    restored

    hh h or h-1

  • 7/27/2019 Analysis Algorithm 8

    37/60

    AVL TREE37

    Example of Insertions in an AVL Tree

    1

    0

    220

    10 30

    25

    0

    35

    0

    Insert 5, 40

  • 7/27/2019 Analysis Algorithm 8

    38/60

  • 7/27/2019 Analysis Algorithm 8

    39/60

    AVL TREE39

    Single rotation (outside case)

    2

    0

    320

    10 30

    25

    1

    35

    2

    50

    20

    10 30

    25

    1

    405

    40

    0

    0

    0

    1

    2

    3

    45

    Imbalance

    3545

    0 0

    1

    Now Insert

    34

  • 7/27/2019 Analysis Algorithm 8

    40/60

    AVL TREE40

    Double rotation (inside case)

    3

    0

    320

    10 30

    25

    1

    40

    2

    50

    20

    10 35

    30

    1

    405

    45

    0 1

    2

    3

    Imbalan

    ce

    45

    0

    1

    Insertion of

    34

    35

    34

    0

    0

    125 34

    0

  • 7/27/2019 Analysis Algorithm 8

    41/60

    5

    3

    1 4

    Insert 3.5

    AVL Tree

    8

    3.5

    5

    3

    1 4

    8

    4

    5

    1

    3

    3.5 After Rotation

    x

    y

    A z

    B

    C

    8

    41

  • 7/27/2019 Analysis Algorithm 8

    42/60

    An Extended Example

    Insert 3,2,1,4,5,6,7, 16,15,14

    3

    Fig 1

    3

    2

    Fig 2

    3

    2

    1

    Fig 3

    2

    1 3Fig 4

    2

    1 3

    4

    Fig 5

    2

    1 3

    4

    5

    Fig 6

    Single rotation

    Single rotation

    42

    2

  • 7/27/2019 Analysis Algorithm 8

    43/60

    2

    1 4

    53

    Fig 7 6

    2

    1 4

    53

    Fig 8

    4

    2 5

    61 3

    Fig 9

    4

    2 5

    61 3

    7Fig 10

    4

    2 6

    71 3

    5 Fig 11

    Single rotation

    Single rotation

    43

  • 7/27/2019 Analysis Algorithm 8

    44/60

    4

    2 6

    71 3

    5 16

    Fig 12

    4

    2 6

    71 3

    5 16

    15Fig 13

    4

    2 6

    151 3 5

    167Fig 14

    Double rotation

    44

  • 7/27/2019 Analysis Algorithm 8

    45/60

    5

    4

    2 7

    151 3

    6

    1614

    Fig 16

    4

    2 6

    15

    13

    516

    7

    14

    Fig 15

    Double rotation

    45

  • 7/27/2019 Analysis Algorithm 8

    46/60

    Remove Operation in AVL Tree

    Removing a node from an AVL Tree is the same

    as removing from a binary search tree.

    However, it may unbalance the tree.

    Similar to insertion, starting from the removed

    node we check all the nodes in the path up tothe root for the first unbalance node.

    Use the appropriate single or double rotation to

    balance the tree.May need to continue searching for unbalanced

    nodes all the way to the root.

    46

  • 7/27/2019 Analysis Algorithm 8

    47/60

    Deletion X in AVL Trees

    Deletion:

    Case 1: if X is a leaf, delete X

    Case 2: if X has 1 child, use it to replace X

    Case 3: if X has 2 children, replace X with its inorder

    predecessor(and recursively delete it)Rebalancing

    47

  • 7/27/2019 Analysis Algorithm 8

    48/60

    Delete 55 (case 1)

    60

    20 70

    10 40 65 85

    5 15 30 50 80 90

    55

    48

    D l ( 1)

  • 7/27/2019 Analysis Algorithm 8

    49/60

    Delete 55 (case 1)

    60

    20 70

    10 40 65 85

    5 15 30 50 80 90

    55

    49

    D l t 50 ( 2)

  • 7/27/2019 Analysis Algorithm 8

    50/60

    Delete 50 (case 2)

    60

    20 70

    10 40 65 85

    5 15 30 50 80 90

    55

    50

    D l t 50 ( 2)

  • 7/27/2019 Analysis Algorithm 8

    51/60

    Delete 50 (case 2)

    60

    20 70

    10 40 65 85

    5 15 3050 80 90

    55

    51

    D l t 60 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    52/60

    Delete 60 (case 3)

    60

    20 70

    10 40 65 85

    5 15 30 50 80 90

    55

    prev

    52

    D l t 60 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    53/60

    Delete 60 (case 3)

    55

    20 70

    10 40 65 85

    5 15 30 50 80 90

    53

    D l t 55 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    54/60

    Delete 55 (case 3)

    55

    20 70

    10 40 65 85

    5 15 30 50 80 90

    prev

    54

    D l t 55 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    55/60

    Delete 55 (case 3)

    50

    20 70

    10 40 65 85

    5 15 30 80 90

    55

    D l t 50 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    56/60

    Delete 50 (case 3)

    50

    20 70

    10 40 65 85

    5 15 30 80 90

    prev

    56

    D l t 50 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    57/60

    Delete 50 (case 3)

    40

    20 70

    10 30 65 85

    5 15 80 90

    57

    D l t 40 ( 3)

  • 7/27/2019 Analysis Algorithm 8

    58/60

    Delete 40 (case 3)

    40

    20 70

    10 30 65 85

    5 15 80 90

    prev

    58

    Delete 40 Rebalancing

  • 7/27/2019 Analysis Algorithm 8

    59/60

    Delete 40 : Rebalancing

    30

    20 70

    10 65 85

    5 15 80 90

    Case ?

    59

  • 7/27/2019 Analysis Algorithm 8

    60/60