The Wayback Machine - https://web.archive.org/web/20241008012829/https://www.geeksforgeeks.org/2-3-trees-search-and-insert/
Open In App

2-3 Trees | (Search, Insert and Deletion)

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

In  binary search trees we have seen the average-case time for operations like search/insert/delete is O(log N) and the worst-case time is O(N) where N is the number of nodes in the tree.

Like other Trees include AVL trees, Red Black Tree, B tree, 2-3 Tree is also a height balanced tree.

The time complexity of search/insert/delete is O(log N) .

A 2-3 tree is a B-tree of order 3.

Properties of 2-3 tree:

  • Nodes with two children are called 2-nodes. The 2-nodes have one data value and two children
  • Nodes with three children are called 3-nodes. The 3-nodes have two data values and three children.
  • Data is stored in sorted order.
  • It is a balanced tree.
  • All the leaf nodes are at same level.
  • Each node can either be leaf, 2 node, or 3 node.
  • Always insertion is done at leaf.

Search: To search a key K in given 2-3 tree T, we follow the following procedure: 

Base cases: 

  1. If T is empty, return False (key cannot be found in the tree).
  2. If current node contains data value which is equal to K, return True.
  3. If we reach the leaf-node and it doesn’t contain the required key value K, return False.

Recursive Calls: 

  1. If K < currentNode.leftVal, we explore the left subtree of the current node.
  2. Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle subtree of the current node.
  3. 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.  

In Deletion Process for a specific value:

  • To delete a value, it is replaced by its in-order successor and then removed.
  • If a node is left with less than one data value then two nodes must be merged together.
  • If a node becomes empty after deleting a value, it is then merged with another node.

To Understand the deletion process-

Consider the 2-3 tree given below

Given 2-3 Tree

delete the following values from it: 69,72, 99, 81.

To delete 69, swap it with its in-order successor, that is, 72. 69 now comes in the leaf node. Remove the value 69 from the leaf node.

After deletion 69

To delete 72, 72 is an internal node. To delete this value swap 72 with its in-order successor 81 so that 72 now becomes a leaf node. Remove the value 72 from the leaf node.

After deletion 72

Now there is a leaf node that has less than 1 data value thereby violating the property of a 2-3 tree. So the node must be merged.

To merge the node, pull down the lowest data value in the parent’s node and merge it with its left sibling.

Rebalancing to Satisfy 23 Tree property

To delete 99, 99 is present in a leaf node, so the data value can be easily removed.

After deletion 99

Now there is a leaf node that has less than 1 data value, thereby violating the property of a 2-3 tree.

So the node must be merged. To merge the node, pull down the lowest data value in the parent’s node and merge it with its left sibling.

Rebalancing to Satisfy 2-3 Tree Property

To delete 81, 81 is an internal node. To delete this value swap 81 with its in-order successor 90 so that 81 now becomes a leaf node. Remove the value 81 from the leaf node.

After deletion 81

Now there is a leaf node that has less than 1 data value, thereby violating the property of a 2-3 tree. So the node must be merged. To merge the node, pull down the lowest data value in the parent’s node and merge it with its left sibling.

Rebalancing to Satisfy 2-3 Tree property

As internal node cannot be empty. So now pull down the lowest data value from the parent’s node and merge the empty node with its left sibling

Rebalancing to Satisfy 2-3 Tree Property

NOTE: In a 2-3 tree, each interior node has either two or three children. This means that a 2-3 tree is not a binary tree.



Previous Article
Next Article

Similar Reads

