b+ trees

28
1 B+ Trees B+ Trees Brian Lee Brian Lee CS157B Section 1 CS157B Section 1 Spring 2006 Spring 2006

Upload: kamal-wiggins

Post on 31-Dec-2015

47 views

Category:

Documents


1 download

DESCRIPTION

B+ Trees. Brian Lee CS157B Section 1 Spring 2006. Table of Contents. History of B+ Trees Definition of B+ Trees Searching in B+ Trees Inserting into B+ Trees Deleting from B+ Trees Works Cited. History of B+ Trees. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: B+ Trees

1

B+ TreesB+ Trees

Brian LeeBrian Lee

CS157B Section 1CS157B Section 1

Spring 2006Spring 2006

Page 2: B+ Trees

2

Table of ContentsTable of Contents

History of B+ TreesHistory of B+ Trees Definition of B+ TreesDefinition of B+ Trees Searching in B+ TreesSearching in B+ Trees Inserting into B+ TreesInserting into B+ Trees Deleting from B+ TreesDeleting from B+ Trees Works CitedWorks Cited

Page 3: B+ Trees

3

History of B+ TreesHistory of B+ Trees

First described in paper by Rudolf First described in paper by Rudolf Bayer and Edward M. McCreight in Bayer and Edward M. McCreight in 19721972

"Rudolf Bayer, Edward M. McCreight: "Rudolf Bayer, Edward M. McCreight: Organization and Maintenance of Organization and Maintenance of Large Ordered Indices. Acta Large Ordered Indices. Acta Informatica 1: 173-189 (1972)“ --Informatica 1: 173-189 (1972)“ --WikipediaWikipedia

Page 4: B+ Trees

4

Description of B+ TreesDescription of B+ Trees

A variation of B-TreesA variation of B-Trees

B-Trees commonly found in B-Trees commonly found in databases and filesystemsdatabases and filesystems

Sacrifices space for efficiency (not as Sacrifices space for efficiency (not as much re-balancing required much re-balancing required compared to other balanced trees)compared to other balanced trees)

Page 5: B+ Trees

5

Description of B+ Trees Description of B+ Trees (cont.)(cont.)

Main difference of B-Trees and B+ TreesMain difference of B-Trees and B+ Trees

B-Trees: data stored at every level (in every B-Trees: data stored at every level (in every node)node)

B+ Trees: data stored only in leavesB+ Trees: data stored only in leaves

Internal nodes only contain keys and pointersInternal nodes only contain keys and pointers

All leaves are at the same level (the lowest one)All leaves are at the same level (the lowest one)

Page 6: B+ Trees

6

Description of B+ Trees Description of B+ Trees (cont.)(cont.)

B+ trees have an order nB+ trees have an order n

An internal node can have up to n-1 An internal node can have up to n-1 keys and n pointerskeys and n pointers

Built from the bottom upBuilt from the bottom up

Page 7: B+ Trees

7

Description of B+ Trees Description of B+ Trees (cont.)(cont.)

All nodes must have between All nodes must have between ceil(n/2) and n keys (except for the ceil(n/2) and n keys (except for the root)root)

For a B+ tree of order n and height For a B+ tree of order n and height h, it can hold up to nh, it can hold up to nhh keys keys

Page 8: B+ Trees

8

Searching in B+ TreesSearching in B+ Trees

Searching just like in a binary Searching just like in a binary search treesearch tree

Starts at the root, works down to the Starts at the root, works down to the leaf levelleaf level

Does a comparison of the search Does a comparison of the search value and the current “separation value and the current “separation value”, goes left or rightvalue”, goes left or right

Page 9: B+ Trees

9

Inserting into a B+ TreeInserting into a B+ Tree

A search is first performed, using A search is first performed, using the value to be addedthe value to be added

After the search is completed, the After the search is completed, the location for the new value is knownlocation for the new value is known

Page 10: B+ Trees

10

Inserting into a B+ Tree Inserting into a B+ Tree (cont.)(cont.)

If the tree is empty, add to the rootIf the tree is empty, add to the root

Once the root is full, split the data Once the root is full, split the data into 2 leaves, using the root to hold into 2 leaves, using the root to hold keys and pointerskeys and pointers

If adding an element will overload a If adding an element will overload a leaf, take the median and split itleaf, take the median and split it

Page 11: B+ Trees

11

Inserting into a B+ Tree Inserting into a B+ Tree (cont.)(cont.)

Example:Example:

Suppose we had a B+ tree with n = 3Suppose we had a B+ tree with n = 3

2 keys max. at each internal node2 keys max. at each internal node

3 pointers max. at each internal node3 pointers max. at each internal node

Internal nodes include the rootInternal nodes include the root

