Deleting from B- Trees Eric Nevala. Objective: Teach you the concept of deleting keys from a B-Tree...

27
Deleting from B-Trees Eric Nevala

Transcript of Deleting from B- Trees Eric Nevala. Objective: Teach you the concept of deleting keys from a B-Tree...

Deleting from B-Trees

Eric Nevala

Objective: Teach you the concept of deleting keys from a B-Tree

1. The concept of deleting from a B-Tree2. A graphical illustration of all the cases of

deletion3. Look at some implementations in C++

code4. Do some practical exercises for practice

(cards -> whiteboard)

The Concept

You can delete a key entry from any node. ->Therefore, you must ensure that

before/after deletion, the B-Tree maintains its properties.

When deleting, you have to ensure that a node doesn’t get too small (minimum node size is T – 1). We prevent this by combining nodes together.

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 and

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

Worst Case:

2

1log

nh t

Lets look at an example:We’re given this valid B-Tree

Source: Introduction to Algorithms, Thomas H. Cormen

Note: T = 2

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.

When to just delete a key from a node: When a key is in a leaf node When deleting the key doesn’t make the

leaf node smaller then the minimum allowable size (t-1)

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.

The rule:

You must have n+1 child nodes. If you delete an intermediate node, you

need to replace the missing node by promoting a child key.

If you don’t go below the minimum t on the right, then take the right most node from the left branch, otherwise try the right left most node on the right branch.

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.

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.

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

The rule

The recursion can’t 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” to form “CLPTX”; Then, “D” is deleted as in the first rule.

Deleting “B”Before:

After: Deleted “B”, Demoted “C”, Promoted “E”

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 you’re deleting Take the left most node from the right neighbor and

promote it to the parent key

Otherwise: combine nodes

Any questions so far?

Next: C++ code implementation of B-Tree delete

The remove class member

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

Recursive_remove class member

Remove_data

Copy_in_predecessor

Restoring node structure

Move_left class member

Move_right class member

Combination Illustration

Combine class member

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

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

You have five minutes

Pending any questions, we’ll move on to the white board.

Fair Use Statement

Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, 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, or research, 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 copyrighted work as a whole; and

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

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.