Lecture Notes for CS 512, Algorithm Design
B-Trees - 14 February 2000
- sometimes a large height is a problem
- a huge number of keys
- expensive node access - imagine accessing each red-black tree node via
the www
- the solution to both problems is to increase the node branching
- b-trees
- each n node has
- n.keycnt keys
- n.keys[1] <= n.keys[2] <= ... <= n.keys[n.keycnt]
- n.leaf
- internal nodes have n.keycnt + 1 children, n.children[1], ...
- the bst property: n.children[i].key[..] <= n.key[i] <= n.children[i + 1].key[..]
- leaves are at the same depth - perfectly balanced
- each b-tree has a minimum degree t
- non-root nodes have at least t - 1 keys and t children for
internal nodes
- every node has at most 2t - 1 keys and 2t children for
internal nodes
- searching for a key
- creating a new b-tree
- splitting a node n
- n has 2t - 1 keys; don't split nonfull nodes
- n's median key moves into the parent
- the parent must not be full
- if n is the root, the b-tree grows by one level
- expensive node access means one-pass traversals are important
- make sure a node isn't too full before trying to insert
- make sure a node isn't too empty before trying to delete
- inserting a key
- to avoid the 2t - 1 problem, split the next node before recursing
to it
- insert the key in a leaf node
- deleting a key k
- to avoid the t - 1 problem, make sure a node has at
least t keys before recursing to it
- if k is in a leaf, delete it
- if k is in an interior node n, use the bst trick: replace the
key with its predecessor or successor
- if k's left child has at least t keys, find k's
predecessor in the left child, delete it, and replace k with
its predecessor in n
- if k's right child has at least t keys, find k's
predecessor in the right child, delete it, and replace k with
it's predecessor in n
- merge the two t - 1 subtrees with the key, delete the key and
the former right subtree from the interior node, and recurse to the
new child
- if the key isn't in the interior node
- find the child that might contain the key
- if the child has only t - 1 keys
- move a key from a sibling with at least t keys
- move the interior-node key between the child and its sibling down
into the child
- move the proper sibling key (leftmost or rightmost) up into the
interior node to replace the moved key
- shift the key-less child pointer from the sibling to the child
- merge the child with a t - 1 key sibling
- the new child is the old child, the key between the child and
sibling, and the sibling
- shift the keys and children in the interior node to compress out
the old key and sibling pointer
This page last modified on 16 February 2000.