Lesson contents

B-trees and multiway indexes

A B-tree page holds several sorted keys. The gaps between keys point to ranges on the next page, so a lookup can skip many shelves at once.

Simple example

A page has separator keys 20 and 50. For a key other than these two, choose one of three child ranges. Print the one-based child number for 63. It belongs in child 3.

Start20,50 63
Result3
Program.cs
using System;

int[] separators = new int[] { 20, 50 };
int target = 63;
int child = 0;
while ((child < 2 && target > separators[child])) {
    child += 1;
}
Console.WriteLine((child + 1));
Output
3
Advertisements

Everyday examples

Open the correct archive drawer

A drawer index has separators 100 and 300. Which drawer could hold record 240?

Start100 300 240
Result2

Record 240 lies between the two separators. Read the middle child page.

Find an exact catalogue key

A catalogue page already holds keys 24 and 65. A reader wants key 65. Do you descend further?

Start24 65
Result65

In a B-tree that stores records at internal keys, the match on this page finishes the search.

Split a full index page

A full page in a minimum-degree-two B-tree contains 10, 20 and 30. Which key is promoted when that page splits?

Start10 20 30
Result20

The middle key 20 moves up. The left and right pages keep 10 and 30 respectively, maintaining the search ranges.

Cost and limits

With bounded page capacity and balanced leaves, searches visit logarithmically many levels. During insertion a full node may split and send a separator to its parent.