B+ Tree Ready
keys 8
levels 2
per node 1–3
  • Current
  • Compared
  • Too many / few
  • New / found
  • 12signpost (inner node)

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Leaves and signposts

A B+ tree has two kinds of node. LEAVES hold every key (in a database: the key plus a pointer to its row) and a next pointer to the leaf on their right. INNER nodes hold only signposts — copies of keys that tell a search which child to enter. A key equal to a signpost is always found on its right.

struct Leaf {
    int n;
    int keys[ORDER - 1];       // the real keys
    struct Leaf *next;         // the leaf to the right
};

struct Inner {
    int n;
    int keys[ORDER - 1];       // signposts (copies)
    void *child[ORDER];        // key >= keys[i-1] → child[i]
};

Search

  1. Follow the signposts down: equal keys go RIGHT.
  2. Always continue to a leaf.
  3. Look for the key in that leaf.
  4. Every search visits exactly one node per level.

Insert

  1. Walk down to the leaf for the key.
  2. Insert it in sorted order.
  3. If the leaf is full, split it into two leaves…
  4. …and link them: left.next = right.

Leaf split vs inner split

  1. Leaf split: COPY the right leaf's first key up.
  2. The key stays in the leaf — all data is in leaves.
  3. Inner split: MOVE the middle signpost up (like a B-tree).
  4. A root split makes the tree one level taller.

Build tree

  1. Start with an empty tree.
  2. Insert the typed keys one by one, in that order.
  3. Every full leaf splits and COPIES a signpost up.
  4. The leaf chain always reads the keys in sorted order.

Range scan

  1. Search for the smallest key of the range.
  2. Read keys in that leaf.
  3. Follow next to the following leaf.
  4. Stop at the first key past the range.

Delete

  1. Remove the key from its leaf.
  2. A signpost with the same value may stay — it still guides correctly.
  3. Too few keys? Borrow from a sibling leaf (fix the signpost)…
  4. …or merge the two leaves and drop the signpost.

Why databases love it

  1. Inner nodes are small, so more fit in memory.
  2. Every lookup has the same cost (tree height).
  3. Range queries read leaves one after another.
  4. Used by MySQL (InnoDB), PostgreSQL, SQLite and file systems.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 SEARCH(key)
2 x ← root
3 while x is not a leaf
4 i ← number of signposts in x that are ≤ key
5 x ← x.child[i]
6 return key is in x.keys
Complexity

How fast is each operation?

n = number of keys, m = order, k = number of keys the range returns. A range scan costs one search plus reading the leaves that hold the k results. Building a tree of n keys is n inserts, so O(n log n).

Operation Best Average Worst Speed
Search O(log n) O(log n) O(log n) Grows with n
Insert O(log n) O(log n) O(log n) Grows with n
Delete O(log n) O(log n) O(log n) Grows with n
Range scan (k results) O(log n) O(log n + k) O(log n + k) Grows with n
Build tree (n keys) O(n log n) O(n log n) O(n log n) Grows with n

B-tree vs B+ tree

B-tree B+ tree
Where the keys are In every node Only in the leaves (inner nodes hold copies)
Leaf split Middle key MOVES up First key of the right leaf is COPIED up
Search stops At any level Always at a leaf
Leaves linked? No Yes — left to right
Range query Climb up and down the tree Walk along the leaves
Used in Some file systems Almost every database index

Range scan of 6 … 20

Step What happens
1 Search for 6: follow the signposts down to its leaf
2 Output the keys ≥ 6 in that leaf
3 Follow next → read the following leaf
4 Stop at the first key larger than 20
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

Where are all the keys of a B+ tree stored?

Question 2

A leaf splits into [5, 6] and [7, 10]. What goes up into the parent?

Question 3

Why does a B+ tree link its leaves?

Question 4

A signpost is 12 and you search for 12. Which child do you go to?

Question 5

After deleting 12 from its leaf, a signpost 12 is still in an inner node. Is the tree broken?

Question 6

How many nodes does a search visit in a B+ tree with 3 levels?

Assessment

Trees quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.