Total number of possible Binary Search Trees and Binary Trees with n keys
Total number of possible Binary Search Trees with n different keys (countBST(n)) = Catalan number Cn = (2n)! / ((n + 1)! * n!) For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, .... So are numbers of Binary Search Trees. Total number of possible Binary Trees with n different keys (countBT(n)) = countBST(n
12 min read
Insertion, Searching and Deletion in AVL trees containing a parent node pointer
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. The insertion and deletion in AVL trees have been discussed in the previous article. In this article, insert, search, and delete operations are discussed on AVL trees that also have a parent po
15+ min read
Proto Van Emde Boas Trees | Set 4 | Deletion
Please check previous sets of Proto Van Emde Boas Tree article first. It is highly recommended.Procedure for delete: Base Case: If we reach at Proto VEB with size 2 then we will check for whether the key is present or not if yes then we assign the pointer to nullptr which will set false to it presence.Recursion: We recursively call delete function
14 min read
m-Way Search Tree | Set-2 | Insertion and Deletion
Insertion in an m-Way search tree: The insertion in an m-Way search tree is similar to binary trees but there should be no more than m-1 elements in a node. If the node is full then a child node will be created to insert the further elements. Let us see the example given below to insert an element in an m-Way search tree.Example: To insert a new el
15+ min read
Threaded Binary Search Tree | Deletion
A threaded binary tree node looks like following. C/C++ Code struct Node { struct Node *left, *right; int info; // false if left pointer points to predecessor // in Inorder Traversal bool lthread; // false if right pointer points to predecessor // in Inorder Traversal bool rthread; }; Java Code static class Node { Node left, right; int info; // Tru
15+ min read
Ternary Search Tree (Deletion)
In the SET 1 post on TST we have described how to insert and search a node in TST. In this article, we will discuss algorithm on how to delete a node from TST. During delete operation we delete the key in bottom-up manner using recursion. The following are possible cases when deleting a key from trie. Key may not be there in TST. Solution: Delete o
14 min read
Deletion in Binary Search Tree (BST)
Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios: Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BST Deleting a single child node is also simple in BST. Copy the child to the node and delete the node.  Case 3. Delete a Node with Both Children in BST Deleting a node with b
10 min read
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 the very first node's address will be stored in a s
5 min read
Implementation of Search, Insert and Delete in Treap
We strongly recommend to refer set 1 as a prerequisite of this post.Treap (A Randomized Binary Search Tree)In this post, implementations of search, insert and delete are discussed.Search: Same as standard BST search. Priority is not considered for search. C/C++ Code // C function to search a given key in a given BST TreapNode* search(TreapNode* roo
15+ min read
Trie Data Structure | Insert and Search
The Trie data structure is a tree-like data structure used for storing a dynamic set of strings. It is commonly used for efficient retrieval and storage of keys in a large dataset. The structure supports operations such as insertion, search, and deletion of keys, making it a valuable tool in fields like computer science and information retrieval. I
15+ min read
Skip List - Efficient Search, Insert and Delete in Linked List
A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis. In a skip list, elements are organized in layers, with each layer having a smaller number of elements th
6 min read
Search, Insert, and Delete in an Unsorted Array | Array Operations
In this post, a program to search, insert, and delete operations in an unsorted array is discussed. Search Operation:In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Coding implementation of the search operation:[GFGTABS] C++ // C++ program to implement linear // search in u
15+ min read
Design a data structure that supports insert, delete, search and getRandom in constant time
Design a data structure that supports the following operations in O(1) time. insert(x): Inserts an item x to the data structure if not already present.remove(x): Removes item x from the data structure if present. search(x): Searches an item x in the data structure.getRandom(): Returns a random element from the current set of elements We can use has
5 min read
Search, Insert, and Delete in an Sorted Array | Array Operations
How to Search in a Sorted Array?In a sorted array, the search operation can be performed by using binary search. Below is the implementation of the above approach: [GFGTABS] C++ // C++ program to implement binary search in sorted array #include <bits/stdc++.h> using namespace std; int binarySearch(int arr[], int low, int high, int key) { if (
15+ min read
Data Structures | Binary Search Trees | Question 1
What is the worst case time complexity for search, insert and delete operations in a general Binary Search Tree for a skewed tree ? (A) O(n) for all (B) O(Logn) for all (C) O(Logn) for search and insert, and O(n) for delete (D) O(Logn) for search, and O(n) for insert and delete Answer: (A)Explanation: In skewed Binary Search Tree (BST), all three o
1 min read
Data Structures | Binary Search Trees | Question 2
In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as non-empty. Which of the following is true about inorder successor needed in delete operation? (A) Inorder Successor is always a leaf node (B) Inorder successor is always either a leaf node or a node with empt
2 min read
Data Structures | Balanced Binary Search Trees | Question 2
The worst case running time to search for an element in a balanced in a binary search tree with n2^n elements is (A) [Tex]\\Theta(n log n) [/Tex](B) [Tex]\\Theta (n2^n) [/Tex](C) [Tex]\\Theta (n) [/Tex](D) [Tex]\\Theta (log n) [/Tex] (A) A (B) B (C) C (D) D Answer: (C)Explanation: Time taken to search an element is [Tex]\\Theta (h) [/Tex]where h is
1 min read
Data Structures | Binary Search Trees | Question 4
How many distinct binary search trees can be created out of 4 distinct keys? (A) 4 (B) 14 (C) 24 (D) 42 Answer: (B) Explanation: number of distinct BSTs = (2n)!/[(n+1)!*n!] . Quiz of this Question Please comment below if you find anything wrong in the above post
1 min read
Data Structures | Binary Search Trees | Question 5
Which of the following traversal outputs the data in sorted order in a BST? (A) Preorder (B) Inorder (C) Postorder (D) Level order Answer: (B)Explanation: Inorder traversal of a BST outputs data in sorted order. Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Data Structures | Balanced Binary Search Trees | Question 2
What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a single node is 0. (A) 2 (B) 3 (C) 4 (D) 5 Answer: (B) Explanation: AVL trees are binary trees with the following restrictions. 1) the height difference of the children is at most 1. 2) both children are AVL trees Following is the most unbalanced AVL tre
1 min read
Data Structures | Binary Search Trees | Question 6
Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree? (A) 7 5 1 0 3 2 4 6 8 9 (B) 0 2 4 3 1 6 5 9 8 7 (C) 0 1 2 3 4 5 6 7 8 9 (D) 9 8 6 4 2 3 0 1 5 7 Answ
1 min read
Data Structures | Binary Search Trees | Question 7
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)? (GATE CS 2004) (A)2 (B)3 (C)4 (D)6 Answer: (B)Explanation: Constructed binary search tree will be. Quiz of this QuestionPleas
1 min read
Data Structures | Balanced Binary Search Trees | Question 4
Which of the following is AVL Tree? A 100 / \ 50 200 / \ 10 300 B 100 / \ 50 200 / / \ 10 150 300 / 5 C 100 / \ 50 200 / \ / \ 10 60 150 300 / \ \ 5 180 400 (A) Only A (B) A and C (C) A, B and C (D) Only B Answer: (B) Explanation: A Binary Search Tree is AVL if balance factor of every node is either -1 or 0 or 1. Balance factor of a node X is [(hei
1 min read
Data Structures | Balanced Binary Search Trees | Question 5
Consider the following AVL tree. 60 / \ 20 100 / \ 80 120 Which of the following is updated AVL tree after insertion of 70 A 70 / \ 60 100 / / \ 20 80 120 B 100 / \ 60 120 / \ / 20 70 80 C 80 / \ 60 100 / \ \ 20 70 120 D 80 / \ 60 100 / / \ 20 70 120 (A) A (B) B (C) C (D) D Answer: (C) Explanation: Refer following for steps used in AVL insertion. A
2 min read
Data Structures | Balanced Binary Search Trees | Question 13
Which of the following is a self-adjusting or self-balancing Binary Search Tree (A) Splay Tree (B) AVL Tree (C) Red Black Tree (D) All of the above Answer: (D) Explanation: See Splay Tree, AVL Tree and Red Black TreeQuiz of this Question
1 min read
Data Structures | Balanced Binary Search Trees | Question 7
Consider the following left-rotate and right-rotate functions commonly used in self-adjusting BSTs T1, T2 and T3 are subtrees of the tree rooted with y (on left side) or x (on right side) y x / \ Right Rotation / \ x T3 – - – - – - – > T1 y / \ < - - - - - - - / \ T1 T2 Left Rotation T2 T3 Which of the following is tightest upper bound for le
2 min read
Data Structures | Balanced Binary Search Trees | Question 13
Which of the following is true (A) The AVL trees are more balanced compared to Red Black Trees, but they may cause more rotations during insertion and deletion. (B) Heights of AVL and Red-Black trees are generally same, but AVL Trees may cause more rotations during insertion and deletion. (C) Red Black trees are more balanced compared to AVL Trees,
1 min read
Data Structures | Balanced Binary Search Trees | Question 9
Which of the following is true about Red Black Trees? (A) The path from the root to the furthest leaf is no more than twice as long as the path from the root to the nearest leaf (B) At least one children of every black node is red (C) Root may be red (D) A leaf node may be red Answer: (A) Explanation: See http://en.wikipedia.org/wiki/Red%E2%80%93bl
1 min read
Data Structures | Balanced Binary Search Trees | Question 13
Which of the following is true about AVL and Red Black Trees? (A) In AVL tree insert() operation, we first traverse from root to newly inserted node and then from newly inserted node to root. While in Red Black tree insert(), we only traverse once from root to newly inserted node. (B) In both AVL and Red Black insert operations, we traverse only on
1 min read
Data Structures | Balanced Binary Search Trees | Question 11
What is the worst case possible height of Red-Black tree? Assume base of Log as 2 in all options (A) 2Log(n+1) (B) 1.44 Logn (C) 4Logn (D) None of the above Answer: (A) Explanation: Refer Wiki Page of Red-Black TreeQuiz of this Question
1 min read