avl trees · 2011. 11. 21. · deletion from an avl tree first, do a normal binary search tree...

12
AVL TREES COP 3502

Upload: others

Post on 02-Feb-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • AVL

    TREES

    COP 3502

  • Deletion from an AVL Tree First, do a normal Binary Search Tree Delete:

    If the node is a leaf, remove it.

    If it has 1 child, replace with its child

    If it has 2 children, replace with the largest in its left subtree (inorder predecessor) and remove that node.

    After deletion, retrace the path back up the tree, starting with the parent of the replacement, to the root, adjusting the balance factor as needed.

  • Deletion from an AVL Tree

    In an insert there is at most one node that needs to be rebalanced.

    But in a delete there may be multiple nodes to be rebalanced.

    Technically only one rebalance that happens at a node, but once that happens it may affect the ancestral nodes.

  • Deletion Example

    Delete 8:

    32

    48 16

    8 24

    28

    40 56

    52 60 36 44

    58 62

  • Deletion Example

    32

    48 16

    24

    28

    40 56

    52 60 36 44

    58 62

  • Deletion Example

    32

    48

    40 56

    52 60 36 44

    58 62

    16

    24

    28 16

    32

    48

    56

    A

    B

    C

  • Deletion Example

    40 52 60

    36 44 58 62 16

    24

    28 16

    32

    48

    56

  • Choosing A,B,C for Delete Restructuring

    One thing that is more complicated about choosing the nodes A, B and C for the AVL Tree delete restructuring is that these nodes are NOT from the ancestral path followed from the origin of the delete. Clearly, if a delete will cause an

    imbalance, it will be because the subtree that contains the deleted node has become too short.

    Remember that the nodes A, B and C are always on the “longest” path to the bottom of the tree.

    This means that when we find an imbalanced node after deleting, the node to the opposite side is guaranteed to be down the longer path.

    30

    15 45

    35 50

    32 55

    10

    A

    B

    C

  • Choosing A,B,C for Delete Restructuring

    After labeling the first two nodes of A,B,C and there is still a choice between the right and left: If one side is longer than the other, choose that side.

    If the two sides are equal, go to the same side as the parent is to the grandparent.

    30

    15 45

    35 50

    32 55

    10

    A

    B

    C

    30

    15

    45

    35

    50

    32

    55

    10

    A

    B

    C

  • Choosing A,B,C for Delete Restructuring

    The following situation is similar if we delete 50 from the following tree:

    30

    15 45

    17 50

    20

    10

    A

    B

    C

    5

    30

    15

    45 17

    20

    10

    A

    B

    C

    5

  • AVL Tree Delete Examples The most simple example is when a node from

    a tree with four nodes gets deleted.

    Consider deleting 12 from the following tree:

    10

    5 12

    8

    A

    B

    C 8

    5 10

    A

    B C

    10

    5

    8

    C

    A

    B

  • AVL Tree Delete Examples

    Delete 30 from the following tree:

    20

    10 30

    15 25

    12

    5

    A

    B

    C

    25

    20

    10

    30 15

    12 5

    A

    B

    C

    25 20 10

    15

    12 5

    A

    B

    C

    25