Page 12: B+ Trees

12

Inserting Into B+ Trees Inserting Into B+ Trees (cont.)(cont.)

Case 1: Empty rootCase 1: Empty root Insert 6Insert 6

Page 13: B+ Trees

13

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

Case 2: Full rootCase 2: Full root Suppose we have this root:Suppose we have this root:

In order to insert another number, like In order to insert another number, like 5, we must split the root and create a 5, we must split the root and create a new level.new level.

Page 14: B+ Trees

14

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

After splitting the root, we would After splitting the root, we would end up with this:end up with this:

Page 15: B+ Trees

15

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

Case 3: Adding to a full nodeCase 3: Adding to a full node Suppose we wanted to insert 7 into Suppose we wanted to insert 7 into

our tree:our tree:

Page 16: B+ Trees

16

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

7 goes with 5 and 6. However, since 7 goes with 5 and 6. However, since each node can only hold a maximum of each node can only hold a maximum of 2 keys, we can take the median of all 3 2 keys, we can take the median of all 3 (which would be 6), keep it with the (which would be 6), keep it with the left (5), and create a new leaf for 7.left (5), and create a new leaf for 7.

An alternative way is to keep the An alternative way is to keep the median with the right and create a median with the right and create a new leaf for 5.new leaf for 5.

Page 17: B+ Trees

17

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

Page 18: B+ Trees

18

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

Case 4: Inserting on a full leaf, Case 4: Inserting on a full leaf, requiring a split at least 1 level uprequiring a split at least 1 level up

Using the last tree, suppose we were Using the last tree, suppose we were to insert 4.to insert 4.

Page 19: B+ Trees

19

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

We would need to split the leftmost We would need to split the leftmost leaf, which would require another leaf, which would require another split of the root.split of the root.

A new root is created with the A new root is created with the pointers referencing the old split pointers referencing the old split root.root.

Page 20: B+ Trees

20

Inserting into B+ Trees Inserting into B+ Trees (cont.)(cont.)

Page 21: B+ Trees

21

Deleting from B+ TreesDeleting from B+ Trees

Deletion, like insertion, begins with a Deletion, like insertion, begins with a search.search.

When the item to be deleted is located When the item to be deleted is located and removed, the tree must be checked and removed, the tree must be checked to make sure no rules are violated.to make sure no rules are violated.

The rule to focus on is to ensure that The rule to focus on is to ensure that each node has at least ceil(n/2) pointers.each node has at least ceil(n/2) pointers.

Page 22: B+ Trees

22

Deleting from B+ Trees Deleting from B+ Trees (cont.)(cont.)

Take the previous example:Take the previous example:

Page 23: B+ Trees

23

Deleting from B+ Trees Deleting from B+ Trees (cont.)(cont.)

Suppose we want to delete 5.Suppose we want to delete 5.

This would not require any This would not require any rebalancing since the leaf that 5 was rebalancing since the leaf that 5 was in still has 1 element in it.in still has 1 element in it.

Page 24: B+ Trees

24

Deleting from B+ Trees Deleting from B+ Trees (cont.)(cont.)

Page 25: B+ Trees

25

Deleting from B+ Trees Deleting from B+ Trees (cont.)(cont.)

Suppose we want to remove 6.Suppose we want to remove 6.

This would require rebalancing, This would require rebalancing, since removing the element 6 would since removing the element 6 would require removal of the entire leaf require removal of the entire leaf (since 6 is the only element in that (since 6 is the only element in that leaf).leaf).

Page 26: B+ Trees

26

Deleting from B+ Trees Deleting from B+ Trees (cont.)(cont.)

Once we remove the leaf node, the Once we remove the leaf node, the parent of that leaf node no longer parent of that leaf node no longer follows the rule.follows the rule.

It has only 1 child, which is less than It has only 1 child, which is less than the 2 required ( ceil(3/2) = 2).the 2 required ( ceil(3/2) = 2).

Then, the tree must be compacted in Then, the tree must be compacted in order to enforce this rule.order to enforce this rule.

Page 27: B+ Trees

27

Deleting from B+ Trees Deleting from B+ Trees (cont.)(cont.)

The end product would look The end product would look something like this:something like this:

Page 28: B+ Trees

28

BibliographyBibliography

WikipediaWikipedia

http://http://en.wikipedia.org/wiki/B_plus_treeen.wikipedia.org/wiki/B_plus_tree

Silberschatz, Abraham, and Henry F. Silberschatz, Abraham, and Henry F. Korth, and S. Sundarshan. Korth, and S. Sundarshan. Database Database System ConceptsSystem Concepts. New York: . New York: McGraw-Hill, 2006.McGraw-Hill, 2006.