Open In App

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

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 .
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: 
 

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

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

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

                    
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)

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

                    

Searching in an m-Way search tree: 
 




 


 

// 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;
    }
}

                    
// 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)

                    
# 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
    

                    
// 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)

                    
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(): 
 


searchnode(): 
 


 


Article Tags :