The Wayback Machine - https://web.archive.org/web/20240924192819/https://www.geeksforgeeks.org/2-3-4-tree/
Open In App

2-3-4 Tree

Last Updated : 27 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A 2-3-4 tree is a self-balancing tree. The number represents the number of children each node can have. Any internal node can have either two, three, or four child nodes. It is also called a 2-4 tree.

Note: It is a B-tree of degree four and all leaf nodes at the same level

Properties of a 2-3-4 Tree:

  • A 2-node has one data element and if it is an internal node, then it has two child nodes.
  • A 3-node has two data elements and if this is an internal node, it has three child nodes.
  • A 4-node has three data elements and if it is an internal node, it has four child nodes.
  • The elements in each node should be sorted from smallest to greatest.
  • 2-3-4 tree is a perfectly balanced tree i.e., in this all leaf nodes are at the same level.
  • The type of any node is decided based on the structure of the tree (the structure gets decided such that the tree is always a perfectly balanced tree).

Structure of a node in 2-3-4 Tree:

Each node can have either 2, 3, or 4 children each of which holds 1, 2, or 3 data elements respectively. The data elements determine the range of the elements that will lie in which segment. See the following figure to get an idea of that:

Structure of a 4 node

Structure of a 4 node

Operations in a 2-3-4 tree:

There are three basic operations that are performed in a 2-3-4 tree. The operations are:

  • Insertion of a node
  • Searching a value
  • Deletion of a node

We have discussed the operations in detail in the following sections.

Insertion in a 2-3-4 Tree:

In insertion operation, a node is inserted in the proper location in the tree. The insertion operation is always performed in a leaf node. If there are empty spaces in the internal nodes also, still we do not use them for insertion. The insertion operation is implemented by following the below steps:

The task is to search for the suitable leaf node where the value should be inserted. In this process whenever we get a 4 node we split that such that we do not need to trace back from the leaf to the root.

  1. If the current node (say temp) is a 4 node that node is split. The following is the way used for splitting a node:
    1. Erase the mid-value (say X) of the node and store it.
    2. Now split the remaining 3 node into two 2 nodes.
    3. If the temp was the root of the tree, then create another root (a 2 node) with the value X which have the newly split nodes as its child.
    4. Otherwise, add the value X accordingly in the parent node. (As we are always splitting the 4 nodes while moving from top to bottom, it is guaranteed that there will always be a space available in the parent).
    5. Now arrange those two 2 nodes accordingly with the parent node.
  2. Find the child whose interval contains the value which is being inserted in the tree.
  3. If the child is a leaf node then insert the value. Otherwise, descend to the child and repeat from step 1.

Illustration:

Follow the illustration for a better understanding.

Say we have to insert the following numbers in the tree:
{10, 20, 30, 40, 50, 60}

Insert 10:
        => As there is nothing currently, insert 10 in a new root.
        => The root is a 2 node.

Insert 10

Insert 10

Insert 20:
        => Insert 20 in the root (which is also the leaf) in a sorted manner.
        => The root is now a 3 node.

Insert 20

Insert 20

Insert 30:
        => Insert 30 in the root (which is also the leaf) in a sorted manner.
        => The root is now a 4 node.

Insert 30

Insert 30

Insert 40:
        => Now the root is a 4 node. So this need to be splitted.
        => So the new root will be 20. The two child will be 10 and 30.
        => Now, add 40 with the node containing 30.

Insert 40

Insert 40

Insert 50:
        => The node 50 needs to be inserted to the right of 20.
        => Is is added with the node having values 30 and 40.
        => This leaf now becomes a 4 node.

Insert 50

Insert 50

Insert 60:
        => While searching for path, we will encounter the node with values 30, 40, 50.
        => Split this node. Add 40 to the parent and arrange the other two nodes accordingly.
        => Now insert 60 in the node with value 50.

Insert 60

Insert 60

Searching in a 2-3-4 Tree:

The searching operation is similar to a binary search tree. Follow the below method for searching anelements:

  1. Start searching from the root of the tree.
  2. If the value is present in that node, then the element is found.
  3. Otherwise, find the suitable interval in which the value is based on the node structure (i.e., it is 2 node, 3 node,s or 4 nodes).
  4. Move to that child and continue from step 1.
  5. If a leaf node is reached and the value is still not found then that value does not exist in the tree.

Deletion of a node from 2-3-4 Tree:

