B Tree

来源:互联网 发布:mac如何把桌面的图标 编辑:程序博客网 时间:2024/04/30 10:51

Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer

Proporities

  • 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 ⌈m⁄2⌉ 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 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: a1 and a2. All values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 and a2, and all values in the rightmost subtree will be greater than a2.

Operations

  • 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.

Insert

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:
1. 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.
2. 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).

Remove

To remove a node, you need to locate and delete the item, then restructure the tree to regain its invariants.
There are two special cases to consider when deleting an element:
1. The element in an internal node is a separator for its child nodes
2. Deleting an element may put its node under the minimum number of elements and children
For the first case, because 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.
For the second case, if the value is in a leaf node, simply delete it from the node; If underflow happens, rebalance the tree.

Rebalance

  • 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.
  • 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 parentCopy 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
0 0