What is a 2-3 Tree?

A 2-3 Tree is a multiway search tree. It’s a self-balancing tree; it’s always perfectly balanced with every leaf node at equal distance from the root node.

Other than the leaf nodes, every node can be one of two types:

  • 2-Node: A node with a single data element that has two child nodes

  • 3-Node: A node with two data elements that has three child nodes

svg viewer

Structure of a 2-Node

Like a binary search tree, when a node only contains one data element the left subtree of that node (parent node) will contain nodes with data elements valued less than the parent node’s data elements, while the right subtree will contain nodes with data elements valued greater than the parent node’s data elements. Nodes having the same value as their parent node can be inserted in either the left or the right subtree.

svg viewer

Structure of a 3-Node

When a node contains two data elements, the left data element is less than the right data element.

svg viewer

The leftmost subtree of a node containing two data elements, we will call the parent node, will contain nodes with data elements less than the parent node’s lesser valued data element.

svg viewer

The rightmost subtree of the parent node will contain nodes with data elements greater than the parent node’s greater valued data element.

svg viewer

As a node with two data elements can have three child nodes, the middle subtree of the parent node will have nodes with data elements greater than the lesser element of the parent node, but lesser than the greater element of the parent node.

svg viewer

Insertion

If a tree is empty, create a root node and insert the new element. In our examples below, the red square represents the new element to be inserted.

Inserting the data element 50
Inserting the data element 50

If a tree is not empty, locate the appropriate leaf node. If the leaf node has 1 data element, insert the new element in the leaf node.

Inserting the data element 30
Inserting the data element 30

After insertion, if the leaf node has more than two data elements, split the data elements into 3 separate nodes and appoint the node with the middle value as the parent node with the other two nodes as its child nodes. In our example below we end up with a 2-Node.

Inserting the data element 10
Inserting the data element 10

By inserting two more elements, we can create a 3-Node.

Inserting the data element 70
Inserting the data element 70
Inserting the data element 60
Inserting the data element 60

Time Complexity

The property of being perfectly balanced, enables the 2-3 Tree operations of insert, delete and search to have a time complexity of O(log(n))O(log (n)).

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved