Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

B-tree

In computer science, a B-tree is a tree data structure that keeps data


sorted and allows searches, sequential access, insertions, and deletions
in logarithmic time. The B-tree is a generalization of a binary search
tree in that a node can have more than two children (Comer 1979,
p. 123). Unlike self-balancing binary search trees, the B-tree is
optimized for systems that read and write large blocks of data. It is
commonly used in databases and file systems.

Invented by (1972):
Rudolf Bayer, Edward M.
McCreight

Time complexity in big O notation
Average Worst case
Space O(n) O(n)
Search O(log n) O(log n)
Insert O(log n) O(log n)
Delete O(log n) O(log n)


Problem: Data base too big to fit memory Disk reads are slow
Example: 1,000,000 records on disk Binary search might take 20 disk reads. Disk reads are done in blocks
Suppose one block read can retrieve 100 records,


General Definition:
A B-Tree of order m is an m-way tree such that
1. All leaf nodes are at the same level.
2. All non-leaf nodes (except the root) have at most m and at least m/2 children.
3. The number of keys is one less than the number of children for non-leaf nodes and at most m-1 and
at least m/2 for leaf nodes.
4. The root may have as few as 2 children unless the tree is the root alone.


Example for m=5
Definition:
A B-Tree of order 5 is an 5-way tree such that
1. All leaf nodes are at the same level.
2. All non-leaf nodes (except the root) have at most 5 and at least 2 children.
3. The number of keys is one less than the number of children for non-leaf nodes and at most 4 and at
least 2 for leaf nodes.
4. The root may have as few as 2 children unless the tree is the root alone.


A B-tree is a method of placing and locating files (called records or keys) in a database. (The meaning of
the letter B has not been explicitly defined.) The B-tree algorithmminimizes the number of times a
medium must be accessed to locate a desired record, thereby speeding up the process.
B-trees are preferred when decision points, called nodes, are on hard disk rather than in random-access
memory (RAM). It takes thousands of times longer to access a data element from hard disk as compared
with accessing it from RAM, because a disk drive has mechanical parts, which read and write data far
more slowly than purely electronic media. B-trees save time by using nodes with many branches (called
children), compared with binary trees, in which each node has only two children. When there are many
children per node, a record can be found by passing through fewer nodes than if there are two children per
node. A simplified example of this principle is shown below.

In a tree, records are stored in locations called leaves. This name derives from the fact that records always
exist at end points; there is nothing beyond them. The maximum number of children per node is the order
of the tree. The number of required disk accesses is the depth. The image at left shows a binary tree for
locating a particular record in a set of eight leaves. The image at right shows a B-tree of order three for
locating a particular record in a set of eight leaves (the ninth leaf is unoccupied, and is called a null). The
binary tree at left has a depth of four; the B-tree at right has a depth of three. Clearly, the B-tree allows a
desired record to be located faster, assuming all other system parameters are identical. The tradeoff is that
the decision process at each node is more complicated in a B-tree as compared with a binary tree. A
sophisticated program is required to execute the operations in a B-tree. But this program is stored in
RAM, so it runs fast.

struct BTreeNode
{
int *keys; // An array of keys
int t; // Minimum degree
BTreeNode *C; // An array of child pointers
int n; // Current number of keys
int leaf; // Is 1 when node is leaf. Otherwise 0
};

Example: Creating a B-tree of order 5
A G F B K D H M J E S I R X C L N T U P
1. [Result of inserting A G F B K ]

2.

3. [insert D H M]

4. [insert J]

5.

6. [ insert E S I R]

7. [insert X]

8. [insert C]

9.

10. [insert L N T U]

11. [insert P]

12.

13.












Deleting Nodes
Delete E from leaf node
[Delete E]
[borrowing from the neighbour]
[delete F, Cant borrow]

[so combine]

[Delete M]
[Borrowing from the neighbour]














Create a B-tree of order 5, when the keys arrive in the following order:
A F G B K D M J E S I X R Y => 1 6 7 2 11 4 13 10 5 19 9 24 18 25



Thus the final tree is:





[animation in java applet: http://slady.net/java/bt/view.php?w=600&h=450]




















Create a B-tree of order 3, when the keys arrive in the following order:
10 24 23 11 31 16 26 35 29 20 46 28

You might also like