MCS-208 · Unit 7

Advanced Trees

Section 5 of 9

Block 3 · Unit 7Advanced Trees11 min · Section 5 of 9

7.5B-Trees: Structure and Search

Covers IGNOU MCS-208 §7.4, 7.4.1

Binary trees are good when everything is in memory. Databases keep their data on disks, where reading one block takes far longer than thousands of comparisons in memory. A B-tree reduces the number of block reads by storing many keys in each node, which makes the tree very wide and very short.

Why wide trees

If each node holds up to m−1m - 1 keys and has up to mm children, the height is about log⁡mn\log_m n instead of log⁡2n\log_2 n. With m=200m = 200, a tree of height 3 can hold about 8 million keys, so any key is found with about 3 disk reads.

Definition

For order 5: every node has at most 4 keys; every node except the root has at least ⌈5/2⌉−1=2\lceil 5/2 \rceil - 1 = 2 keys.

A B-tree of order 5 (Figure 7.11)
                       [ M ]
           /                        \
       [ E H ]                   [ P T X ]
      /   |   \               /    |    |    \
 [B D] [F G] [I K L]      [N O] [Q S] [V W] [Y Z]

All leaves are on level 3, every non-root node has 2 to 4 keys, and the keys in each node separate its children.

Searching

To search for key kk in node xx with keys x.key1<⋯<x.keynx.key_1 < \dots < x.key_n:

  1. Find the smallest ii with k≤x.keyik \le x.key_i (linear or binary search inside the node).
  2. If k=x.keyik = x.key_i, the key is found.
  3. Otherwise, if xx is a leaf, kk is not in the tree; else read child cic_i (the child to the left of x.keyix.key_i, or the last child if kk is larger than every key) from disk and search it.

The number of nodes read is at most the height, O(log⁡mn)O(\log_m n); the work inside each node is O(m)O(m).

Key takeaways

  • B-tree nodes hold many sorted keys, so the tree is wide and shallow: height O(log⁡mn)O(\log_m n).
  • Order mm: at most mm children / m−1m - 1 keys; non-root nodes at least ⌈m/2⌉\lceil m/2 \rceil children / ⌈m/2⌉−1\lceil m/2 \rceil - 1 keys.
  • All leaves are on the same level.
  • Search: find the position in the node, then follow one child; few disk reads.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In a B-tree of order 7, what is the minimum number of keys in a non-root node?

Show answer

Answer: B. 3

⌈7/2⌉−1=4−1=3\lceil 7/2 \rceil - 1 = 4 - 1 = 3. 6 is the maximum number of keys (m−1m - 1).

Question 2 · multiple choice

What is the maximum number of keys in a node of a B-tree of order 5?

Show answer

Answer: D. 4

At most m=5m = 5 children, so at most m−1=4m - 1 = 4 keys.

Question 3 · multiple choice

Which condition does not belong to the definition of a B-tree?

Show answer

Answer: C. Every node has exactly two children.

That would make it a binary tree. B-tree nodes have between ⌈m/2⌉\lceil m/2 \rceil and mm children (except the root and leaves).

Question 4 · multiple choice

In the order-5 B-tree of Figure 7.11, which nodes are read when searching for S?

Show answer

Answer: C. [M], [P T X], [Q S]

S > M: go right to [P T X]; P < S < T: follow the child between P and T, [Q S], where S is found.

Question 5 · multiple choice

Why are B-trees used for databases on disk instead of AVL trees?

Show answer

Answer: D. A B-tree node fills a disk block with many keys, so far fewer slow disk reads are needed.

The cost on disk is the number of blocks read, which is the height; log⁡mn\log_m n is much smaller than log⁡2n\log_2 n for large mm.

Question 6 · short answer

Define a B-tree of order mm and state the minimum and maximum number of keys per node for m=5m = 5.

Show worked answer

A B-tree of order mm is a multiway search tree in which every node has at most mm children and m−1m - 1 keys in sorted order; a node with kk children has k−1k - 1 keys that separate the children’s key ranges; every node except the root has at least ⌈m/2⌉\lceil m/2 \rceil children (if internal) and at least ⌈m/2⌉−1\lceil m/2 \rceil - 1 keys; the root, if not a leaf, has at least 2 children; and all leaves are on the same level. For m=5m = 5: at most 4 keys per node; at least 2 keys per non-root node (the root may have just 1).

↑ ↓ move↵ openTab switch type