You are on page 1of 6

2-3-4 Tree is a Self-balancing, Multiway Search Tree.

It is always perfectly
balanced. 2-3-4 Trees are also known by name 2-4 Trees and is a specialization of
M-way Tree (M=4). These sometimes also called as 4th order B-trees are useful in
indexing and storage in large databases systems.

Properties

Now, let us look at some properties of 2-3-4 trees and its structure:

• Each Node in the tree can store at most 3 values/keys and 4 references or
pointers to child nodes.
• The values in each node are Ordered or in Sorted form.
• All leaf nodes are at same level. Hence making it perfectly balanced.
• An Internal (non-leaf) node can either have 2, 3 or 4 children. To be more
precise, nodes can be of the following three types.
1. 2-Node: Node has two child pointers and 1 data or key value.
2. 3-Node: Node has three child pointers and 2 data elements..
4-Node: Node has four child pointers and 3 data elements.

• A leaf node can have 2, 3 or 4 items but no children. In other words, a leaf is
2-Node, 3-Node or 4-Node where all pointers hold NULL reference.
Now let us look at the the type of nodes in 2-3-4 tree and structure of each node:
In the above examples, we have shown the Structure of 2-Node, 3-Node, and 4-
Node. We can see the 2-Node consists of only one Data/Value (A) and has two
pointers to its left and right children, where value of left child is smaller than the
parent and value of right child is greater than the parent. In 3-Node there are 2
Values i.e. (A, B) and 3 pointers: The first pointer points to left child with Value
smaller than A, the second pointer holds the node with a Value greater than A but
lesser than B and the third pointer points to node with a value greater than B.
Similarly, in 4-Node the node has 3 Values i.e. (A, B, C) and 4 child pointers as
shown in the figure.

Creation of 2-3-4 Tree with Insertion

So, while creating a 2-3-4 tree and for inserting nodes we have to follow some
rules:

• We insert each data item in the node in sorted order. A node can hold
maximum 3 values i.e. 4 node.
• When we try inserting an element in a 4-Node we will split the node and
move the middle item to its parent node if the parent becomes a 4-Node we
split it again.
• Once a node is split we cannot insert value in the parent node, we need to
traverse to its child nodes and then insert or split the node itself.
• Usually The Parent Node cannot be a 4-Node so we can accommodate the
extra data inside it while inserting.
et us understand with Step by Step example:

Suppose we are creating a 2-3-4 Tree with random data: [3,1,5,4,2,9,10]. We


follow these steps:

Step 1:

We insert value 3, 1, 5 serially maintaining the order in nodes while insertion. We


insert values or data items in node until it becomes a 4-node. After inserting the
values the node becomes a 4-Node. 3 gets inserted first, 1 is smaller so is inserted
before 3 and 5 being largest is inserted after 3.
Step 2:

Now, when we try inserting 4, there is no space to insert it so we have to split the
node from the middle element (3). Now 3 becomes the parent node (Root) and has
left child node 1 and in right child 5, as value 4 is greater than 3 we insert it in its
right child with node 5 in sorted order. The tree remains balanced and looks like:
Node marked in blue are Internal Node and in green are Leaf Nodes

Step 3:

Now, when we insert value 2 into the tree we traverse down comparing value with
Present Root 3,we insert value 2 into its left child as it is a 2-Node so we insert
after 1. Now when we insert 9 we check again with the root 3 , since the value is
greater we traverse down to its right child and then insert in it maintaining the
order as the right child is 3-Node and has space to accommodate one more node.
The 2-3-4 Tree now looks:
Step 4:

Finally, we try to insert 10 as it is greater than 3 so we go to its right child but the
node is already a 4-Node, so we split the node from the middle element again and
send node 5 to the parent node as it has space to accommodate (Discussed in
Rules) now parent node has values 3 and 5 in it since 4 is less than 5 so it
becomes the left child of 5 and right child of 3. Now we can insert 10 in the right
child of 5 after 9. So, The 2-3-4 tree now looks like:

You might also like