What is 2-3 Tree?
This lesson is an introduction to 2-3 trees, its properties, and an example with the basic operations that this data structure offers.
We'll cover the following
Introduction #
A 2-3 Tree is another form of search tree but is very different from Binary Search Tree. Unlike BST, 2-3 Tree is a balanced and ordered search tree which provides a very efficient storage mechanism to guarantee fast operations. In this chapter, we will take a detailed look at 2-3 Trees’ structure, the limitations it follows, and how elements are inserted and deleted from it.
One key feature of a 2-3 Tree is that it remains balanced, no matter how many insertions or deletion you perform. The leaf nodes are always present on the same level and are quite small in number. This is to make sure the height doesn’t increase up to a certain level as the time complexity of all the operations is mainly dependent upon it. Ideally, we want the height to be in logarithmic terms because, as the tree grows larger, it will require more time to perform operations. In 2-3 Trees, the height is logarithmic in the number of nodes present in the tree. They generally come in 2 forms:
- 2 Node Tree
- 3 Node Tree
See the figures below to get the idea of how they are different. Given below is a 2-3 Tree with only two nodes. To keep it ordered, the left child key must be smaller than the parent node key. Similarly, the right child key must be greater than the parent key.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.