avl tree deletion

Upload: rajan-jaiprakash

Post on 03-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 AVL Tree Deletion

    1/42

    Hash,

    AVL Tree/ Binary Tree deletion and

    Graph Creation

  • 8/12/2019 AVL Tree Deletion

    2/42

    What is a Hash Table ?

    The simplest kind of hash

    table is an array of records.

    This example has 701records.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

    An array of records

    . . .

    [ 700]

  • 8/12/2019 AVL Tree Deletion

    3/42

    What is a Hash Table ?

    Each record has a special

    field, called its key.

    In this example, the key is a

    long integer field called

    Number.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

    . . .

    [ 700]

    Number 506643548

  • 8/12/2019 AVL Tree Deletion

    4/42

    What is a Hash Table ?

    The number might be a

    person's identification

    number, and the rest of the

    record has informationabout the person.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

    . . .

    [ 700]

    Number 506643548

  • 8/12/2019 AVL Tree Deletion

    5/42

    What is a Hash Table ?

    When a hash table is in use,

    some spots contain valid

    records, and other spots are

    "empty".

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .

  • 8/12/2019 AVL Tree Deletion

    6/42

    Inserting a New Record

    In order to insert a new

    record, the keymust

    somehow be converted to

    an array index.

    The index is called the hash

    value of the key.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .

    Number 580625685

  • 8/12/2019 AVL Tree Deletion

    7/42

    Inserting a New Record

    Typical way create a hash

    value:

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .

    Number 580625685

    (Number mod 701)

    What is (580625685 mod 701) ?

  • 8/12/2019 AVL Tree Deletion

    8/42

    Inserting a New Record

    Typical way to create a hash

    value:

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .

    Number 580625685

    (Number mod 701)

    What is (580625685 mod 701) ?3

  • 8/12/2019 AVL Tree Deletion

    9/42

    Inserting a New Record

    The hash value is used for

    the location of the new

    record.

    Number 580625685

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .

    [3]

  • 8/12/2019 AVL Tree Deletion

    10/42

    Inserting a New Record

    The hash value is used for

    the location of the new

    record.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685

  • 8/12/2019 AVL Tree Deletion

    11/42

    Collisions

    Here is another new record

    to insert, with a hash value

    of 2.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685

    Number 701466868

    My hash

    value is [2].

  • 8/12/2019 AVL Tree Deletion

    12/42

    Collisions

    This is called a collision,

    because there is already

    another valid record at [2].

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685

    Number 701466868

    When a collision occurs,

    move forward until you

    find an empty spot.

  • 8/12/2019 AVL Tree Deletion

    13/42

    Collisions

    This is called a collision,

    because there is already

    another valid record at [2].

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685

    Number 701466868

    When a collision occurs,

    move forward until you

    find an empty spot.

  • 8/12/2019 AVL Tree Deletion

    14/42

    Collisions

    This is called a collision,

    because there is already

    another valid record at [2].

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685

    Number 701466868

    When a collision occurs,

    move forward until you

    find an empty spot.

  • 8/12/2019 AVL Tree Deletion

    15/42

    Collisions

    This is called a collision,

    because there is already

    another valid record at [2].

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    The new record goes

    in the empty spot.

  • 8/12/2019 AVL Tree Deletion

    16/42

    Searching for a Key

    The data that's attached to

    a key can be found fairly

    quickly.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Number 701466868

  • 8/12/2019 AVL Tree Deletion

    17/42

    Searching for a Key

    Calculate the hash value.

    Check that location of the array

    for the key.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Number 701466868

    My hash

    value is [2].

    Not me.

  • 8/12/2019 AVL Tree Deletion

    18/42

    Searching for a Key

    Keep moving forward until you

    find the key, or you reach an

    empty spot.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Number 701466868

    My hash

    value is [2].

    Not me.

  • 8/12/2019 AVL Tree Deletion

    19/42

    Searching for a Key

    Keep moving forward until you

    find the key, or you reach an

    empty spot.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Number 701466868

    My hash

    value is [2].

    Not me.

  • 8/12/2019 AVL Tree Deletion

    20/42

    Searching for a Key

    Keep moving forward until you

    find the key, or you reach an

    empty spot.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Number 701466868

    My hash

    value is [2].

    Yes!

  • 8/12/2019 AVL Tree Deletion

    21/42

    Searching for a Key

    When the item is found, the

    information can be copied to

    the necessary location.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Number 701466868

    My hash

    value is [2].

    Yes!

  • 8/12/2019 AVL Tree Deletion

    22/42

    Deleting a Record

    Records may also be deleted from a hash table.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]

    Number 506643548Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Please

    delete me.

  • 8/12/2019 AVL Tree Deletion

    23/42

    Deleting a Record

    Records may also be deleted from a hash table.

    But the location must not be left as an ordinary

    "empty spot" since that could interfere with

    searches.

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

  • 8/12/2019 AVL Tree Deletion

    24/42

    Deleting a Record

    [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 233667136Number 281942902

    Number 155778322

    . . .Number 580625685 Number 701466868

    Records may also be deleted from a hash table.

    But the location must not be left as an ordinary

    "empty spot" since that could interfere with

    searches.

    The location must be marked in some special way so

    that a search can tell that the spot used to have

    something in it.

  • 8/12/2019 AVL Tree Deletion

    25/42

    Binary Tree Deletion

  • 8/12/2019 AVL Tree Deletion

    26/42

  • 8/12/2019 AVL Tree Deletion

    27/42

  • 8/12/2019 AVL Tree Deletion

    28/42

  • 8/12/2019 AVL Tree Deletion

    29/42

  • 8/12/2019 AVL Tree Deletion

    30/42

  • 8/12/2019 AVL Tree Deletion

    31/42

  • 8/12/2019 AVL Tree Deletion

    32/42

    DELETING IN AVL TREES:

  • 8/12/2019 AVL Tree Deletion

    33/42

  • 8/12/2019 AVL Tree Deletion

    34/42

    -2, -1, 0, +1, +2

    -- , - , 0, +, ++

  • 8/12/2019 AVL Tree Deletion

    35/42

  • 8/12/2019 AVL Tree Deletion

    36/42

  • 8/12/2019 AVL Tree Deletion

    37/42

  • 8/12/2019 AVL Tree Deletion

    38/42

  • 8/12/2019 AVL Tree Deletion

    39/42

    Adjacency Matrix

    1

    2

    3

    04

    0 1 2 3 4

    0 false false true false false

    1 false false false true false

    2 false true false false true

    3 false false false false false

    4 false false false true false

  • 8/12/2019 AVL Tree Deletion

    40/42

    Adjacency Matrix

    Can be implemented with a two-dimensional

    array, e.g.:

    boolean[][] adjMatrix;adjMatrix = new boolean[5][5];

  • 8/12/2019 AVL Tree Deletion

    41/42

    Edge Lists

    Graphs can also be represented by creating

    a linked list for each vertex

    1

    2

    3

    04

    2

    null

    0

    3

    null

    1

    1

    2

    4

    null

    For each entry J in listnumber I, there is an edgefrom I to J.

  • 8/12/2019 AVL Tree Deletion

    42/42

    Thank You