Btree
Btree
part or all of the tree must be maintained in secondary storage. Since disk accesses are expensive (time consuming) operations, a btree tries to minimize the number of disk accesses. Unlike a binary-tree, each node of a b-tree may have a variable number of keys and children. The keys are stored in non-decreasing order. Each key has an associated child that is the root of a subtree containing all nodes with keys less than or equal to the key but greater than the preceeding key. A node also has an additional rightmost child that is the root for a subtree containing all keys greater than any keys in the node. Operations on B-Trees Since all nodes are assumed to be stored in secondary storage (disk) rather than primary storage (memory), all references to a given node be be preceeded by a read operation denoted by Disk-Read. The search operation on a b-tree is analogous to a search on a binary tree. Instead of choosing between a left and a right child as in a binary tree, a b-tree search must make an n-way choice. The correct child is chosen by performing a linear search of the values in the node. After finding the value greater than or equal to the desired value, the child pointer to the immediate left of that value is followed. If all values are less than the desired value, the rightmost child pointer is followed. Of course, the search can be terminated as soon as the desired node is found. Since the running time of the search operation depends upon the height of the tree, B-Tree-Search is O(logt n). B-Tree-Search(x, k) i <- 1 while i <= n[x] and k > keyi[x] do i <- i + 1 if i <= n[x] and k = keyi[x] then return (x, i) if leaf[x] then return NIL else Disk-Read(ci[x]) return B-Tree-Search(ci[x], k)
B+ tree
In computer science, a B+ tree or B plus tree is a type of tree which represents sorted data in a way that allows for efficient insertion, retrieval and removal of records, each of which is identified by a key. It is a dynamic, multilevel index, with maximum and minimum bounds on the number of keys in each index segment
(usually called a "block" or "node"). In a B+ tree, in contrast to a B-tree, all records are stored at the leaf level of the tree; only keys are stored in interior nodes.
A B + -Tree consists of one or more blocks of data, called nodes, linked together by pointers. The B + -Tree is a tree structure. The tree has a single node at the top, called the root node. The root node points to two or more blocks , called child nodes. Each child nodes points to further child nodes and so on. The B + -Tree consists of two types of (1) internal nodes and (2) leaf nodes: Internal nodes point to other nodes in the tree. Leaf nodes point to data in the database using data pointers. Leaf nodes also contain an additional pointer, called the sibling pointer, which is used to improve the efficiency of certain types of search. All the nodes in a B + -Tree must be at least half full except the root node which may contain a minimum of two entries. The algorithms that allow data to be inserted into and deleted from a B + -Tree guarantee that each node in the tree will be at least half full. Searching for a value in the B + -Tree always starts at the root node and moves downwards until it reaches a leaf node. Both internal and leaf nodes contain key values that are used to guide the search for entries in the index. The B + -Tree is called a balanced tree because every path from the root node to a leaf node is the same length. A balanced tree means that all searches for individual values require the same number of nodes to be read from the disc. Internal Nodes An internal node in a B + -Tree consists of a set of key values and pointers. The set of keys and values are ordered so that a pointer is followed by a key value. The last key value is followed by one pointer. Each pointer points to nodes containing values that are less than or equal to the value of the key immediately to its right The last pointer in an internal node is called the infinity pointer. The infinity pointer points to a node containing key values that are greater than the last key value in the node. When an internal node is searched for a key value, the search begins at the leftmost key value and moves rightwards along the keys. If the key value is less than the sought key then the pointer to the left of the key is known to point to a node containing keys less than the sought key. If the key value is greater than or equal to the sought key then the
pointer to the left of the key is known to point to a node containing keys between the previous key value and the current key value. Leaf Nodes A leaf node in a B + -Tree consists of a set of key values and data pointers. Each key value has one data pointer. The key values and data pointers are ordered by the key values. The data pointer points to a record or block in the database that contains the record identified by the key value. For instance, in the example, above, the pointer attached to key value 7 points to the record identified by the value 7. Searching a leaf node for a key value begins at the leftmost value and moves rightwards until a matching key is found. The leaf node also has a pointer to its immediate sibling node in the tree. The sibling node is the node immediately to the right of the current node. Because of the order of keys in the B + -Tree the sibling pointer always points to a node that has key values that are greater than the key values in the current node.