B-tree: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Twimoki
→‎External links: remove subjective statement
en>Ysangkok
→‎See also: link 2-3 and 2-3-4 trees cause they are so related and almost not mentioned
Line 1: Line 1:
{{distinguish|Binary tree}}
I am 24 years old and my name is Alfred Gaunson. I life in Bergeijk (Netherlands).<br><br>Also visit my web-site ... [http://www.triuneweb.com/fifa15-coingenerator.com-143933.html FIFA Coin generator]
 
{{Infobox data structure
|name=B-tree
|type=tree
|invented_by=[[Rudolf Bayer]], [[Edward M. McCreight]]
|invented_year=1972
|
|space_avg= O(''n'')
|space_worst= O(''n'')
|search_avg= O(log ''n'')
|search_worst= O(log ''n'')
|insert_avg= O(log ''n'')
|insert_worst= O(log ''n'')
|delete_avg= O(log ''n'')
|delete_worst= O(log ''n'')
}}
 
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 {{Harv|Comer|1979|p=123}}. Unlike [[self-balancing binary search tree]]s, the B-tree is optimized for systems that read and write large blocks of data. It is commonly used in [[database]]s and [[filesystem]]s.
 
==Overview==
[[File:B-tree.svg|thumb|400px|right|A B-tree of order 2 {{Harv|Bayer|McCreight|1972}} or order 5 {{Harv|Knuth|1998}}.]]
In B-trees, internal ([[Leaf node|non-leaf]]) nodes can have a variable number of child nodes within some pre-defined range. When data is inserted or removed from a node, its number of child nodes changes. In order to maintain the pre-defined range, internal nodes may be joined or split. Because a range of child nodes is permitted, B-trees do not need re-balancing as frequently as other self-balancing search trees, but may waste some space, since nodes are not entirely full. The lower and upper bounds on the number of child nodes are typically fixed for a particular implementation. For example, in a [[2-3 tree|2-3 B-tree]] (often simply referred to as a '''2-3 tree'''), each internal node may have only 2 or 3 child nodes.
 
Each internal node of a B-tree will contain a number of keys. The keys act as separation values which divide its [[subtree]]s. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: ''a''<sub>1</sub> and ''a''<sub>2</sub>. All values in the leftmost subtree will be less than ''a''<sub>1</sub>, all values in the middle subtree will be between ''a''<sub>1</sub> and ''a''<sub>2</sub>, and all values in the rightmost subtree will be greater than ''a''<sub>2</sub>.
 
Usually, the number of keys is chosen to vary between <math>d</math> and <math>2d</math>, where <math>d</math> is the minimum number of keys, and <math>d+1</math> is the minimum [[Outdegree#Indegree_and_outdegree|degree]] or [[branching factor]] of the tree. In practice, the keys take up the most space in a node. The factor of 2 will guarantee that nodes can be split or combined. If an internal node has <math>2d</math> keys, then adding a key to that node can be accomplished by splitting the <math>2d</math> key node into two <math>d</math> key nodes and adding the key to the parent node. Each split node has the required minimum number of keys. Similarly, if an internal node and its neighbor each have <math>d</math> keys, then a key may be deleted from the internal node by combining with its neighbor. Deleting the key would make the internal node have <math>d-1</math> keys; joining the neighbor would add <math>d</math> keys plus one more key brought down from the neighbor's parent. The result is an entirely full node of <math>2d</math> keys.
 
The number of branches (or child nodes) from a node will be one more than the number of keys stored in the node. In a 2-3 B-tree, the internal nodes will store either one key (with two child nodes) or two keys (with three child nodes). A B-tree is sometimes described with the parameters <math>(d+1)</math> — <math>(2d+1)</math> or simply with the highest branching order, <math>(2d+1)</math>.
 
A B-tree is kept balanced by requiring that all leaf nodes be at the same depth. This depth will increase slowly as elements are added to the tree, but an increase in the overall depth is infrequent, and results in all leaf nodes being one more node farther away from the root.
 
B-trees have substantial advantages over alternative implementations when otherwise the time to access the data of a node greatly exceeds the time spent processing that data, because then the cost of accessing the node may be amortized over multiple operations within the node. This usually occurs when the node data are in [[secondary storage]] such as [[hard drive|disk drives]]. By maximizing the number of keys within each [[internal node]], the height of the tree decreases and the number of expensive node accesses is reduced. In addition, rebalancing of the tree occurs less often. The maximum number of child nodes depends on the information that must be stored for each child node and the size of a full [[Block (data storage)|disk block]] or an analogous size in secondary storage. While 2-3 B-trees are easier to explain, practical B-trees using secondary storage need a large number of child nodes to improve performance.
 
===Variants===
The term '''B-tree''' may refer to a specific design or it may refer to a general class of designs. In the narrow sense, a B-tree stores keys in its internal nodes but need not store those keys in the records at the leaves. The general class includes variations such as the [[B+ tree|B<sup>+</sup>-tree]] and the B<sup>*</sup>-tree.
* In the B<sup>+</sup>-tree, copies of the keys are stored in the internal nodes; the keys and records are stored in leaves; in addition, a leaf node may include a pointer to the next leaf node to speed sequential access {{Harv|Comer|1979|p=129}}.
* The B<sup>*</sup>-tree balances more neighboring internal nodes to keep the internal nodes more densely packed {{Harv|Comer|1979|p=129}}. This variant requires non-root nodes to be at least 2/3 full instead of 1/2 {{Harv|Knuth|1998|p=488}}. To maintain this, instead of immediately splitting up a node when it gets full, its keys are shared with a node next to it. When both nodes are full, then the two nodes are split into three.
* B-trees can be turned into [[order statistic tree]]s to allow rapid searches for the Nth record in key order, or counting the number of records between any two records, and various other related operations.<ref>[http://www.chiark.greenend.org.uk/~sgtatham/algorithms/cbtree.html Counted B-Trees], retrieved 2010-01-25</ref>
 
===Etymology unknown===
[[Rudolf Bayer]] and [[Edward M. McCreight|Ed McCreight]] invented the B-tree while working at [[Boeing|Boeing Research Labs]] in 1971 {{Harv|Bayer|McCreight|1972}}, but they did not explain what, if anything, the ''B'' stands for. [[Douglas Comer]] explains:
<blockquote>The origin of "B-tree" has never been explained by the authors. As we shall see, "balanced," "broad," or "bushy" might apply. Others suggest that the "B" stands for Boeing. Because of his contributions, however, it seems appropriate to think of B-trees as "Bayer"-trees. {{Harv|Comer|1979|p=123 footnote 1}}</blockquote>
 
[[Donald Knuth]] speculates on the etymology of B-trees in his May, 1980 lecture on the topic "CS144C classroom lecture about disk storage and B-trees", suggesting the "B" may have originated from Boeing or from Bayer's name.<ref>[http://scpd.stanford.edu/knuth/index.jsp Knuth's video lectures from Stanford]</ref>
 
After a talk at CPM 2013 (24th Annual Symposium on Combinatorial Pattern Matching, Bad Herrenalb, Germany, June 17–19, 2013), Ed McCreight answered a question on B-tree's name by Martin Farach-Colton saying: "Bayer and I were in a lunch time where we get to think a name.  And we were, so, B, we were thinking... B is, you know... We were working for Boeing at the time, we couldn't use the name without talking to lawyers.  So, there is a B.  It has to do with balance, another B.  Bayer was the senior author, who did have several years older than I am and had many more publications than I did. So there is another B.  And so, at the lunch table we never did resolve whether there was one of those that made more sense than the rest.  What really lives to say is: the more you think about what the B in B-trees means, the better you understand B-trees."<ref>[http://vimeo.com/channels/574784 Talk's video], retrieved 2014-01-17</ref>
 
==The database problem==
This section describes a problem faced by database designers, outlines a series of increasingly effective solutions to the problem, and ends by describing how the B-tree solves the problem completely.
 
===Time to search a sorted file===
Usually, sorting and searching algorithms have been characterized by the number of comparison operations that must be performed using [[Big O notation|order notation]]. A [[binary search]] of a sorted table with <math>N</math> records, for example, can be done in roughly <math>\lceil \log_2 N \rceil</math> comparisons. If the table had 1,000,000 records, then a specific record could be located with at most 20 comparisons: <math>\lceil \log_2 1,000,000 \rceil = 20 </math>.
 
Large databases have historically been kept on disk drives. The time to read a record on a disk drive far exceeds the time needed to compare keys once the record is available. The time to read a record from a disk drive involves a [[seek time]] and a rotational delay. The seek time may be 0 to 20 or more milliseconds, and the rotational delay averages about half the rotation period. For a 7200 RPM drive, the rotation period is 8.33 milliseconds. For a drive such as the Seagate ST3500320NS, the track-to-track seek time is 0.8 milliseconds and the average reading seek time is 8.5 milliseconds.<ref>Seagate Technology LLC, Product Manual: Barracuda ES.2 Serial ATA, Rev. F., publication 100468393, 2008 [http://www.seagate.com/staticfiles/support/disc/manuals/NL35%20Series%20&%20BC%20ES%20Series/Barracuda%20ES.2%20Series/100468393f.pdf], page 6</ref> For simplicity, assume reading from disk takes about 10 milliseconds.
 
Naively, then, the time to locate one record out of a million would take 20 disk reads times 10 milliseconds per disk read, which is 0.2 seconds.
 
The time won't be that bad because individual records are grouped together in a disk '''block'''. A disk block might be 16 kilobytes. If each record is 160 bytes, then 100 records could be stored in each block. The disk read time above was actually for an entire block. Once the disk head is in position, one or more disk blocks can be read with little delay. With 100 records per block, the last 6 or so comparisons don't need to do any disk reads—the comparisons are all within the last disk block read.
 
To speed the search further, the first 13 to 14 comparisons (which each required a disk access) must be sped up.
 
===An index speeds the search===
A significant improvement can be made with an [[Index (database)|index]]. In the example above, initial disk reads narrowed the search range by a factor of two. That can be improved substantially by creating an auxiliary index that contains the first record in each disk block (sometimes called a [[Index (database)#Sparse index|sparse index]]). This auxiliary index would be 1% of the size of the original database, but it can be searched more quickly. Finding an entry in the auxiliary index would tell us which block to search in the main database; after searching the auxiliary index, we would have to search only that one block of the main database—at a cost of one more disk read. The index would hold 10,000 entries, so it would take at most 14 comparisons. Like the main database, the last 6 or so comparisons in the aux index would be on the same disk block. The index could be searched in about 8 disk reads, and the desired record could be accessed in 9 disk reads.
 
The trick of creating an auxiliary index can be repeated to make an auxiliary index to the auxiliary index. That would make an aux-aux index that would need only 100 entries and would fit in one disk block.
 
Instead of reading 14 disk blocks to find the desired record, we only need to read 3 blocks. Reading and searching the first (and only) block of the aux-aux index identifies the relevant block in aux-index. Reading and searching that aux-index block identifies the relevant block in the main database. Instead of 150 milliseconds, we need only 30 milliseconds to get the record.
 
The auxiliary indices have turned the search problem from a binary search requiring roughly <math>\log_2 N</math> disk reads to one requiring only <math>\log_b N</math> disk reads where <math>b</math> is the blocking factor (the number of entries per block: <math>b = 100</math> entries per block; <math>\log_b 1,000,000 = 3</math> reads).
 
In practice, if the main database is being frequently searched, the aux-aux index and much of the aux index may reside in a [[page cache|disk cache]], so they would not incur a disk read.
 
===Insertions and deletions cause trouble===
If the database does not change, then compiling the index is simple to do, and the index need never be changed. If there are changes, then managing the database and its index becomes more complicated.
 
Deleting records from a database doesn't cause much trouble. The index can stay the same, and the record can just be marked as deleted. The database stays in sorted order. If there is a large number of deletions, then the searching and storage become less efficient.
 
Insertions can be very slow in a sorted sequential file because room for the inserted record must be made. Inserting a record before the first record in the file requires shifting all of the records down one. Such an operation is just too expensive to be practical. A trick is to leave some space lying around to be used for insertions. Instead of densely storing all the records in a block, the block can have some free space to allow for subsequent insertions. Those records would be marked as if they were "deleted" records.
 
Both insertions and deletions are fast as long as space is available on a block. If an insertion won't fit on the block, then some free space on some nearby block must be found and the auxiliary indices adjusted. The hope is that enough space is nearby such that a lot of blocks do not need to be reorganized. Alternatively, some out-of-sequence disk blocks may be used.
 
===The B-tree uses all those ideas===
The B-tree uses all of the ideas described above.  In particular, a B-tree:
* keeps keys in sorted order for sequential traversing
* uses a hierarchical index to minimize the number of disk reads
* uses partially full blocks to speed insertions and deletions
* keeps the index balanced with an elegant recursive algorithm
 
In addition, a B-tree minimizes waste by making sure the interior nodes are at least half full. A B-tree can handle an arbitrary number of insertions and deletions.
 
==Technical description==
 
===Terminology===
Unfortunately, the literature on B-trees is not uniform in its terminology {{Harv|Folk|Zoellick|1992|p=362}}.
 
{{Harvtxt|Bayer|McCreight|1972}}, {{Harvtxt|Comer|1979}}, and others define the '''order''' of B-tree as the minimum number of keys in a non-root node. {{Harvtxt|Folk|Zoellick|1992}} points out that terminology is ambiguous because the maximum number of keys is not clear. An order 3 B-tree might hold a maximum of 6 keys or a maximum of 7 keys. {{Harvtxt|Knuth|1998|p=483}} avoids the problem by defining the '''order''' to be maximum number of children (which is one more than the maximum number of keys).
 
The term '''leaf''' is also inconsistent. {{Harvtxt|Bayer|McCreight|1972}} considered the leaf level to be the lowest level of keys, but Knuth considered the leaf level to be one level below the lowest keys {{Harv|Folk|Zoellick|1992|p=363}}. There are many possible implementation choices. In some designs, the leaves may hold the entire data record; in other designs, the leaves may only hold pointers to the data record. Those choices are not fundamental to the idea of a B-tree.<ref>{{Harvtxt|Bayer|McCreight|1972}} avoided the issue by saying an index element is a (physically adjacent) pair of (''x'',&nbsp;''a'') where ''x'' is the key, and ''a'' is some associated information. The associated information might be a pointer to a record or records in a random access, but what it was didn't really matter. {{Harvtxt|Bayer|McCreight|1972}} states, "For this paper the associated information is of no further interest."</ref>
 
There are also unfortunate choices like using the variable ''k'' to represent the number of children when ''k'' could be confused with the number of keys.
 
For simplicity, most authors assume there are a fixed number of keys that fit in a node. The basic assumption is the key size is fixed and the node size is fixed. In practice, variable length keys may be employed {{Harv|Folk|Zoellick|1992|p=379}}.
 
===Definition===
According to Knuth's definition, a B-tree of order ''m'' is a tree which satisfies the following properties:
 
# Every node has at most ''m'' children.
# Every non-leaf node (except root) has at least &lceil;{{frac|''m''|2}}&rceil; children.
# The root has at least two children if it is not a leaf node.
# A non-leaf node with ''k'' children contains ''k''−1 keys.
# All leaves appear in the same level, and internal vertices carry no information.
 
Each internal node’s keys act as separation values which divide its subtrees. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: ''a''<sub>1</sub> and ''a''<sub>2</sub>. All values in the leftmost subtree will be less than ''a''<sub>1</sub>, all values in the middle subtree will be between ''a''<sub>1</sub> and ''a''<sub>2</sub>, and all values in the rightmost subtree will be greater than ''a''<sub>2</sub>.
 
;'''Internal nodes'''
: Internal nodes are all nodes except for leaf nodes and the root node. They are usually represented as an ordered set of elements and child pointers. Every internal node contains a '''maximum''' of ''U'' children and a '''minimum''' of ''L'' children. Thus, the number of elements is always 1 less than the number of child pointers (the number of elements is between ''L''−1 and ''U''−1). ''U'' must be either 2''L'' or 2''L''−1; therefore each internal node is at least half full. The relationship between ''U'' and ''L'' implies that two half-full nodes can be joined to make a legal node, and one full node can be split into two legal nodes (if there’s room to push one element up into the parent). These properties make it possible to delete and insert new values into a B-tree and adjust the tree to preserve the B-tree properties.
 
;'''The root node'''
: The root node’s number of children has the same upper limit as internal nodes, but has no lower limit. For example, when there are fewer than ''L''−1 elements in the entire tree, the root will be the only node in the tree, with no children at all.
 
;'''Leaf nodes'''
: Leaf nodes have the same restriction on the number of elements, but have no children, and no child pointers.
 
A B-tree of depth ''n''+1 can hold about ''U'' times as many items as a B-tree of depth ''n'', but the cost of search, insert, and delete operations grows with the depth of the tree. As with any balanced tree, the cost grows much more slowly than the number of elements.
 
Some balanced trees store values only at leaf nodes, and use different kinds of nodes for leaf nodes and internal nodes. B-trees keep values in every node in the tree, and may use the same structure for all nodes. However, since leaf nodes never have children, the B-trees benefit from improved performance if they use a specialized structure.
 
==Best case and worst case heights==
Let ''h'' be the height of the classic B-tree. Let ''n'' > 0 be the number of entries in the tree.<ref>If ''n'' is zero, then no root node is needed, so the height of an empty tree is not well defined.</ref> Let ''m'' be the maximum number of children a node can have. Each node can have at most ''m''−1 keys.
 
It can be shown (by induction for example) that a B-tree of height ''h'' with all its nodes completely filled has ''n''=''m''<sup>''h''</sup>&minus;1 entries. Hence, the best case height of a B-tree is:
: <math>\lceil \log_{m} (n+1) \rceil .</math>
 
Let ''d'' be the minimum number of children an internal (non-root) node can have. For an ordinary B-tree, ''d''=⌈''m''/2⌉.
 
{{Harvtxt|Comer|1979|p=127}} and {{Harvtxt|Cormen|Leiserson|Rivest|Stein|2001|pp=383–384}} give the worst case height of a B-tree (where the root node is considered to have height 0) as
: <math>h \le \left\lfloor \log_{d}\left(\frac{n+1}{2}\right) \right\rfloor .</math>
 
==Algorithms==
{{confusing|reason=the discussion below uses "element", "value", "key", "separator", and "separation value" to mean essentially the same thing. The terms are not clearly defined. There are some subtle issues at the root and leaves
|date=February 2012}}
 
===Search===
Searching is similar to searching a binary search tree. Starting at the root, the tree is recursively traversed from top to bottom. At each level, the search chooses the child pointer (subtree) whose separation values are on either side of the search value.
 
Binary search is typically (but not necessarily) used within nodes to find the separation values and child tree of interest.
 
===Insertion===
[[Image:B tree insertion example.png|thumb|A B Tree insertion example with each iteration. The nodes of this B tree have at most 3 children (Knuth order 3).]]
 
All insertions start at a leaf node. To insert a new element, search the tree to find the leaf node where the new element should be added. Insert the new element into that node with the following steps:
 
# If the node contains fewer than the maximum legal number of elements, then there is room for the new element. Insert the new element in the node, keeping the node's elements ordered.
# Otherwise the node is full, evenly split it into two nodes so:
## A single median is chosen from among the leaf's elements and the new element.
## Values less than the median are put in the new left node and values greater than the median are put in the new right node, with the median acting as a separation value.
## The separation value is inserted in the node's parent, which may cause it to be split, and so on. If the node has no parent (i.e., the node was the root), create a new root above this node (increasing the height of the tree).
 
If the splitting goes all the way up to the root, it creates a new root with a single separator value and two children, which is why the lower bound on the size of internal nodes does not apply to the root. The maximum number of elements per node is ''U''−1. When a node is split, one element moves to the parent, but one element is added. So, it must be possible to divide the maximum number ''U''−1 of elements into two legal nodes. If this number is odd, then ''U''=2''L'' and one of the new nodes contains (''U''−2)/2 = ''L''−1 elements, and hence is a legal node, and the other contains one more element, and hence it is legal too. If ''U''−1 is even, then ''U''=2''L''−1, so there are 2''L''−2 elements in the node. Half of this number is ''L''−1, which is the minimum number of elements allowed per node.
 
An improved algorithm {{Harv|Mond|Raz|1985}} supports a single pass down the tree from the root to the node where the insertion will take place, splitting any full nodes encountered on the way. This prevents the need to recall the parent nodes into memory, which may be expensive if the nodes are on secondary storage. However, to use this improved algorithm, we must be able to send one element to the parent and split the remaining ''U''−2 elements into two legal nodes, without adding a new element. This requires ''U'' = 2''L'' rather than ''U'' = 2''L''−1, which accounts for why some textbooks impose this requirement in defining B-trees.
 
===Deletion===
There are two popular strategies for deletion from a B-tree.
 
# Locate and delete the item, then restructure the tree to regain its invariants, '''OR'''
# Do a single pass down the tree, but before entering (visiting) a node, restructure the tree so that once the key to be deleted is encountered, it can be deleted without triggering the need for any further restructuring
 
The algorithm below uses the former strategy.
 
There are two special cases to consider when deleting an element:
 
# The element in an internal node is a separator for its child nodes
# Deleting an element may put its node under the minimum number of elements and children
 
The procedures for these cases are in order below.
 
====Deletion from a leaf node====
# Search for the value to delete.
# If the value is in a leaf node, simply delete it from the node.
# If underflow happens, rebalance the tree as described in section "Rebalancing after deletion" below.
 
====Deletion from an internal node====
Each element in an internal node acts as a separation value for two subtrees, therefore we need to find a replacement for separation. Note that the largest element in the left subtree is still less than the separator. Likewise, the smallest element in the right subtree is still greater than the separator. Both of those elements are in leaf nodes, and either one can be the new separator for the two subtrees. Algorithmically described below:
 
# Choose a new separator (either the largest element in the left subtree or the smallest element in the right subtree), remove it from the leaf node it is in, and replace the element to be deleted with the new separator.
# The previous step deleted an element (the new separator) from a leaf node. If that leaf node is now deficient (has fewer than the required number of nodes), then rebalance the tree starting from the leaf node.
 
====Rebalancing after deletion====
Rebalancing starts from a leaf and proceeds toward the root until the tree is balanced. If deleting an element from a node has brought it under the minimum size, then some elements must be redistributed to bring all nodes up to the minimum. Usually, the redistribution involves moving an element from a sibling node that has more than the minimum number of nodes. That redistribution operation is called a '''rotation'''. If no sibling can spare a node, then the deficient node must be '''merged''' with a sibling. The merge causes the parent to lose a separator element, so the parent may become deficient and need rebalancing. The merging and rebalancing may continue all the way to the root. Since the minimum element count doesn't apply to the root, making the root be the only deficient node is not a problem. The algorithm to rebalance the tree is as follows:{{Citation needed|date=July 2008}}
 
* If the deficient node's right sibling exists and has more than the minimum number of elements, then rotate left
*# Copy the separator from the parent to the end of the deficient node (the separator moves down; the deficient node now has the minimum number of elements)
*# Replace the separator in the parent with the first element of the right sibling (right sibling loses one node but still has at least the minimum number of elements)
*# The tree is now balanced
* Otherwise, if the deficient node's left sibling exists and has more than the minimum number of elements, then rotate right
*# Copy the separator from the parent to the start of the deficient node (the separator moves down; deficient node now has the minimum number of elements)
*# Replace the separator in the parent with the last element of the left sibling (left sibling loses one node but still has at least the minimum number of elements)
*# The tree is now balanced
* Otherwise, if both immediate siblings have only the minimum number of elements, then merge with a sibling sandwiching their separator taken off from their parent
*# Copy the separator to the end of the left node (the left node may be the deficient node or it may be the sibling with the minimum number of elements)
*# Move all elements from the right node to the left node (the left node now has the maximum number of elements, and the right node – empty)
*# Remove the separator from the parent along with its empty right child (the parent loses an element)
*#* If the parent is the root and now has no elements, then free it and make the merged node the new root (tree becomes shallower)
*#* Otherwise, if the parent has fewer than the required number of elements, then rebalance the parent
 
:<small>'''Note''': The rebalancing operations are different for B<sup>+</sup>-trees (e.g., rotation is different because parent has copy of the key) and B*-tree (e.g., three siblings are merged into two siblings).</small>
 
===Sequential access===
While freshly loaded databases tend to have good sequential behavior, this behavior becomes increasingly difficult to maintain as a database grows, resulting in more random I/O and performance challenges.<ref>{{cite web |url=http://www.cs.sunysb.edu/~bender/pub/cache-oblivious-btree.ps |title=Cache Oblivious B-trees |publisher=State University of New York (SUNY) at Stony Brook |accessdate=2011-01-17}}</ref>
 
===Initial construction===
In applications, it is frequently useful to build a B-tree to represent a large existing collection of data and then update it incrementally using standard B-tree operations. In this case, the most efficient way to construct the initial B-tree is not to insert every element in the initial collection successively, but instead to construct the initial set of leaf nodes directly from the input, then build the internal nodes from these. This approach to B-tree construction is called [[bulkloading]]. Initially, every leaf but the last one has one extra element, which will be used to build the internal nodes.{{Citation needed|date=July 2008}}
 
For example, if the leaf nodes have maximum size 4 and the initial collection is the integers 1 through 24, we would initially construct 4 leaf nodes containing 5 values each and 1 which contains 4 values:
 
{|
|-
|
{|class="wikitable"
|-
|1||2||3||4||5
|}
||
{|class="wikitable"
|-
|6||7||8||9||10
|}
||
{|class="wikitable"
|-
|11||12||13||14||15
|}
||
{|class="wikitable"
|-
|16||17||18||19||20
|}
||
{|class="wikitable"
|-
|21||22||23||24
|}
 
|}
 
We build the next level up from the leaves by taking the last element from each leaf node except the last one. Again, each node except the last will contain one extra value. In the example, suppose the internal nodes contain at most 2 values (3 child pointers). Then the next level up of internal nodes would be:
 
{|
|-
| colspan=3 |
{|class="wikitable" align=center
|-
|5||10||15
|}
| &nbsp;
| colspan=2 |
{|class="wikitable" align=center
|-
|20
|}
|-
|
{|class="wikitable"
|-
|1||2||3||4
|}
||
{|class="wikitable"
|-
|6||7||8||9
|}
||
{|class="wikitable"
|-
|11||12||13||14
|}
| &nbsp;
|
{|class="wikitable"
|-
|16||17||18||19
|}
||
{|class="wikitable"
|-
|21||22||23||24
|}
 
|}
 
This process is continued until we reach a level with only one node and it is not overfilled. In the example only the root level remains:
 
{|
|-
| colspan=6 |
{|class="wikitable" align=center
|-
|15
|}
|-
| colspan=3 |
{|class="wikitable" align=center
|-
|5||10
|}
| &nbsp;
| colspan=2 |
{|class="wikitable" align=center
|-
|20
|}
|-
|
{|class="wikitable"
|-
|1||2||3||4
|}
||
{|class="wikitable"
|-
|6||7||8||9
|}
||
{|class="wikitable"
|-
|11||12||13||14
|}
| &nbsp;
|
{|class="wikitable"
|-
|16||17||18||19
|}
||
{|class="wikitable"
|-
|21||22||23||24
|}
 
|}
 
==In filesystems==
In addition to its use in databases, the B-tree is also used in filesystems to allow quick random access to an arbitrary block in a particular file. The basic problem is turning the file block <math>i</math> address into a disk block (or perhaps to a [[cylinder-head-sector]]) address.
 
Some operating systems require the user to allocate the maximum size of the file when the file is created. The file can then be allocated as contiguous disk blocks. Converting to a disk block: the operating system just adds the file block address to the starting disk block of the file. The scheme is simple, but the file cannot exceed its created size.
 
Other operating systems allow a file to grow. The resulting disk blocks may not be contiguous, so mapping logical blocks to physical blocks is more involved.
 
[[MS-DOS]], for example, used a simple [[File Allocation Table]] (FAT). The FAT has an entry for each disk block,<ref group="note">For FAT, what is called a "disk block" here is what the FAT documentation calls a "cluster", which is fixed-size group of one or more contiguous whole physical disk [[Cylinder-head-sector|sectors]]. For the purposes of this discussion, a cluster has no significant difference from a physical sector.</ref> and that entry identifies whether its block is used by a file and if so, which block (if any) is the next disk block of the same file. So, the allocation of each file is represented as a [[linked list]] in the table. In order to find the disk address of file block <math>i</math>, the operating system (or disk utility) must sequentially follow the file's linked list in the FAT. Worse, to find a free disk block, it must sequentially scan the FAT. For MS-DOS, that was not a huge penalty because the disks and files were small and the FAT had few entries and relatively short file chains. In the [[FAT12]] filesystem (used on floppy disks and early hard disks), there were no more than 4,080 <ref group="note">Two of these were reserved for special purposes, so only 4078 could actually represent disk blocks (clusters).</ref> entries, and the FAT would usually be resident in memory. As disks got bigger, the FAT architecture began to confront penalties. On a large disk using FAT, it may be necessary to perform disk reads to learn the disk location of a file block to be read or written.
 
[[TOPS-20]] (and possibly [[TOPS-20#TENEX|TENEX]]) used a 0 to 2 level tree that has similarities to a B-tree{{Citation needed|date=October 2009}}. A disk block was 512 36-bit words. If the file fit in a 512 (2<sup>9</sup>) word block, then the file directory would point to that physical disk block. If the file fit in 2<sup>18</sup> words, then the directory would point to an aux index; the 512 words of that index would either be NULL (the block isn't allocated) or point to the physical address of the block. If the file fit in 2<sup>27</sup> words, then the directory would point to a block holding an aux-aux index; each entry would either be NULL or point to an aux index. Consequently, the physical disk block for a 2<sup>27</sup> word file could be located in two disk reads and read on the third.
 
Apple's filesystem [[HFS+]], Microsoft's [[NTFS]],<ref name=insidewin2kntfs>{{cite web
| url = http://msdn2.microsoft.com/en-us/library/ms995846.aspx
| title = Inside Win2K NTFS, Part 1
| author = [[Mark Russinovich]]
| publisher = [[MSDN|Microsoft Developer Network]]
| accessdate = 2008-04-18
| archiveurl= http://web.archive.org/web/20080413181940/http://msdn2.microsoft.com/en-us/library/ms995846.aspx| archivedate= 13 April 2008 <!--DASHBot-->| deadurl= no}}</ref> AIX (jfs2) and some [[Linux]] filesystems, such as [[btrfs]] and [[Ext4]], use B-trees.
 
B*-trees are used in the [[Hierarchical File System|HFS]] and [[Reiser4]] [[file system]]s.
 
==Variations==
 
===Access concurrency===
Lehman and Yao<ref>{{cite web|url=http://portal.acm.org/citation.cfm?id=319663&dl=GUIDE&coll=GUIDE&CFID=61777986&CFTOKEN=74351190 |title=Efficient locking for concurrent operations on B-trees |doi=10.1145/319628.319663 |publisher=Portal.acm.org |date= |accessdate=2012-06-28}}</ref> showed that all read locks could be avoided (and thus concurrent access greatly improved) by linking the tree blocks at each level together with a "next" pointer. This results in a tree structure where both insertion and search operations descend from the root to the leaf. Write locks are only required as a tree block is modified. This maximizes access concurrency by multiple users, an important consideration for databases and/or other B-tree based [[ISAM]] storage methods. The cost associated with this improvement is that empty pages cannot be removed from the btree during normal operations. (However, see <ref>http://www.dtic.mil/cgi-bin/GetTRDoc?AD=ADA232287&Location=U2&doc=GetTRDoc.pdf</ref> for various strategies to implement node merging, and source code at.<ref>{{cite web|url=https://github.com/malbrain/Btree-source-code|title=Downloads - high-concurrency-btree - High Concurrency B-Tree code in C - GitHub Project Hosting |date= |accessdate=2014-01-27}}</ref>)
 
United States Patent 5283894, granted in 1994, appears to show a way to use a 'Meta Access Method' <ref>[http://www.freepatentsonline.com/5283894.html Lockless Concurrent B+Tree]</ref> to allow concurrent B+Tree access and modification without locks. The technique accesses the tree 'upwards' for both searches and updates by means of additional in-memory indexes that point at the blocks in each level in the block cache. No reorganization for deletes is needed and there are no 'next' pointers in each block as in Lehman and Yao.
 
==See also==
* [[R-tree]]
 
==Notes==
{{reflist|30em|group=note}}
 
==References==
{{reflist|30em}}
 
;General
* {{Citation
| last = Bayer
| first = R.
| author-link = Rudolf Bayer
| last2 = McCreight
| first2 = E.
| author2-link = Edward M. McCreight
| title = Organization and Maintenance of Large Ordered Indexes
| journal = Acta Informatica
| volume = 1
| issue = 3
| pages = 173–189
| date =
| year = 1972
| url = http://www.minet.uni-jena.de/dbis/lehre/ws2005/dbs1/Bayer_hist.pdf
| doi =
| ref = harv}}
* {{Citation
| last = Comer
| first = Douglas
| author-link = Douglas Comer
| title = The Ubiquitous B-Tree
| journal = Computing Surveys
| volume = 11
| issue = 2
| pages = 123–137
| date = June 1979
| issn = 0360-0300
| url =
| doi = 10.1145/356770.356776
| ref = harv}}.
* {{Citation
| last = Cormen
| first = Thomas
| author-link = Thomas H. Cormen
| last2 = Leiserson
| first2 = Charles
| author2-link = Charles E. Leiserson
| last3 = Rivest
| first3 = Ronald
| author3-link = Ronald L. Rivest
| last4 = Stein
| first4 = Clifford
| author4-link = Clifford Stein
| title = [[Introduction to Algorithms]]
| place =
| publisher = MIT Press and McGraw-Hill
| year = 2001
| volume =
| edition = Second
| page =
| pages = 434–454
| url =
| doi =
| isbn = 0-262-03293-7}}. Chapter 18: B-Trees.
* {{Citation
| last =Folk
| first =Michael J.
| author-link =
| last2 =Zoellick
| first2 =Bill
| author2-link =
| title =File Structures
| place =
| publisher =Addison-Wesley
| year = 1992
| volume =
| edition =2nd
| page =
| pages =
| url =
| doi =
| isbn = 0-201-55713-4
| ref =harv}}
* {{Citation
| last = Knuth
| first = Donald
| author-link = Donald Knuth
| series = [[The Art of Computer Programming]]
| title = Sorting and Searching
| place =
| publisher = Addison-Wesley
| year = 1998
| volume = Volume 3
| edition = Second
| page =
| pages =
| url =
| doi =
| isbn = 0-201-89685-0 }}. Section 6.2.4: Multiway Trees, pp.&nbsp;481–491. Also, pp.&nbsp;476–477 of section 6.2.3 (Balanced Trees) discusses 2-3 trees.
* {{Citation
| last = Mond
| first = Yehudit
| author-link = Yehudit Mond
| last2 = Raz
| first2 = Yoav
| author2-link = Yoav Raz
| title = Concurrency Control in B+-Trees Databases Using Preparatory Operations
| journal = VLDB'85, Proceedings of 11th International Conference on Very Large Data Bases
| pages = 331–334
| year = 1985
| url = http://www.informatik.uni-trier.de/~ley/db/conf/vldb/MondR85.html
| ref = harv}}.
 
===Original papers===
* {{Citation
| last = Bayer
| first = Rudolf
| author-link = Rudolf Bayer
| last2 = McCreight
| first2 = E.
| author2-link = Edward M. McCreight
| title = Organization and Maintenance of Large Ordered Indices
| place =
| publisher = Boeing Scientific Research Laboratories
| year = 1970
| volume = Mathematical and Information Sciences Report No. 20
| edition =
| page =
| pages =
| url =
| doi =
| date = July 1970
| isbn = }}.
* {{Citation
| last = Bayer
| first = Rudolf
| author-link = Rudolf Bayer
| contribution = Binary B-Trees for Virtual Memory
| series = Proceedings of 1971 ACM-SIGFIDET Workshop on Data Description, Access and Control
| year = 1971
| pages =
| place = San Diego, California
| publisher =
| url =
| doi =
}}. November 11–12, 1971.
 
==External links==
* [http://www.youtube.com/watch?v=I22wEC1tTGo B-tree lecture] by David Scot Taylor, SJSU
* [http://slady.net/java/bt/view.php B-Tree animation applet] by slady
* [http://www.scholarpedia.org/article/B-tree_and_UB-tree B-tree and UB-tree on Scholarpedia] Curator: Dr Rudolf Bayer
* [http://www.bluerwhite.org/btree B-Trees: Balanced Tree Data Structures]
* [http://www.nist.gov/dads/HTML/btree.html NIST's Dictionary of Algorithms and Data Structures: B-tree]
* [http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html B-Tree Tutorial]
* [http://www.boilerbay.com/infinitydb/TheDesignOfTheInfinityDatabaseEngine.htm The InfinityDB BTree implementation]
* [http://supertech.csail.mit.edu/cacheObliviousBTree.html Cache Oblivious B(+)-trees]
* [http://www.nist.gov/dads/HTML/bstartree.html Dictionary of Algorithms and Data Structures entry for B*-tree]
* [http://opendatastructures.org/versions/edition-0.1e/ods-java/14_2_B_Trees.html Open Data Structures - Section 14.2 - B-Trees]
* [http://www.chiark.greenend.org.uk/~sgtatham/algorithms/cbtree.html Counted B-Trees]
 
{{CS-Trees}}
{{Data structures}}
 
{{DEFAULTSORT:B-Tree}}
[[Category:Trees (data structures)]]
[[Category:1971 introductions]]
[[Category:B-tree]]
[[Category:Database index techniques]]
 
{{Link GA|de}}

Revision as of 15:34, 3 March 2014

I am 24 years old and my name is Alfred Gaunson. I life in Bergeijk (Netherlands).

Also visit my web-site ... FIFA Coin generator