deleting from b-trees

Upload: deeksha-ranjan

Post on 07-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Deleting From B-Trees

    1/27

    Deleting from B-

    Trees

    Eric Nevala

  • 8/3/2019 Deleting From B-Trees

    2/27

    Objective: Teach you the concept

    of deleting keys from a B-Tree

    1. The concept of deleting from a B-Tree

    2. A graphical illustration of all the cases ofdeletion

    3. Look at some implementations in C++code

    4. Do some practical exercises for practice(cards -> whiteboard)

  • 8/3/2019 Deleting From B-Trees

    3/27

    The Concept

    You can delete a key entry from any node.

    ->Therefore, you must ensure that

    before/after deletion, the B-Tree maintainsits properties.

    When deleting, you have to ensure that a

    node doesnt get too small(minimum nodesize is T 1). We prevent this bycombiningnodes together.

  • 8/3/2019 Deleting From B-Trees

    4/27

    Run Time of a B-Tree

    The height grows in O(Lg N) time.

    t = degree

    h = height

    n = number of keys

    Run time is influenced by CPU speed andDisk I/O speeds

    Space used: O(2n) + sizeof(class/struct)

    Worst Case:2

    1log

    n

    h t

  • 8/3/2019 Deleting From B-Trees

    5/27

    Lets look at an example:Were given this valid B-Tree

    Source: Introduction to Algorithms, Thomas H. Cormen

    Note: T = 2

  • 8/3/2019 Deleting From B-Trees

    6/27

    Simple DeletionCase 1: We delete F

    Source: Introduction to Algorithms, Thomas H. Cormen

    Result: We remove F from the leaf node. No further

    action needed.

  • 8/3/2019 Deleting From B-Trees

    7/27

    When to just delete a key from a

    node: When a key is in a leaf node

    When deleting the key doesnt make the

    leaf node smaller then the minimumallowable size (t-1)

  • 8/3/2019 Deleting From B-Trees

    8/27

    Deleting and shiftingCase 2a: We deleted M

    Source: Introduction to Algorithms, Thomas H. Cormen

    Result: We remove M from the parent node. Since

    there are four nodes and two letters, we move L to

    replace M. Now, the N O node has a parent again.

  • 8/3/2019 Deleting From B-Trees

    9/27

    The rule:

    You musthave n+1 child nodes.

    If you delete an intermediate node, you

    need to replace the missing node bypromoting a child key.

    If you dont go below the minimum ton the

    right, then take the right most node fromthe left branch, otherwise try the right leftmost node on the right branch.

  • 8/3/2019 Deleting From B-Trees

    10/27

    Combining and DeletingCase 2c: Now, we delete G

    Source: Introduction to Algorithms, Thomas H. Cormen

    Result: First, we combine nodes DE and JK. Then,

    we push down G into the DEJK node and delete it as

    a leaf.

  • 8/3/2019 Deleting From B-Trees

    11/27

    The rule

    Condition: You must delete a key. The key

    has a minimum number of nodes on the

    left and right branches. Action: Take the key to be deleted, move it

    down, and then combine the left and right

    branches. Then delete the key.

  • 8/3/2019 Deleting From B-Trees

    12/27

    Check this out!Case 2b: Now, we delete D

  • 8/3/2019 Deleting From B-Trees

    13/27

    The rule

    The recursion cant descend down to node CL

    because it only has two keys, so P is pushed

    down to the end of CL and merged with TX toform CLPTX; Then, D is deleted as in the first

    rule.

  • 8/3/2019 Deleting From B-Trees

    14/27

    Deleting BBefore:

    After: Deleted B, Demoted C, Promoted E

  • 8/3/2019 Deleting From B-Trees

    15/27

    The rule

    Conditions:

    Deleting the key from the node would result in a node

    smaller then the minimum allowable size The neighboring node has extra keys

    Actions:

    Take the parent key and overwrite the node youre

    deleting Take the left most node from the right neighbor and

    promote it to the parent key

    Otherwise: combine nodes

  • 8/3/2019 Deleting From B-Trees

    16/27

    Any questions so far?

    Next: C++ code implementation of B-Tree

    delete

  • 8/3/2019 Deleting From B-Trees

    17/27

    The remove class member

    Source: Data Structures and Program Design in C++, Robert L. Kruse;

  • 8/3/2019 Deleting From B-Trees

    18/27

    Recursive_remove class member

  • 8/3/2019 Deleting From B-Trees

    19/27

    Remove_data

    Copy_in_predecessor

  • 8/3/2019 Deleting From B-Trees

    20/27

    Restoring node structure

  • 8/3/2019 Deleting From B-Trees

    21/27

    Move_left class member

  • 8/3/2019 Deleting From B-Trees

    22/27

    Move_right class member

  • 8/3/2019 Deleting From B-Trees

    23/27

    Combination Illustration

  • 8/3/2019 Deleting From B-Trees

    24/27

    Combine class member

  • 8/3/2019 Deleting From B-Trees

    25/27

    Quick ExerciseRemove C, P and V from this B-Tree.

    Work with the guy next to you to compare and check your work. Whichever pair can correctly remove all three the fastest is awesome!

    You have five minutes

  • 8/3/2019 Deleting From B-Trees

    26/27

    Pending any questions, well move

    on to the white board.

  • 8/3/2019 Deleting From B-Trees

    27/27

    Fair Use Statement

    Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrightedwork, including such use by reproduction in copies or phono records or by any other

    means specified by that section, for purposes such as criticism, comment, news

    reporting, teaching (including multiple copies for classroom use), scholarship, orresearch, is not an infringement of copyright. In determining whether the use made of

    a work in any particular case is a fair use the factors to be considered shall include

    the purpose and character of the use, including whether such use is of a commercial

    nature or is for nonprofit educational purposes;

    1. the nature of the copyrighted work;

    2. the amount and substantiality of the portion used in relation to the copyrightedwork as a whole; and

    3. the effect of the use upon the potential market for or value of the copyrightedwork.

    The fact that a work is unpublished shall not itself bar a finding of fair use if such finding

    is made upon consideration of all the above factors.