Open In App

m-WAY Search Trees | Set-1 ( Searching )

Last Updated : 10 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The m-way search trees are multi-way trees which are generalised versions of binary trees where each node contains multiple elements. In an m-Way tree of order m, each node contains a maximum of m – 1 elements and m children.
The goal of m-Way search tree of height h calls for O(h) no. of accesses for an insert/delete/retrieval operation. Hence, it ensures that the height h is close to log_m(n + 1).
The number of elements in an m-Way search tree of height h ranges from a minimum of h to a maximum of m^{h} -1               .
An m-Way search tree of n elements ranges from a minimum height of log_m(n+1) to a maximum of n
An example of a 5-Way search tree is shown in the figure below. Observe how each node has at most 5 child nodes & therefore has at most 4 keys contained in it. 
 


The structure of a node of an m-Way tree is given below: 
 

C++

struct node {
    int count;
    int value[MAX + 1];
    struct node* child[MAX + 1];
};

                    

Java

public class Node {
    int count;
    int[] value = new int[MAX + 1];
    Node[] child = new Node[MAX + 1];
}
 
// This code is contributed by tapeshdua420.

                    

Python3

class node:
    def __init__(self):
        self.count = -1
        self.value = [-1]*(MAX + 1)
        self.child = [None]*(MAX + 1)

                    

C#

class node {
    public int count;
    public int[] value = new int[MAX + 1];
    public node[] child = new node[MAX + 1];
}
 
// This code is contributed by Tapesh (tapeshdua420)

                    

Javascript

class Node {
  constructor() {
    this.count = 0;
    this.value = new Array(MAX + 1);
    this.child = new Array(MAX + 1);
  }
}

                    
  • Here, count represents the number of children that a particular node has
  • The values of a node stored in the array value
  • The addresses of child nodes are stored in the child array
  • The MAX macro signifies the maximum number of values that a particular node can contain

Searching in an m-Way search tree: 
 

  • Searching for a key in an m-Way search tree is similar to that of binary search tree
  • To search for 77 in the 5-Way search tree, shown in the figure, we begin at the root & as 77> 76> 44> 18, move to the fourth sub-tree
  • In the root node of the fourth sub-tree, 77< 80 & therefore we move to the first sub-tree of the node. Since 77 is available in the only node of this sub-tree, we claim 77 was successfully searched


 


 

C++

// Searches value in the node
struct node* search(int val,
                    struct node* root,
                    int* pos)
{
 
    // if root is Null then return
    if (root == NULL)
        return NULL;
    else {
 
        // if node is found
        if (searchnode(val, root, pos))
            return root;
 
        // if not then search in child nodes
        else
            return search(val,
                          root->child[*pos],
                          pos);
    }
}
 
// Searches the node
int searchnode(int val,
               struct node* n,
               int* pos)
{
    // if val is less than node->value[1]
    if (val < n->value[1]) {
        *pos = 0;
        return 0;
    }
 
    // if the val is greater
    else {
        *pos = n->count;
 
        // check in the child array
        // for correct position
        while ((val < n->value[*pos])
               && *pos > 1)
            (*pos)--;
        if (val == n->value[*pos])
            return 1;
        else
            return 0;
    }
}

                    

Java

// Searches value in the node
public Node search(int val, Node root, int pos) {
    // if root is Null then return
    if (root == null)
        return null;
    else {
        // if node is found
        if (searchnode(val, root, pos))
            return root;
             
        // if not then search in child nodes
        else
            return search(val, root.child[pos], pos);
    }
}
 