Here also the deletion operation is performed always on the leaf. The deletion is performed in the following way:

  1. Search the node whose value needs to be deleted.
  2. If the node is a leaf node then remove the required value from that node and decrease the data elements by 1.
  3. If the node is not a leaf node then:
    1. Find the successor of that node. A successor of a node is the smallest element among the ones which are greater than it or the largest element among the ones that are smaller than it.
    2. Swap the successor with the current node and delete that node in the leaf.

But it may cause an issue of underflow if the leaf node is a 2 node. To avoid this we perform the following adjustments on 2 nodes encountered along the path to reach the node to be removed while moving from top to bottom.

Case – 1: If either of the siblings of the current node is a 3 or 4 node.

  • Perform a rotation with that sibling.
  • The key having the closest value to this node moves up to the parent that overlooks the current node and the parent is added to the current node to make it a 3 node.
  • The node that was the originally rotated sibling’s child is now the child of the current node.
Rotation

Rotation

Case – 2: If the parent is a 2 node and the sibling is also a 2 node. In this particular case the parent is root. So merge the three 2 nodes to form a 4 node and remove the required value.

When parent and sibling are all 2 nodes

When parent and sibling are all 2 nodes

Case – 3: If the siblings are 2 nodes but the parent is a 3 node or 4 nodes:

  • The siblings (that are 2 nodes) and the parent key overlooking the siblings are merged to form a 4 node.
  • The child of the siblings is brought to this node.
Merge two 2-nodes

Merge two 2-nodes

Complexity Analysis of 2-3-4 trees:

  • Searching, insertion, and deletion all take O(logN) time complexity in 2-3-4 trees. Since the 2-3-4 is always balanced. By repeating inserting to initialize the 2-3-4 tree, we may say the time cost of init is O(n*log(n)). 
  • Height: In the worst case in 2-3-4 trees the height is logN and in the best case the height is 1/2 * logN (It is the condition when all nodes are 4 nodes)

Applications

Building a B-tree to represent a big existing collection of data and then slowly updating it using conventional B-tree operations is commonly beneficial in applications. In this scenario, the most efficient method of constructing the initial B-tree is to produce the initial set of leaf nodes straight from the input, then build the internal nodes from these. Bulkloading is the term for this method of B-tree construction. Every leaf, except the last, has an extra element that will be utilized to construct the internal nodes.



Previous Article
Next Article

