Btree
Btree
IS 320
B Tree
A B-tree of order m (the maximum number of children for
each node) is a tree which satisfies the following
properties:
1. Every node has at most m children.
2. Every node (except root and leaves) has at least m⁄2
children.
3. The root has at least two children if it is not a leaf node.
4. All leaves appear in the same level, and carry information.
5. A non-leaf node with k children contains k–1 keys.
k1 k2 k3 ... km-2 km-
1
T0 T1 T2 Tm-2 Tm-1
key < k1 k1 < key < k2 k2 < key < k3 km-2 < key < km-1 key > km-1
Multi-way tree
Example: A B-tree of order 4
Note:
• The data references are not shown.
• The leaf references are to empty subtrees
B-Tree Examples
Operations
• B-Tree of order 4
– Each node has at most 4 pointers and 3 keys,
and at least 2 pointers and 1 key.
• Insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8
• Delete: 2, 21, 10, 3, 4
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, so evenly split it into two nodes.
• 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.
• Insert the separation value 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)
Insert 5, 3, 21
*5* a
*3*5* a
* 3 * 5 * 21 * a
Insert 9
*9* a
b c
*3*5* * 21 *
b d c
*1*2* *5* * 13 * 21 *
b d c
*1*2* *5*7* * 10 * 13 * 21 *
b d c e
*1*2* *5*7* * 10 * 12 * * 21 *
f g
*3*7* * 13 *
b d h c e
*1*2* *4*5* *8* * 10 * 12 * * 21 *
If the right sibling has more than the minimum number of elements
• Copy key (from the parent) to end of deficient node
• Replace key in parent with first element in right sibling.
• Tree is balanced
ELSE If the left sibling has more than the minimum number of elements
• Copy the key (from the parent) to the start of the deficient node
• Replace the key in the parent with the last element of the left sibling
• Tree is balanced
ELSE If both immediate siblings have only minimum number of elements
• Copy the key to the end of the left node
• Move all elements from the right node to the left node (right becomes
empty)
• Remove the key (from the parent) and remove its empty right child
– IF parent is ROOT and has no elements, FREE it and make the merged node as
ROOT
– IF parent is NOT ROOT and has less elements, REBALANCE it
Delete 2
*9* a
f g
*3*7* * 13 *
b d h c e
*1* *4*5* *8* * 10 * 12 * * 21 *
f g
*3*7* * 12 *
b d h c e
*1* *4*5* *8* * 10 * * 13 *
b d h e
*1* *4*5* *8* * 12 * 13 *
b d h e
*1* *5* *8* * 12 * 13 *
b h e
*1*5* *8* * 12 * 13 *