Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
2-3 Trees | (Search, Insert and Deletion)
Next article icon

2-3-4 Tree

Last Updated : 27 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.



Next Article
2-3 Trees | (Search, Insert and Deletion)
author
jeetamar__singh
Improve
Article Tags :
  • DSA
  • Tree
Practice Tags :
  • Tree

Similar Reads

  • Introduction to Tree Data Structure
    Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree. Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) o
    15+ min read
  • Tree Traversal Techniques
    Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss all the tree travers
    7 min read
  • Applications of tree data structure
    A tree is a type of data structure that represents a hierarchical relationship between data elements, called nodes. The top node in the tree is called the root, and the elements below the root are called child nodes. Each child node may have one or more child nodes of its own, forming a branching st
    4 min read
  • Advantages and Disadvantages of Tree
    Tree is a non-linear data structure. It consists of nodes and edges. A tree represents data in a hierarchical organization. It is a special type of connected graph without any cycle or circuit. Advantages of Tree:Efficient searching: Trees are particularly efficient for searching and retrieving data
    2 min read
  • Difference between an array and a tree
    Array:An array is a collection of homogeneous(same type) data items stored in contiguous memory locations. For example, if an array is of type “int”, it can only store integer elements and cannot allow the elements of other types such as double, float, char, etc. The array is a linear data structure
    3 min read
  • Inorder Tree Traversal without Recursion
    Given a binary tree, the task is to perform in-order traversal of the tree without using recursion. Example: Input: Output: 4 2 5 1 3Explanation: Inorder traversal (Left->Root->Right) of the tree is 4 2 5 1 3 Input: Output: 1 7 10 8 6 10 5 6Explanation: Inorder traversal (Left->Root->Rig
    8 min read
  • Types of Trees in Data Structures
    A tree in data structures is a hierarchical data structure that consists of nodes connected by edges. It is used to represent relationships between elements, where each node holds data and is connected to other nodes in a parent-child relationship. Types of Trees The main types of trees in data stru
    4 min read
  • Generic Trees (N-ary Tree)

    • Introduction to Generic Trees (N-ary Trees)
      Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and t
      5 min read

    • Inorder traversal of an N-ary Tree
      Given an N-ary tree containing, the task is to print the inorder traversal of the tree. Examples:  Input: N = 3   Output: 5 6 2 7 3 1 4Input: N = 3   Output: 2 3 5 1 4 6  Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finall
      6 min read

    • Preorder Traversal of an N-ary Tree
      Given an N-ary Tree. The task is to write a program to perform the preorder traversal of the given n-ary tree. Examples: Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 O
      14 min read

    • Iterative Postorder Traversal of N-ary Tree
      Given an N-ary tree, the task is to find the post-order traversal of the given tree iteratively.Examples: Input: 1 / | \ 3 2 4 / \ 5 6 Output: [5, 6, 3, 2, 4, 1] Input: 1 / \ 2 3 Output: [2, 3, 1] Approach:We have already discussed iterative post-order traversal of binary tree using one stack. We wi
      10 min read

    • Level Order Traversal of N-ary Tree
      Given an N-ary Tree. The task is to print the level order traversal of the tree where each level will be in a new line. Examples: Input: Output: 13 2 45 6Explanation: At level 1: only 1 is present.At level 2: 3, 2, 4 is presentAt level 3: 5, 6 is present Input: Output: 12 3 4 56 7 8 9 1011 12 1314Ex
      11 min read

    • ZigZag Level Order Traversal of an N-ary Tree
      Given a Generic Tree consisting of n nodes, the task is to find the ZigZag Level Order Traversal of the given tree.Note: A generic tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), a generic tree allow
      8 min read

    Binary Tree

    • Introduction to Binary Tree
      Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. Representation of Binary TreeEach node in a Binar
      15+ min read

    • Properties of Binary Tree
      This post explores the fundamental properties of a binary tree, covering its structure, characteristics, and key relationships between nodes, edges, height, and levels Note: Height of root node is considered as 0. Properties of Binary Trees1. Maximum Nodes at Level 'l'A binary tree can have at most
      4 min read

    • Applications, Advantages and Disadvantages of Binary Tree
      A binary tree is a tree that has at most two children for any of its nodes. There are several types of binary trees. To learn more about them please refer to the article on "Types of binary tree" Applications:General ApplicationsDOM in HTML: Binary trees help manage the hierarchical structure of web
      2 min read

    • Binary Tree (Array implementation)
      Given an array that represents a tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root. Construct the standard linked representation o
      6 min read

    • Complete Binary Tree
      We know a tree is a non-linear data structure. It has no limitation on the number of children. A binary tree has a limitation as any node of the tree has at most two children: a left and a right child. What is a Complete Binary Tree?A complete binary tree is a special type of binary tree where all t
      7 min read

    • Perfect Binary Tree
      What is a Perfect Binary Tree? A perfect binary tree is a special type of binary tree in which all the leaf nodes are at the same depth, and all non-leaf nodes have two children. In simple terms, this means that all leaf nodes are at the maximum depth of the tree, and the tree is completely filled w
      4 min read

    Ternary Tree

    • Create a Doubly Linked List from a Ternary Tree
      Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like a binary tree but instead of having two nodes, it has three nodes i.e. left, middle, and right. The doubly linked list should hold the following properties – The left pointer of the ternary tree should act as pr
      12 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences