3

According to Knuth's definition, 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) has at least ⌈m⁄2⌉ children.

(3) The root has at least two children if it is not a leaf node.

(4) A non-leaf node with k children contains k−1 keys.

(5) All leaves appear in the same level, and carry information.

Source: Wikipedia

Some visualizations of B-Trees look like this:

enter image description here

From this visualization, I would think that each node has an array datastructure (or at least something similar).

Others look like this: enter image description here

This looks rather as a list-like datastructure.

So my question is:

Which datastructure do B-trees use?

Examples for the usage in my algorithms class were databases and filesystems. Does anybody know how SQLite implements B-tree nodes? Or ext3? Or any other (well-known) real-world example?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • There are B-trees and B+-trees. The former stores values with the associated keys. The later duplicate keys and store values in leaves. – didierc Oct 28 '12 at 22:29
  • The SQLite B-tree implementaton is described in some detail in section 1.5 here: https://www.sqlite.org/fileformat2.html – bsa Jun 26 '15 at 15:59

1 Answers1

0

B-Tree itself is the datastructure (also an indexstructure). Here is an pseudocode example (without needed methods and some needed definitions, this is an good excersize!):

The Body of a node:

class BNode
{
    int keys[];
    BNode children[];

    public BNode() {}

    public BNode() {}

    public int getValue(int key) {}

    public BNode getChildren(int key) {}
}

And the body of the B-Tree:

class BTree
{
    BNode root;

    BTree()
    {
        root = new BNode(null);
    }

    BNode search(int key) {}

    void insert(int key) {}

    void delete(int key) {}
}

In real world:

Here is the B-Tree implementation of PostgreSQL which is used for indexing the database.

zwergmaster
  • 253
  • 2
  • 12