Similar Reads

Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Convert a Generic Tree(N-array Tree) to Binary Tree
Prerequisite: Generic Trees(N-array Trees) In this article, we will discuss the conversion of the Generic Tree to a Binary Tree. Following are the rules to convert a Generic(N-array Tree) to a Binary Tree: The root of the Binary Tree is the Root of the Generic Tree.The left child of a node in the Generic Tree is the Left child of that node in the B
13 min read
Check if a given Binary Tree is height balanced like a Red-Black Tree
In a Red-Black Tree, the maximum height of a node is at most twice the minimum height (The four Red-Black tree properties make sure this is always followed). Given a Binary Search Tree, we need to check for following property. For every node, length of the longest leaf to node path has not more than twice the nodes on shortest path from node to lea
9 min read
Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
We have discussed Threaded Binary Tree. The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without recursion. In a simple threaded binary tree, the NULL right pointers are used to store inorder successor. Wherever a right pointer is NULL, it is used to store inorder successor. The following diagram sho
12 min read
Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
A complete binary tree is a binary tree whose all levels except the last level are completely filled and all the leaves in the last level are all to the left side. More information about complete binary trees can be found here. Example:Below tree is a Complete Binary Tree (All nodes till the second last nodes are filled and all leaves are to the le
10 min read
Check whether a binary tree is a full binary tree or not | Iterative Approach
Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has only one child node. Examples: Input : 1 / \ 2 3 / \ 4 5 Outp
8 min read
Binary Tree to Binary Search Tree Conversion using STL set
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of the Binary Tree.This solution will use Sets of C++ STL instead of array-based solution. Examples: Example 1 Input: 10 / \ 2 7 / \ 8 4 Output: 8 / \ 4 10 / \ 2 7 Example 2 Input: 10 / \ 30 15 / \ 20 5 Output: 15 / \
12 min read
What is B+ Tree | B+ Tree meaning
The B+ tree is similar to the B-tree data structure in that it is a tree structure with a fixed number of keys per node, and it is balanced so that all leaf nodes are at the same level. However, in a B+ tree, all keys are stored in the leaf nodes, while the internal nodes only contain pointers to other nodes. Characteristics of B+ Tree: The charact
5 min read
Red Black Tree vs AVL Tree
In this post, we will compare Red-Black Tree and AVL Tree. Red Black Tree: Properties: Self-Balancing is provided by painting each node with two colors(Red or Black).When the Tree is modified, a new tree is subsequently rearranged and repainted.It requires 1 bit of color information for each node in the tree.Time complexity: O(logn). Constraints ma
2 min read
Check whether a given binary tree is skewed binary tree or not?
Given a Binary Tree check whether it is skewed binary tree or not. A skewed tree is a tree where each node has only one child node or none. Examples: Input : 5 / 4 \ 3 / 2 Output : Yes Input : 5 / 4 \ 3 / \ 2 4 Output : No The idea is to check if a node has two children. If node has two children return false, else recursively compute whether subtre
13 min read
Build a segment tree for N-ary rooted tree
Prerequisite: Segment tree and depth first search.In this article, an approach to convert an N-ary rooted tree( a tree with more than 2 children) into a segment tree is discussed which is used to perform a range update queries. Why do we need a segment tree when we already have an n-ary rooted tree? Many times, a situation occurs where the same ope
15+ min read
Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
Given a Complete Binary Tree as an array, the task is to print all of its levels in sorted order.Examples: Input: arr[] = {7, 6, 5, 4, 3, 2, 1} The given tree looks like 7 / \ 6 5 / \ / \ 4 3 2 1 Output: 7 5 6 1 2 3 4 Input: arr[] = {5, 6, 4, 9, 2, 1} The given tree looks like 5 / \ 6 4 / \ / 9 2 1 Output: 5 4 6 1 2 9 Approach: A similar problem is
6 min read
Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
Given a tree having every node's value as either 0 or 1, the task is to find whether the given binary tree contains any sub-tree that has equal number of 0's and 1's, if such sub-tree is found then print Yes else print No.Examples: Input: Output: Yes There are two sub-trees with equal number of 1's and 0's. Hence the output is "Yes"Input: Output: N
10 min read
Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
Given a tree, and the weights (in the form of strings) of all the nodes, the task is to count the nodes whose weighted string when concatenated with the strings of the sub-tree nodes becomes a pangram. Pangram: A pangram is a sentence containing every letter of the English Alphabet. Examples: Input: Output: 1 Only the weighted string of sub-tree of
7 min read
Cartesian tree from inorder traversal | Segment Tree
Given an in-order traversal of a cartesian tree, the task is to build the entire tree from it. Examples: Input: arr[] = {1, 5, 3} Output: 1 5 3 5 / \ 1 3 Input: arr[] = {3, 7, 4, 8} Output: 3 7 4 8 8 / 7 / \ 3 4 Approach: We have already seen an algorithm here that takes O(NlogN) time on an average but can get to O(N2) in the worst case.In this art
13 min read
Check if the given n-ary tree is a binary tree
Given an n-ary tree, the task is to check whether the given tree is binary or not. Examples: Input: A / \ B C / \ \ D E F Output: Yes Input: A / | \ B C D \ F Output: No Approach: Every node in a binary tree can have at most 2 children. So, for every node of the given tree, count the number of children and if for any node the count exceeds 2 then p
6 min read
What is B-Tree? | B-Tree meaning
A B-tree is a self-balancing tree where all the leaf nodes are at the same level which allows for efficient searching, insertion and deletion of records. Because of all the leaf nodes being on the same level, the access time of data is fixed regardless of the size of the data set. Characteristics of B-Tree? B-trees have several important characteri
3 min read
Convert a given Binary tree to a tree that holds Logical OR property
Given a Binary Tree (Every node has at most 2 children) where each node has value either 0 or 1. The task is to convert the given Binary tree to a tree that holds Logical OR property, i.e., each node value should be the logical OR between its children. Example: Input: 1 / \ 1 0 / \ / \ 0 1 1 1 Output: 0 1 1 1 1 1 1Explanation: Given Tree 1 / \ 1 0
7 min read
Difference between General tree and Binary tree
General Tree: In the data structure, General tree is a tree in which each node can have either zero or many child nodes. It can not be empty. In general tree, there is no limitation on the degree of a node. The topmost node of a general tree is called the root node. There are many subtrees in a general tree. The subtree of a general tree is unorder
2 min read
Construct XOR tree by Given leaf nodes of Perfect Binary Tree
Given the leaf nodes of a perfect binary tree, the task is to construct the XOR tree and print the root node of this tree. An XOR tree is a tree whose parent node is the XOR of the left child and the right child node of the tree. Parent node = Left child node ^ Right child node Examples: Input: arr = {40, 32, 12, 1, 4, 3, 2, 7} Output: Nodes of the
12 min read
Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
Given two binary trees S and T, the task is the check that if S is a subtree of the Tree T. For Example: Input: Tree T - 1 / \ 2 3 / \ / \ 4 5 6 7 Tree S - 2 / \ 4 5 Output: YES Explanation: The above tree is the subtree of the tree T, Hence the output is YES Approach: The idea is to traverse both the tree in Pre-order Traversal and check for each
11 min read
Min-Max Product Tree of a given Binary Tree
Given a Binary Tree, the task is to convert the given Binary tree into Min-Max Product Tree and print the level-order sequence of the modified tree. Min-Max Product Tree: A Min-Max Product Tree contains the product of the minimum and maximum values of its left and right subtrees at each node. Note: For any node having only a single child, the value
14 min read
Minimum difference between any two weighted nodes in Sum Tree of the given Tree
Given a tree of N nodes, the task is to convert the given tree to its Sum Tree(including its own weight) and find the minimum difference between any two node's weight of the sum tree. Note: The N nodes of the given tree are given in the form of top to bottom with N-1 line where each line describes two nodes that are connected. Examples: Input: Outp
11 min read
Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
Given a Binary Search Tree and a binary integer K, the task is to convert Binary search tree into a Skewed Tree in increasing order if K = 0 or in decreasing order if K = 1. Examples: Input: K = 0, 5 / \ 3 6 Output: 3 \ 5 \ 6 Input: K = 1, 2 / \ 1 3 Output: 3 \ 2 \ 1 Approach: The key observation in the problem is that the first node of the skewed
10 min read
Check if max sum level of Binary tree divides tree into two equal sum halves
Given a Binary Tree, the task is to check if the maximum sum level divides the binary tree into the two parts of two equal sum halves.Examples: Input: 1 / \ 2 3 / \ \ 4 5 8 / \ 2 4 Output: YES Explanation: The maximum sum level is 2 and its sum is (4 + 5 + 8 = 17) Sum of the upper half (1 + 2 + 3) = 6 Sum of the Lower half (2 + 4) = 6 Input: 10 / \
11 min read
Difference between B tree and B+ tree
B-Tree: B-Tree is known as a self-balancing tree as its nodes are sorted in the inorder traversal. In B-tree, a node can have more than two children. B-tree has a height of logM N (Where ‘M’ is the order of tree and N is the number of nodes). And the height is adjusted automatically at each update. In the B-tree data is sorted in a specific order,
3 min read
Remove all leaf nodes from a Generic Tree or N-ary Tree
Given a Generic tree, the task is to delete the leaf nodes from the tree. Examples: Input: 5 / / \ \ 1 2 3 8 / / \ \ 15 4 5 6 Output: 5 : 1 2 3 1 : 2 : 3 : Explanation: Deleted leafs are: 8, 15, 4, 5, 6 Input: 8 / | \ 9 7 2 / | \ | / / | \ \ 4 5 6 10 11 1 2 2 3 Output: 8: 9 7 2 9: 7: 2: Approach: Follow the steps given below to solve the problem Co
9 min read
Check if a Binary Tree is an Even-Odd Tree or not
Given a Binary Tree, the task is to check if the binary tree is an Even-Odd binary tree or not. A Binary Tree is called an Even-Odd Tree when all the nodes which are at even levels have even values (assuming root to be at level 0) and all the nodes which are at odd levels have odd values. Examples: Input: 2 / \ 3 9 / \ \ 4 10 6 Output: YES Explanat
15+ min read
Flatten a Binary Search Tree to convert the tree into a wave list in place only
Given a Binary Search Tree consisting of N distinct nodes, the task is to flatten the given Binary Search Tree to convert the tree into a wave list. A wave list arr[0..n-1] is called a wave list if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ... . Examples: Input: 1 / \ 0 10Output: 0 \ 10 \ 1 Input: 3 / \ 1 7 / \ / \ 0 2 5 10 O
12 min read
Convert given Binary Tree to Symmetric Tree by adding minimum number of nodes
Given a Binary Tree, the task is to convert the given Binary Tree to the Symmetric Tree by adding the minimum number of nodes in the given Tree. Examples: Input: Output: Input: Output: Approach: To solve this problem, follow the below steps: Make a function buildSymmetricTree which will accept two parameters root1 and root2.Here, root1 and root2, a
8 min read
Article Tags :
Practice Tags :