// Searches the node
public boolean searchnode(int val, Node n, int pos) {
    // if val is less than node.value[1]
    if (val < n.value[1]) {
        pos = 0;
        return false;
    }
     // if the val is greater
    else {
        pos = n.count;
         
        // check in the child array
        // for correct position
        while ((val < n.value[pos]) && pos > 1)
            pos--;
             
        if (val == n.value[pos])
            return true;
        else
            return false;
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)

                    

Python3

# Searches value in the node
def search(val, root, pos):
 
    # if root is None then return
    if (root == None):
        return None
    else :
        # if node is found
        if (searchnode(val, root, pos)):
            return root
 
        # if not then search in child nodes
        else:
            return search(val, root.child[pos], pos)
     
 
 
# Searches the node
def searchnode(val, n, pos):
    # if val is less than node.value[1]
    if (val < n.value[1]):
        pos = 0
        return 0
     
 
    # if the val is greater
    else :
        pos = n.count
 
        # check in the child array
        # for correct position
        while ((val < n.value[pos]) and pos > 1):
            pos-=1
        if (val == n.value[pos]):
            return 1
        else:
            return 0
    

                    

C#

// Searches value in the node
public Node search(int val, Node root, int pos)
{
    // if root is Null then return
    if (root == null)
        return null;
    else {
        // if node is found
        if (searchnode(val, root, pos))
            return root;
 
        // if not then search in child nodes
        else
            return search(val, root.child[pos], pos);
    }
}
 
// Searches the node
public bool searchnode(int val, Node n, int pos)
{
    // if val is less than node.value[1]
    if (val < n.value[1]) {
        pos = 0;
        return false;
    }
 
    // if the val is greater
    else {
        pos = n.count;
 
        // check in the child array
        // for correct position
        while ((val < n.value[pos]) && pos > 1)
            pos--;
 
        if (val == n.value[pos])
            return true;
        else
            return false;
    }
}
 
// This code is contributed by Tapesh (tapeshdua420)

                    

Javascript

function search(val, root, pos) {
  // if root is null then return
  if (root === null) {
    return null;
  } else {
    // if node is found
    if (searchNode(val, root, pos)) {
      return root;
    }
    // if not then search in child nodes
    else {
      return search(val, root.child[pos.value], pos);
    }
  }
}
 
function searchNode(val, n, pos) {
  // if val is less than node.value[1]
  if (val < n.value[1]) {
    pos.value = 0;
    return false;
  }
  // if the val is greater
  else {
    pos.value = n.count;
    // check in the child array
    // for correct position
    while (val < n.value[pos.value] && pos.value > 1) {
      pos.value--;
    }
    if (val === n.value[pos.value]) {
      return true;
    } else {
      return false;
    }
  }
}

                    

search(): 
 

  • The function search() receives three parameters
  • The first parameter is the value to be searched, second is the address of the node from where the search is to be performed and third is the address of a variable that is used to store the position of the value once found
  • Initially a condition is checked whether the address of the node being searched is NULL
  • If it is, then simply a NULL value is returned
  • Otherwise, a function searchnode() is called which actually searches the given value
  • If the search is successful the address of the node in which the value is found is returned
  • If the search is unsuccessful then a recursive call is made to the search() function for the child of the current node


searchnode(): 
 

  • The function searchnode() receives three parameters
  • The first parameter is the value that is to be searched
  • The second parameter is the address of the node in which the search is to be performed and third is a pointer pos that holds the address of a variable in which the position of the value that once found is stored
  • This function returns a value 0 if the search is unsuccessful and 1 if it is successful
  • In this function initially it is checked whether the value that is to be searched is less than the very first value of the node
  • If it is then it indicates that the value is not present in the current node. Hence, a value 0 is assigned in the variable that is pointed to by pos and 0 is returned, as the search is unsuccessful


 



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
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
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
Iterative searching in Binary Search Tree
Given a binary search tree and a key. Check the given key exists in BST or not without recursion. Please refer binary search tree insertion for recursive search. C/C++ Code // C++ program to demonstrate searching operation // in binary search tree without recursion #include &lt;bits/stdc++.h&gt; using namespace std; struct Node { int data; struct N
7 min read
Searching in Binary Search Tree (BST)
Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Algorithm to search for a key in a given Binary Search Tree:Let's say we want to search for the number X, We start at the root. Then: We compare the valu
10 min read
Two Way Linear Search Algorithm
Two-Way Linear Search Algorithm is based on the principle of Linear Search, but the search is conducted from both ends towards the center of the array. In this article, we will learn about the basics of Two Way Linear Search Algorithm, its advantages, implementation etc. What is Two-Way Linear Search?Two Way Linear Search is a searching technique w
6 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