2-3-4 trees is an advanced topic on data structures and interview questions on this topic may be asked for senior positions.
2-3-4 trees are multiway trees in which node can have up to four child nodes.
Similar to other data structures, it is important to be thorough with the concepts of 2-3-4 tres, the common operations performed on 2-3-4 trees and the efficiency of these operations. Below interview questions on 2-3-4 trees addresses some of these topics. Java programming language will be used for code snippets when applicable to depict these concepts of 2-3-4 trees.
Multiway trees are trees in which the nodes have more data items and children than a binary tree. I.e each node in a multiway tree can have more than one data item, and more than two children.
2-3-4 trees are multiway trees in which each node has up to three data items and four children. Since nodes in a 2-3-4 tree can have up to four children they are multiway trees of order four.
2-3-4 trees are balanced trees similar to red-black trees.
Following is the relationship between the number of data items and the number of children that a node can contain in a 2-3-4 tree.
1. A node with one data item must have two children.
2. A node with two data items must have three children
3. A node with three data items must have four children.
A leaf node does not have children but can have up to three data items.
No, a node in a 2-3-4 tree cannot have a single child. Each node must have a minimum of two nodes and if it is leaf node then it does not have any child nodes.
No, a node in a 2-3-4 tree cannot have a single child. Each node must have a minimum of two nodes and if it is leaf node then it does not have any child nodes.
The data items in a node are arranged in ascending key order from left to right. The data items in a node are numbered as 0, 1 and 2 for convenience.
Following is the principle by which nodes are organized in a 2-3-4 tree.
All children in the subtree rooted at child 0 have key values less than key 0.
All children in the subtree rooted at child 1 have key values greater than key 0 but less than key 1
All children in the subtree rooted at child 2 have key values greater than key 1 but less than key 2.
All children in the subtree rooted at child 3 have key values greater than key 2.
Similar to the search routine in the binary search tree, you start a search in a 2-3-4 tree by starting at the root and selecting sub-trees with appropriate range of values that ultimately leads to the correct node.