Introduction to B-trees

Helene
Geek Culture
Published in
7 min readMay 27, 2021

--

Photo by Arnaud Mesureur on Unsplash

One of the most used data structures in computer science is the tree. This has resulted in many different versions being developed. In this article, we will become better acquaintances with the so-called B-trees. To do so, we will first understand the different parts of it and its structure. Afterward, we will see how we can perform different operations on them, i.e., searching, insertion, and deletion. In the end, we will wrap it up with an explanation of the pros and cons of using B-trees to store data.

Understanding the B-tree

The B-tree is a so-called balanced tree, meaning that all paths from the root to a leaf have the same length. It can be divided into three parts: the root, intermediate layer(s), and the leaves. We can visualize it:

In our case, we assume that the maximum number of elements in a node is 3. It also means it can have up to 4 children. Let us consider the different cases:

  • If a node contains one data element, then Val has two subtrees namely left and right.
  • If a node contains two data elements leftVal and rightVal, it has three subtrees namely left, middle and right.
  • If it contains three data elements, i.e., leftVal, middleVal and rightVal, then it can have four subtrees; left, middleLeft, middleRight, and right.

--

--