B Tree and B+ Tree

15
1 B+ - Tree & B - B+ - Tree & B - Tree Tree By Phi Thong Ho By Phi Thong Ho

description

its a ppt to describe about B trees and B+trees

Transcript of B Tree and B+ Tree

Page 1: B Tree and B+ Tree

11

B+ - Tree & B - TreeB+ - Tree & B - Tree

By Phi Thong HoBy Phi Thong Ho

Page 2: B Tree and B+ Tree

22

AgendaAgenda

B+ - Tree StructureB+ - Tree Structure

B+ - Tree UpdatesB+ - Tree Updates

B+ - Tree File OrganizationB+ - Tree File Organization

B - Tree Index FilesB - Tree Index Files

Page 3: B Tree and B+ Tree

33

B+ - Tree StructureB+ - Tree Structure

A B+ - Tree is in the form of a balanced tree in A B+ - Tree is in the form of a balanced tree in which every path from the root of the tree to a which every path from the root of the tree to a leaf of the tree is the same length.leaf of the tree is the same length.

Each nonleaf node in the tree has between [n/2] Each nonleaf node in the tree has between [n/2] and n children, where n is fixed.and n children, where n is fixed.

B+ - Trees are good for searches, but cause B+ - Trees are good for searches, but cause some overhead issues in wasted space.some overhead issues in wasted space.

Page 4: B Tree and B+ Tree

44

A typical node contains up to n – 1 search key A typical node contains up to n – 1 search key values K1, K2,…, Kn-1, and n pointers P1, P2,values K1, K2,…, Kn-1, and n pointers P1, P2,…, Pn. The search key values are kept in sorted …, Pn. The search key values are kept in sorted order.order.

P1 K1 P2 … Pn-1 Kn-1 Pn

Page 5: B Tree and B+ Tree

55

The pointer Pi can point to either a file record or The pointer Pi can point to either a file record or a bucket of pointers which each point to a file a bucket of pointers which each point to a file record.record.

leaf node, n = 3leaf node, n = 3 Brighton Downtown

A – 212 Brighton 750

A – 101 Brighton 750

A – 212 Brighton 750...

Page 6: B Tree and B+ Tree

66

Each leaf can hold up to n – 1 values and must Each leaf can hold up to n – 1 values and must contain at least [(n – 1) / 2] values.contain at least [(n – 1) / 2] values.

Nonleaf node pointers point to tree nodes (leaf Nonleaf node pointers point to tree nodes (leaf nodes). Nonleaf nodes can hold up to n pointers nodes). Nonleaf nodes can hold up to n pointers and must hold at least [n/2] pointers.and must hold at least [n/2] pointers.

i.e. n = 3i.e. n = 3 Perryridge

Mianus Redwood

Brighton Downtown Mianus Redwood Round Hill

Perryridge

Page 7: B Tree and B+ Tree

77

B+ - Tree UpdatesB+ - Tree Updates

Insertion – If the new node has a search key that Insertion – If the new node has a search key that already exists in another leaf node, then it adds already exists in another leaf node, then it adds the new record to the file and a pointer to the the new record to the file and a pointer to the bucket of pointers. If the search key is different bucket of pointers. If the search key is different from all others, it is inserted in order.from all others, it is inserted in order.

Deletion – It removes the search key value from Deletion – It removes the search key value from the node.the node.

Page 8: B Tree and B+ Tree

88

i.e. we are going to insert a node with a search i.e. we are going to insert a node with a search key value “Clearview”. We find that “Clearview” key value “Clearview”. We find that “Clearview” should be in the node with Brighton and should be in the node with Brighton and Downtown, so we must split the node.Downtown, so we must split the node.

i.e. n = 3i.e. n = 3 Perryridge

Downtown Mianus Redwood

Brighton Clearview Mianus Redwood Round Hill

Perryridge Downtown

Page 9: B Tree and B+ Tree

99

i.e. Deletion of “Downtown” from slide #8.i.e. Deletion of “Downtown” from slide #8.

Perryridge

Mianus Redwood

Brighton Clearview Mianus Redwood Round Hill

Perryridge

Page 10: B Tree and B+ Tree

1010

i.e. Deletion of “Perryridge” from slide #8i.e. Deletion of “Perryridge” from slide #8

Mianus

Downtown Redwood

Brighton Clearview Mianus Redwood Round Hill

Downtown

Page 11: B Tree and B+ Tree

1111

B+ - Tree File OrganizationB+ - Tree File Organization

In a B+ - Tree file organization, the leaf nodes of the tree In a B+ - Tree file organization, the leaf nodes of the tree stores the actual record rather than storing pointers to stores the actual record rather than storing pointers to records.records.During insertion, the system locates the block that During insertion, the system locates the block that should contain the record. If there is enough free space should contain the record. If there is enough free space in the node then the system stores it. Otherwise the in the node then the system stores it. Otherwise the system splits the record in two and distributes the system splits the record in two and distributes the records.records.During deletion, the system first removes the record from During deletion, the system first removes the record from the block containing it. If the block becomes less than the block containing it. If the block becomes less than half full as a result, the records in the block are half full as a result, the records in the block are redistributed.redistributed.

Page 12: B Tree and B+ Tree

1212

B+ - Tree File OrganizationB+ - Tree File Organization

Page 13: B Tree and B+ Tree

1313

B - Tree Index FilesB - Tree Index Files

Similar to B+-tree, but B-tree allows search-key values to appear only once; eliminates redundant storage of search keys.

Search keys in nonleaf nodes appear nowhere else in the B-tree; an additional pointer field for each search key in a nonleaf node must be included.

Nonleaf node Nonleaf node –– pointers pointers BBii are the bucket or file record pointers. are the bucket or file record pointers.

Page 14: B Tree and B+ Tree

1414

B – Tree Index FilesB – Tree Index Files

Advantages of B-Tree indices:Advantages of B-Tree indices: May use less tree nodes than a corresponding BMay use less tree nodes than a corresponding B++-Tree.-Tree. Sometimes possible to find search-key value before reaching Sometimes possible to find search-key value before reaching

leaf node.leaf node.

Disadvantages of B-Tree indices:Disadvantages of B-Tree indices: Only small fraction of all search-key values are found early Only small fraction of all search-key values are found early Non-leaf nodes are larger, so fan-out is reduced. Thus B-Trees Non-leaf nodes are larger, so fan-out is reduced. Thus B-Trees

typically have greater depth than corresponding typically have greater depth than corresponding BB++-Tree-Tree

Insertion and deletion more complicated than in BInsertion and deletion more complicated than in B++-Trees -Trees Implementation is harder than BImplementation is harder than B++-Trees.-Trees.

Page 15: B Tree and B+ Tree

1515

Example of B – TreeExample of B – Tree