Sunteți pe pagina 1din 27

Deleting from BTrees

Eric Nevala

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


1. 2. 3.
4.

The concept of deleting from a B-Tree A graphical illustration of all the cases of deletion Look at some implementations in C++ code 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 doesnt 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 n 1 Worst Case: h log t h = height 2 n = number of keys Run time is influenced by CPU speed and Disk I/O speeds Space used: O(2n) + sizeof(class/struct)

Lets look at an example:


Were given this valid B-Tree
Note: T = 2

Source: Introduction to Algorithms, Thomas H. Cormen

Simple Deletion
Case 1: We delete F
Result: We remove F from the leaf node. No further action needed.

Source: Introduction to Algorithms, Thomas H. Cormen

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 minimum allowable size (t-1)

Deleting and shifting


Case 2a: We deleted M

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.

Source: Introduction to Algorithms, Thomas H. Cormen

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 dont 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 Deleting


Case 2c: Now, we delete G

Result: First, we combine nodes DE and JK. Then, we push down G into the DEJK node and delete it as a leaf.

Source: Introduction to Algorithms, Thomas H. Cormen

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 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 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 youre 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 Exercise
Remove 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, well 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.

S-ar putea să vă placă și