2-3 tree is a tree data structure in which every internal node (non-leaf node) has either one data element and two children or two data elements and three children. If a node contains one data element leftVal, it has two subtrees (children) namely left and middle. Whereas if a node contains two data elements leftVal and rightVal, it has three subtrees namely left, middle and right.
The main advantage with 2-3 trees is that it is balanced in nature as opposed to a binary search tree whose height in the worst case can be O(n). Due to this, the worst case time-complexity of operations such as search, insertion and deletion is as the height of a 2-3 tree is .
Search: To search a key K in given 2-3 tree T, we follow the following procedure:
- If T is empty, return False (key cannot be found in the tree).
- If current node contains data value which is equal to K, return True.
- If we reach the leaf-node and it doesn’t contain the required key value K, return False.
- If K < currentNode.leftVal, we explore the left subtree of the current node.
- Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle subtree of the current node.
- Else if K > currentNode.rightVal, we explore the right subtree of the current node.
Consider the following example:
Insertion: There are 3 possible cases in insertion which have been discussed below:
Case 1: Insert in a node with only one data element
Case 2: Insert in a node with two data elements whose parent contains only one data element.
Case 3: Insert in a node with two data elements whose parent also contains two data elements.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Trie | (Insert and Search)
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Treap | Set 2 (Implementation of Search, Insert and Delete)
- K Dimensional Tree | Set 1 (Search and Insert)
- Insert a node in Binary Search Tree Iteratively
- Generic Trees(N-array Trees)
- Print Common Nodes in Two Binary Search Trees
- Self-Balancing-Binary-Search-Trees (Comparisons)
- Count the Number of Binary Search Trees present in a Binary Tree
- m-WAY Search Trees | Set-1 ( Searching )
- Total number of possible Binary Search Trees using Catalan Number
- Meta Binary Search | One-Sided Binary Search
- Efficiently design Insert, Delete and Median queries on a set
- MongoDB Python | Insert and Update Data
- Java Servlet and JDBC Example | Insert data in MySQL
- Insert Operation in B-Tree
- Splay Tree | Set 2 (Insert)
- Program to Insert new item in array on any position in PHP
- unordered_set insert() function in C++ STL
- unordered_multiset insert() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : DuyHoang