# 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 element into an m-Way search tree we proceed in the same way as one would in order to search for the element
• To insert 6 into the 5-Way search tree shown in the figure, we proceed to search for 6 and find that we fall off the tree at the node [7, 12] with the first child node showing a null pointer
• Since the node has only two keys and a 5-Way search tree can accommodate up to 4 keys in a node, 6 is inserted into the node like [6, 7, 12]
• But to insert 146, the node [148, 151, 172, 186] is already full, hence we open a new child node and insert 146 into it. Both these insertions have been illustrated below

## C

 // Inserts a value in the m-Way treestruct node* insert(int val,                    struct node* root){    int i;    struct node *c, *n;    int flag;     // Function setval() is called which    // returns a value 0 if the new value    // is inserted in the tree, otherwise    // it returns a value 1    flag = setval(val, root, &i, &c);     if (flag) {        n = (struct node*)malloc(sizeof(struct node));        n->count = 1;        n->value[1] = i;        n->child[0] = root;        n->child[1] = c;        return n;    }    return root;}

## C++

 // Inserts a value in the m-Way treestruct node* insert(int val,                    struct node* root){    int i;    struct node *c, *n;    int flag;     // Function setval() is called which    // returns a value 0 if the new value    // is inserted in the tree, otherwise    // it returns a value 1    flag = setval(val, root, &i, &c);     if (flag) {        n = new node();        n->count = 1;        n->value[1] = i;        n->child[0] = root;        n->child[1] = c;        return n;    }    return root;}

## Java

 // Inserts a value in the m-Way treepublic node insert(int val, node root){    int i;    node c, n;    int flag;       // Function setval() is called which    // returns a value 0 if the new value    // is inserted in the tree, otherwise    // it returns a value 1    flag = setval(val, root, i, c);     if (flag) {        n = new node();        n.count = 1;        n.value[1] = i;        n.child[0] = root;        n.child[1] = c;        return n;    }    return root;} // This code is contributed by Tapesh(tapeshdua420)

## Python3

 # Inserts a value in the m-Way treedef insert(val, root):    # Function setval() is called which    # returns a value 0 if the new value    # is inserted in the tree, otherwise    # it returns a value 1    i,c=0,None    flag = setval(val, root, i, c)     if (flag) :        n = node()        n.count = 1        n.value[1] = i        n.child[0] = root        n.child[1] = c        return n         return root

## C#

 // Inserts a value in the m-Way treepublic node insert(int val, node root){    int i;    node c, n;    int flag;     // Function setval() is called which returns a value 0    // if the new value is inserted in the tree, otherwise    // it returns a value 1    flag = setval(val, root, i, c);     if (flag) {        n = new node();        n.count = 1;        n.value[1] = i;        n.child[0] = root;        n.child[1] = c;        return n;    }    return root;} // This code is contributed by Tapesh(tapeshdua420)

## Javascript

 function insert(val, root) {  let i;  let c, n;  let flag;   // Function setval() is called which  // returns a value 0 if the new value  // is inserted in the tree, otherwise  // it returns a value 1  flag = setval(val, root, i, c);   if (flag) {    n = new node();    n.count = 1;    n.value[1] = i;    n.child[0] = root;    n.child[1] = c;    return n;  }  return root;}

insert():

• The function insert() receives two parameters-the address of the new node and the value that is inserted
• This function calls a function setval() which returns a value 0 if the new value is inserted in the tree, otherwise it returns a value 1
• If it returns 1 then memory is allocated for new node, the variable count is assigned a value 1 and the new value is inserted in the node
• Then the addresses of the child nodes are stored in child pointers and finally the address of the node is returned

## C++

 // Sets the value in the nodeint setval(int val,           struct node* n,           int* p,           struct node** c){    int k;     // if node is null    if (n == NULL) {        *p = val;        *c = NULL;        return 1;    }    else {         // Checks whether the value to be        // inserted is present or not        if (searchnode(val, n, &k))            cout << "Key value already exists\n";         // The if-else condition checks whether        // the number of nodes is greater or less        // than the maximum number. If it is less        // then it inserts the new value in the        // same level node, otherwise, it splits the        // node and then inserts the value        if (setval(val, n->child[k], p, c)) {             // if the count is less than the max            if (n->count < MAX) {                fillnode(*p, *c, n, k);                return 0;            }            else {                 // Insert by splitting                split(*p, *c, n, k, p, c);                return 1;            }        }        return 0;    }}

## C

 // Sets the value in the nodeint setval(int val,           struct node* n,           int* p,           struct node** c){    int k;     // if node is null    if (n == NULL) {        *p = val;        *c = NULL;        return 1;    }    else {         // Checks whether the value to be        // inserted is present or not        if (searchnode(val, n, &k))            printf("Key value already exists\n");         // The if-else condition checks whether        // the number of nodes is greater or less        // than the maximum number. If it is less        // then it inserts the new value in the        // same level node, otherwise, it splits the        // node and then inserts the value        if (setval(val, n->child[k], p, c)) {             // if the count is less than the max            if (n->count < MAX) {                fillnode(*p, *c, n, k);                return 0;            }            else {                 // Insert by splitting                split(*p, *c, n, k, p, c);                return 1;            }        }        return 0;    }}

## Java

 // Sets the value in the nodepublic static int setval(int val, node n, int p, node c){    int k;     // if node is null    if (n == null) {        p = val;        c = null;        return 1;    }    else {         // Checks whether the value to be        // inserted is present or not        if (searchnode(val, n, k)) {            System.out.println("Key value already exists");        }         // The if-else condition checks whether        // the number of nodes is greater or less        // than the maximum number. If it is less        // then it inserts the new value in the        // same level node, otherwise, it splits the        // node and then inserts the value        if (setval(val, n.child[k], p, c)) {             // if the count is less than the max            if (n.count < MAX) {                fillnode(p, c, n, k);                return 0;            }             else {                 // Insert by splitting                split(p, c, n, k, p, c);                return 1;            }        }        return 0;    }} // This code is contributed by Tapesh(tapeshdua420)

## Python3

 # Sets the value in the nodedef setval(val, n, p, c):    k=0     # if node is None    if (n == None) :        p = val        c = None        return 1         else :         # Checks whether the value to be        # inserted is present or not        if (searchnode(val, n, k)):            print("Key value already exists")         # The if-else condition checks whether        # the number of nodes is greater or less        # than the maximum number. If it is less        # then it inserts the new value in the        # same level node, otherwise, it splits the        # node and then inserts the value        if (setval(val, n.child[k], p, c)) :             # if the count is less than the max            if (n.count < MAX) :                fillnode(p, c, n, k)                return 0                         else :                 # Insert by splitting                split(p, c, n, k, p, c)                return 1                              return 0

## C#

 // Sets the value in the nodepublic static int setval(int val, node n, int p, node c){    int k;     // if node is null    if (n == null) {        p = val;        c = null;        return 1;    }    else {         // Checks whether the value to be        // inserted is present or not        if (searchnode(val, n, k)) {            Console.WriteLine("Key value already exists");        }         // The if-else condition checks whether        // the number of nodes is greater or less        // than the maximum number. If it is less        // then it inserts the new value in the        // same level node, otherwise, it splits the        // node and then inserts the value        if (setval(val, n.child[k], p, c)) {             // if the count is less than the max            if (n.count < MAX) {                fillnode(p, c, n, k);                return 0;            }             else {                // Insert by splitting                split(p, c, n, k, p, c);                return 1;            }        }        return 0;    }} // This code is contributed by Tapesh(tapeshdua420)

## Javascript

 function setval(val, n, p, c) {  let k;   // if node is null  if (n == null) {    p = val;    c = null;    return 1;  }  else {     // Checks whether the value to be    // inserted is present or not    if (searchnode(val, n, k)) {      console.log("Key value already exists");    }     // The if-else condition checks whether    // the number of nodes is greater or less    // than the maximum number. If it is less    // then it inserts the new value in the    // same level node, otherwise, it splits the    // node and then inserts the value    if (setval(val, n.child[k], p, c)) {       // if the count is less than the max      if (n.count < MAX) {        fillnode(p, c, n, k);        return 0;      }       else {         // Insert by splitting        split(p, c, n, k, p, c);        return 1;      }    }    return 0;  }}

setval():

• The function setval() receives four parameters
• The first argument is the value that is to be inserted
• The second argument is the address of the node
• The third argument is an integer pointer that points to a local flag variable defined in the function insert()
• The last argument is a pointer to pointer to the child node that will be set in a function called from this function
• The function setval() returns a flag value that indicates whether the value is inserted or not
• If the node is empty then this function calls a function searchnode() that checks whether the value already exists in the tree
• If the value already exists then a suitable message is displayed
• Then a recursive call is made to the function setval() for the child of the node
• If this time the function returns a value 1 it means the value is not inserted
• Then a condition is checked whether the node is full or not
• If the node is not full then a function fillnode() is called that fills the value in the node hence at this point a value 0 is returned
• If the node is full then a function split() called that splits the existing node. At this point, a value 1 is returned to add the current value to the new node

## C++

 // Adjusts the value of the nodevoid fillnode(int val,              struct node* c,              struct node* n,              int k){    int i;     // Shifting the node by one position    for (i = n->count; i > k; i--) {        n->value[i + 1] = n->value[i];        n->child[i + 1] = n->child[i];    }    n->value[k + 1] = val;    n->child[k + 1] = c;    n->count++;}

## C

 // Adjusts the value of the nodevoid fillnode(int val,              struct node* c,              struct node* n,              int k){    int i;     // Shifting the node by one position    for (i = n->count; i > k; i--) {        n->value[i + 1] = n->value[i];        n->child[i + 1] = n->child[i];    }    n->value[k + 1] = val;    n->child[k + 1] = c;    n->count++;}

## Java

 // Adjusts the value of the nodepublic static void fillnode(int val, node c, node n, int k){    int i;     // Shifting the node by one position    for (i = n.count; i > k; i--) {        n.value[i + 1] = n.value[i];        n.child[i + 1] = n.child[i];    }    n.value[k + 1] = val;    n.child[k + 1] = c;    n.count++;} // This code is contributed by Tapesh(tapeshdua420)

## Python3

 # Adjusts the value of the nodedef fillnode(val, c, n, k):    i=0     # Shifting the node by one position    for i in range(n.count, k, -1):        n.value[i + 1] = n.value[i]        n.child[i + 1] = n.child[i]         n.value[k + 1] = val    n.child[k + 1] = c    n.count+=1

## C#

 // Adjusts the value of the node static void fillnode(int val, node c, node n, int k){    int i;     // Shifting the node by one position    for (i = n.count; i > k; i--) {        n.value[i + 1] = n.value[i];        n.child[i + 1] = n.child[i];    }       n.value[k + 1] = val;    n.child[k + 1] = c;    n.count++;} // This code is contributed by Tapesh(tapeshdua420)

## Javascript

 function fillnode(val, c, n, k) {  let i;   // Shifting the node by one position  for (i = n.count; i > k; i--) {    n.value[i + 1] = n.value[i];    n.child[i + 1] = n.child[i];  }  n.value[k + 1] = val;  n.child[k + 1] = c;  n.count++;}

fillnode():

• The function fillnode() receives four parameters
• The first is the value that is to be inserted
• The second is the address of the child node of the new value that is to be inserted
• The third is the address of the node in which the new value is to be inserted
• The last parameter is the position of the node where the new value is to be inserted

## C++

 // Splits the nodevoid split(int val,           struct node* c,           struct node* n,           int k, int* y,           struct node** newnode){    int i, mid;    if (k <= MIN)        mid = MIN;    else        mid = MIN + 1;     // Allocating the memory for a new node    *newnode = new node();     for (i = mid + 1; i <= MAX; i++) {        (*newnode)->value[i - mid] = n->value[i];        (*newnode)->child[i - mid] = n->child[i];    }     (*newnode)->count = MAX - mid;    n->count = mid;     // it checks whether the new value    // that is to be inserted is inserted    // at a position less than or equal    // to minimum values required in a node    if (k <= MIN)        fillnode(val, c, n, k);    else        fillnode(val, c, *newnode, k - mid);     *y = n->value[n->count];    (*newnode)->child[0] = n->child[n->count];    n->count--;}

## C

 // Splits the nodevoid split(int val,           struct node* c,           struct node* n,           int k, int* y,           struct node** newnode){    int i, mid;    if (k <= MIN)        mid = MIN;    else        mid = MIN + 1;     // Allocating the memory for a new node    *newnode = (struct node*)malloc(sizeof(struct node));     for (i = mid + 1; i <= MAX; i++) {        (*newnode)->value[i - mid] = n->value[i];        (*newnode)->child[i - mid] = n->child[i];    }     (*newnode)->count = MAX - mid;    n->count = mid;     // it checks whether the new value    // that is to be inserted is inserted    // at a position less than or equal    // to minimum values required in a node    if (k <= MIN)        fillnode(val, c, n, k);    else        fillnode(val, c, *newnode, k - mid);     *y = n->value[n->count];    (*newnode)->child[0] = n->child[n->count];    n->count--;}

## Java

 // Splits the nodepublic void split(int val, node c, node n, int k, int y,                  node newnode){    int i, mid;    if (k <= MIN)        mid = MIN;    else        mid = MIN + 1;     // Allocating the memory for a new node    newnode = new node();     for (i = mid + 1; i <= MAX; i++) {        newnode.value[i - mid] = n.value[i];        newnode.child[i - mid] = n.child[i];    }     newnode.count = MAX - mid;    n.count = mid;     // it checks whether the new value    // that is to be inserted is inserted    // at a position less than or equal    // to minimum values required in a node    if (k <= MIN)        fillnode(val, c, n, k);    else        fillnode(val, c, newnode, k - mid);     y = n.value[n.count];    newnode.child[0] = n.child[n.count];    n.count--;} // This code is contributed by Tapesh(tapeshdua420)

## Python3

 # Splits the nodedef split(val, c, n, k, y, newnode):    i, mid=0,0    if (k <= MIN):        mid = MIN    else:        mid = MIN + 1     # Allocating the memory for a new node    newnode = node()     for i in range(mid + 1,MAX+1) :        newnode.value[i - mid] = n.value[i]        newnode.child[i - mid] = n.child[i]          newnode.count = MAX - mid    n.count = mid     # it checks whether the new value    # that is to be inserted is inserted    # at a position less than or equal    # to minimum values required in a node    if (k <= MIN):        fillnode(val, c, n, k)    else:        fillnode(val, c, newnode, k - mid)     y = n.value[n.count]    newnode.child[0] = n.child[n.count]    n.count-=1

## C#

 // C# Code - Splits the nodepublic static void split(int val, node c, node n, int k, int y, node newnode){    int i, mid;    if (k <= MIN)        mid = MIN;    else        mid = MIN + 1;     // Allocating the memory for a new node    newnode = new node();     for (i = mid + 1; i <= MAX; i++) {        newnode.value[i - mid] = n.value[i];        newnode.child[i - mid] = n.child[i];    }     newnode.count = MAX - mid;    n.count = mid;     // it checks whether the new value    // that is to be inserted is inserted    // at a position less than or equal    // to minimum values required in a node    if (k <= MIN)        fillnode(val, c, n, k);    else        fillnode(val, c, newnode, k - mid);     y = n.value[n.count];    newnode.child[0] = n.child[n.count];    n.count--;} // This code is contributed Tapesh (tapeshdua420)

## Javascript

 function split(val, c, n, k, y, newnode) {    let i, mid;    const MIN = 2; // assuming MIN and MAX are constants    const MAX = 4;     if (k <= MIN) {        mid = MIN;    } else {        mid = MIN + 1;    }     // Allocating the memory for a new node    * newnode = new node();     for (i = mid + 1; i <= MAX; i++) {        ( * newnode) - > value[i - mid] = n - > value[i];        ( * newnode) - > child[i - mid] = n - > child[i];    }     ( * newnode) - > count = MAX - mid;    n - > count = mid;     // it checks whether the new value    // that is to be inserted is inserted    // at a position less than or equal    // to minimum values required in a node    if (k <= MIN) {        fillnode(val, c, n, k);    } else {        fillnode(val, c, * newnode, k - mid);    }     * y = n - > value[n - > count];    ( * newnode) - > child[0] = n - > child[n - > count];    n - > count--;}

split():

• The function split() receives six parameters
• The first four parameters are exactly the same as in the case of function fillnode()
• The fifth parameter is a pointer to variable that holds the value from where the node is split
• The last parameter is a pointer to pointer of the new node created at the time of split
• In this function firstly it is checked whether the new value that is to be inserted is inserted at a position less than or equal to minimum values required in a node
• If the condition is satisfied then the node is split at the position MIN
• Otherwise, the node is split at one position more than MIN
• Then dynamically memory is allocated for a new node
• Next, a for loop is executed which copies into the new node the values and children that occur on the right side of the value from where the node is split

Deletion in an m-Way search tree:

Let K be a key to be deleted from the m-Way search tree. To delete the key we proceed as one would to search for the key. Let the node accommodating the key be as illustrated below.

Approach:
There are several cases for deletion

• If (Ai = Aj = NULL) then delete K
• If (Ai != NULL, Aj = NULL) then choose the largest of the key elements K’ in the child node pointed to by Ai, delete the key K’ and replace K by K’
• Obviously deletion of K’ may call for subsequent replacements and therefore deletions in a similar manner, to enable the key K’ move up the tree
• If (Ai = NULL, Aj != NULL) then choose the smallest of the key element K” from the sub-tree pointed to by Aj, delete K” and replace K by K”
• Again deletion of K” may trigger subsequent replacements and deletions to enable K” move up the tree
• If (Ai !=NULL, Aj != NULL) then choose either the largest of the key elements K’ in the sub-tree pointed to by Ai, or the smallest of the key elements K” from the sub-tree pointed to by Aj to replace K
• As mentioned before, to move K’ or K” up the tree it may call for subsequent replacements and deletions

Example:

• To delete 151, we search for 151 and observe that in the leaf node [148, 151, 172, 186] where it is present, both its left sub-tree pointer and right sub-tree pointer are such that (Ai = Aj = NULL)
• We therefore simply delete 151 and the node becomes [148, 172, 186]. Deletion of 92 also follows a similar process
• To delete 262, we find its left and right sub-tree pointers Ai</sub and Aj respectively, are such that (Ai = Aj = NULL)
• Hence we choose the smallest element 272 from the child node [272, 286, 350], delete 272 and replace 262 with 272. Note that, to delete 272 the deletion procedure needs to be observed again
• To delete 12, we find the node [7, 12] accommodates 12 and the key satisfies (Ai != NULL, Aj = NULL)
• Hence we choose the largest of the keys from the node pointed to by Ai, viz., 10 and replace 12 with 10. This deletion is illustrated below

## C++

 // Deletes value from the nodestruct node* del(int val,                 struct node* root){    struct node* temp;    if (!delhelp(val, root)) {        cout << '\n';        cout << "value %d not found.\n";    }    else {        if (root->count == 0) {            temp = root;            root = root->child[0];            free(temp);        }    }    return root;}

## C

 // Deletes value from the nodestruct node* del(int val,                 struct node* root){    struct node* temp;    if (!delhelp(val, root)) {        printf("\n");        printf("value %d not found.\n", val);    }    else {        if (root->count == 0) {            temp = root;            root = root->child[0];            free(temp);        }    }    return root;}

## Python3

 # Deletes value from the nodedef delete(val, root):    temp=None    if (not delhelp(val, root)) :        print()        print("value not found.")         else :        if (root.count == 0) :            temp = root            root = root.child[0]                return root

## Javascript

 // Deletes value from the nodefunction del(val, root) {  let temp;  if (!delhelp(val, root)) {    console.log("\nvalue " + val + " not found.");  } else {    if (root.count === 0) {      temp = root;      root = root.child[0];      temp = null;    }  }  return root;}

## Java

 // Java Code// Deletes value from the nodepublic static Node del(int val, Node root){    Node temp;    if (!delhelp(val, root)) {        System.out.println(            String.format("Value %d not found.", val));    }    else {        if (root.count == 0) {            temp = root;            root = root.child[0];            temp = null;        }    }    return root;}// This code is written by Sundaram.

## C#

 public Node Del(int val, Node root){    Node temp;    if (!DelHelp(val, root))    {        Console.WriteLine("\nvalue {0} not found.", val);    }    else    {        if (root.Count == 0)        {            temp = root;            root = root.Child[0];            temp.Dispose();        }    }    return root;}

del():

• The function del() receives two parameters. First is the value that is to be deleted second is the address of the root node
• This function calls another helper function delhelp() which returns value 0 if the deletion of the value is unsuccessful, 1 otherwise
• Otherwise, a condition is checked whether the count is 0
• If it is, then it indicates that the node from which the value is deleted is the last value
• Hence, the first child of the node is itself made the node and the original node is deleted. Finally, the address of the new root node is returned

## C++

 // Helper function for del()int delhelp(int val,            struct node* root){    int i;    int flag;    if (root == NULL)        return 0;    else {         // Again searches for the node        flag = searchnode(val,                          root,                          &i);         // if flag is true        if (flag) {            if (root->child[i - 1]) {                copysucc(root, i);                // delhelp() is called recursively                flag = delhelp(root->value[i],                                root->child[i])                  if (!flag)                {                    cout << "\n";                    cout << "value %d not found.\n";                }            }            else                clear(root, i);        }        else {            // Recursion            flag = delhelp(val, root->child[i]);        }         if (root->child[i] != NULL) {            if (root->child[i]->count < MIN)                restore(root, i);        }        return flag;    }

## C

 // Helper function for del()int delhelp(int val,            struct node* root){    int i;    int flag;    if (root == NULL)        return 0;    else {         // Again searches for the node        flag = searchnode(val,                          root,                          &i);         // if flag is true        if (flag) {            if (root->child[i - 1]) {                copysucc(root, i);                // delhelp() is called recursively                flag = delhelp(root->value[i],                                root->child[i])                  if (!flag)                {                    printf("\n");                    printf("value %d not found.\n", val);                }            }            else                clear(root, i);        }        else {            // Recursion            flag = delhelp(val, root->child[i]);        }         if (root->child[i] != NULL) {            if (root->child[i]->count < MIN)                restore(root, i);        }        return flag;    }}

## Python3

 # Helper function for del()def delhelp(val, root):    i,flag=None,None    if (root == None):        return 0    else :         # Again searches for the node        flag = searchnode(val,root,i)         # if flag is true        if (flag) :            if (root.child[i - 1]) :                copysucc(root, i)                # delhelp() is called recursively                flag = delhelp(root.value[i],                                root.child[i])                 if (not flag):                    print()                    print("value not found.")                                          else:                clear(root, i)                 else :            # Recursion            flag = delhelp(val, root.child[i])                  if (root.child[i] != None) :            if (root.child[i].count < MIN):                restore(root, i)                 return flag

## Java

 // GFG// Java Code// Helper function for del()public static int delhelp(int val, Node root){    int i;    int flag;    if (root == null)        return 0;    else {        // Again searches for the node        flag = searchnode(val, root, i);        // if flag is true        if (flag == 1) {            if (root.child[i - 1] != null) {                copysucc(root, i);                // delhelp() is called recursively                flag                    = delhelp(root.value[i], root.child[i]);                if (flag == 0) {                    System.out.println(                        String.format("Value %d not found.",                                      root.value[i]));                }            }            else {                clear(root, i);            }        }        else {            // Recursion            flag = delhelp(val, root.child[i]);        }        if (root.child[i] != null) {            if (root.child[i].count < MIN) {                restore(root, i);            }        }        return flag;    }}// This code is contributed by Sundaram

## C#

 public int DelHelp(int val, Node root){    int i;    int flag;    if (root == null)        return 0;    else    {        // Again searches for the node        flag = SearchNode(val, root, out i);         // if flag is true        if (flag != 0)        {            if (root.Child[i - 1] != null)            {                CopySucc(root, i);                // DelHelp() is called recursively                flag = DelHelp(root.Value[i], root.Child[i]);                if (flag == 0)                {                    Console.WriteLine("\nvalue {0} not found.", root.Value[i]);                }            }            else            {                Clear(root, i);            }        }        else        {            // Recursion            flag = DelHelp(val, root.Child[i]);        }         if (root.Child[i] != null)        {            if (root.Child[i].Count < MIN)            {                Restore(root, i);            }        }        return flag;    }}

## Javascript

 // Helper function for del()function delhelp(val, root) {    let i;    let flag;    if (root == null)        return 0;    else {             // Again searches for the node        flag = searchnode(val, root, i);                 // if flag is true        if (flag) {          if (root.child[i - 1]) {            copysucc(root, i);            // delhelp() is called recursively            flag = delhelp(root.value[i], root.child[i]);            if (!flag) {              console.log(`\nvalue \${val} not found.\n`);            }          } else {            clear(root, i);          }        } else {          // Recursion          flag = delhelp(val, root.child[i]);        }                 if (root.child[i] != null) {          if (root.child[i].count < MIN) {            restore(root, i);          }        }        return flag;    }}

delhelp():

• The function delhelp() receives two parameters. First is the value to be deleted and the second is the address of the node from which it is to be deleted
• Initially it is checked whether the node is NULL
• If it is, then a value 0 is returned
• Otherwise, a call to function searchnode() is made
• If the value is found then another condition is checked to see whether there is any child to the value that is to be deleted
• If so, the function copysucc() is called which copies the successor of the value to be deleted and then a recursive call is made to the function delhelp() for the value that is to be deleted and its child
• If the child is empty then a call to function clear() is made which deletes the value
• If the searchnode() function fails then a recursive call is made to function delhelp() by passing the address of the child
• If the child of the node is not empty, then a function restore() is called to merge the child with its siblings
• Finally the value of the flag is returned which is set as a returned value of the function searchnode()

## C++

 // Removes the value from the// node and adjusts the valuesvoid clear(struct node* m, int k){    int i;    for (i = k + 1; i <= m->count; i++) {        m->value[i - 1] = m->value[i];        m->child[i - 1] = m->child[i];    }    m->count--;}

## C

 // Removes the value from the// node and adjusts the valuesvoid clear(struct node* m, int k){    int i;    for (i = k + 1; i <= m->count; i++) {        m->value[i - 1] = m->value[i];        m->child[i - 1] = m->child[i];    }    m->count--;}

## Python3

 # Removes the value from the# node and adjusts the valuesdef clear(m, k):    for i in range(k + 1,m.count+1) :        m.value[i - 1] = m.value[i]        m.child[i - 1] = m.child[i]         m.count-=1

## Java

 // GFG Java Code// Removes the value from the// node and adjusts the valuespublic static void clear(Node m, int k){    int i;    for (i = k + 1; i <= m.count; i++) {        m.value[i - 1] = m.value[i];        m.child[i - 1] = m.child[i];    }    m.count--;} // This code is contributed by Sundaram.

## C#

 public void Clear(Node m, int k){    for (int i = k + 1; i <= m.Count; i++)    {        m.Value[i - 1] = m.Value[i];        m.Child[i - 1] = m.Child[i];    }    m.Count--;}

## Javascript

 // Removes the value from the// node and adjusts the valuesfunction clear(m, k) {for (let i = k + 1; i <= m.count; i++) {m.value[i - 1] = m.value[i];m.child[i - 1] = m.child[i];}m.count--;}

clear():

• The function clear() receives two parameters. First is the address of the node from which the value is to be deleted and second is the position of the value that is to be deleted
• This function simply shifts the values one place to the left from the position where the value is to be deleted is present

## C++

 // Copies the successor of the// value that is to be deletedvoid copysucc(struct node* m, int i){    struct node* temp;    temp = p->child[i];    while (temp->child[0])        temp = temp->child[0];    p->value[i] = temp->value[i];}

## C

 // Copies the successor of the// value that is to be deletedvoid copysucc(struct node* m, int i){    struct node* temp;    temp = p->child[i];    while (temp->child[0])        temp = temp->child[0];    p->value[i] = temp->value[i];}

## Python3

 # Copies the successor of the# value that is to be deleteddef copysucc(m, i):    temp = p.child[i]    while (temp.child[0]):        temp = temp.child[0]    p.value[i] = temp.value[i]

## Java

 // Copies the successor of the// value that is to be deletedpublic static void copysucc(Node m, int i){    Node temp = m.child[i];    while (temp.child[0] != null) {        temp = temp.child[0];    }    m.value[i] = temp.value[0];} // This code is written by Sundaram.

## C#

 public void CopySucc(Node m, int i){    Node temp = m.Child[i];    while (temp.Child[0] != null)    {        temp = temp.Child[0];    }    m.Value[i] = temp.Value[0];}

## Javascript

 // Copies the successor of the// value that is to be deletedfunction copysucc(m, i) {  let temp = m.child[i];  while (temp.child[0]) {    temp = temp.child[0];  }  m.value[i] = temp.value[i];}

copysucc()

• The function copysucc() receives two parameters. First is the address of the node where the successor is to be copied and second is the position of the value that is to be overwritten with its successor

## C++

 // Adjusts the nodevoid restore(struct node* m, int i){    if (i == 0) {        if (m->child[1]->count > MIN)            leftshift(m, 1);        else            merge(m, 1);    }    else {        if (i == m->count) {            if (m->child[i - 1]->count > MIN)                rightshift(m, i);            else                merge(m, i);        }        else {            if (m->child[i - 1]->count > MIN)                rightshift(m, i);            else {                if (m->child[i + 1]->count > MIN)                    leftshift(m, i + 1);                else                    merge(m, i);            }        }    }}

## C

 // Adjusts the nodevoid restore(struct node* m, int i){    if (i == 0) {        if (m->child[1]->count > MIN)            leftshift(m, 1);        else            merge(m, 1);    }    else {        if (i == m->count) {            if (m->child[i - 1]->count > MIN)                rightshift(m, i);            else                merge(m, i);        }        else {            if (m->child[i - 1]->count > MIN)                rightshift(m, i);            else {                if (m->child[i + 1]->count > MIN)                    leftshift(m, i + 1);                else                    merge(m, i);            }        }    }}

## Python3

 # Adjusts the nodedef restore(m, i):    if (i == 0):        if (m.child[1].count > MIN):            leftshift(m, 1)        else:            merge(m, 1)         else :        if (i == m.count) :            if (m.child[i - 1].count > MIN):                rightshift(m, i)            else:                merge(m, i)                 else :            if (m.child[i - 1].count > MIN):                rightshift(m, i)            else :                if (m.child[i + 1].count > MIN):                    leftshift(m, i + 1)                else:                    merge(m, i)

## Java

 // Adjusts the nodepublic static void restore(Node m, int i){    if (i == 0) {        if (m.child[1].count > MIN) {            leftshift(m, 1);        }        else {            merge(m, 1);        }    }    else {        if (i == m.count) {            if (m.child[i - 1].count > MIN) {                rightshift(m, i);            }            else {                merge(m, i);            }        }        else {            if (m.child[i - 1].count > MIN) {                rightshift(m, i);            }            else {                if (m.child[i + 1].count > MIN) {                    leftshift(m, i + 1);                }                else {                    merge(m, i);                }            }        }    }} // This code is written by Sundaram.

## C#

 public void Restore(Node m, int i){    if (i == 0)    {        if (m.Child[1].Count > MIN)        {            LeftShift(m, 1);        }        else        {            Merge(m, 1);        }    }    else if (i == m.Count)    {        if (m.Child[i - 1].Count > MIN)        {            RightShift(m, i);        }        else        {            Merge(m, i);        }    }    else    {        if (m.Child[i - 1].Count > MIN)        {            RightShift(m, i);        }        else        {            if (m.Child[i + 1].Count > MIN)            {                LeftShift(m, i + 1);            }            else            {                Merge(m, i);            }        }    }}

## Javascript

 // Adjusts the nodefunction restore(m, i) {  if (i === 0) {    if (m.child[1].count > MIN) {      leftshift(m, 1);    } else {      merge(m, 1);    }  } else {    if (i === m.count) {      if (m.child[i - 1].count > MIN) {        rightshift(m, i);      } else {        merge(m, i);      }    } else {      if (m.child[i - 1].count > MIN) {        rightshift(m, i);      } else {        if (m.child[i + 1].count > MIN) {          leftshift(m, i + 1);        } else {          merge(m, i);        }      }    }  }}

restore():

• The function restore() receives two parameters. First is the node that is to be restored and second is the position of the value from where the values are restored
• If the second parameter is 0, then another condition is checked to find out whether the values present at the first child are more than the required minimum number of values
• If so, then a function leftshift() is called by passing the address of the node and a value 1 signifying that the value of this node is to be shifted from the first value
• If the condition is not satisfied then a function merge() is called for merging the two children of the node

## C++

 // Adjusts the values and children// while shifting the value from// parent to right childvoid rightshift(struct node* m, int k){    int i;    struct node* temp;     temp = m->child[k];     // Copying the nodes    for (i = temp->count; i > 0; i--) {        temp->value[i + 1] = temp->value[i];        temp->child[i + 1] = temp->child[i];    }    temp->child[1] = temp->child[0];    temp->count++;    temp->value[1] = m->value[k];     temp = m->child[k - 1];    m->value[k] = temp->value[temp->count];    m->child[k]->child[0]                 = temp->child[temp->count];    temp->count--;} // Adjusts the values and children// while shifting the value from// parent to left childvoid leftshift(struct node* m, int k){    int i;    struct node* temp;     temp = m->child[k - 1];    temp->count++;    temp->value[temp->count] = m->value[k];    temp->child[temp->count]                    = m->child[k]->child[0];     temp = m->child[k];    m->value[k] = temp->value[1];    temp->child[0] = temp->child[1];    temp->count--;     for (i = 1; i <= temp->count; i++) {        temp->value[i] = temp->value[i + 1];        temp->child[i] = temp->child[i + 1];    }}

## C

 // Adjusts the values and children// while shifting the value from// parent to right childvoid rightshift(struct node* m, int k){    int i;    struct node* temp;     temp = m->child[k];     // Copying the nodes    for (i = temp->count; i > 0; i--) {        temp->value[i + 1] = temp->value[i];        temp->child[i + 1] = temp->child[i];    }    temp->child[1] = temp->child[0];    temp->count++;    temp->value[1] = m->value[k];     temp = m->child[k - 1];    m->value[k] = temp->value[temp->count];    m->child[k]->child[0]                 = temp->child[temp->count];    temp->count--;} // Adjusts the values and children// while shifting the value from// parent to left childvoid leftshift(struct node* m, int k){    int i;    struct node* temp;     temp = m->child[k - 1];    temp->count++;    temp->value[temp->count] = m->value[k];    temp->child[temp->count]                    = m->child[k]->child[0];     temp = m->child[k];    m->value[k] = temp->value[1];    temp->child[0] = temp->child[1];    temp->count--;     for (i = 1; i <= temp->count; i++) {        temp->value[i] = temp->value[i + 1];        temp->child[i] = temp->child[i + 1];    }}

## Python3

 # Adjusts the values and children# while shifting the value from# parent to right childdef rightshift(m, k):    temp = m.child[k]     # Copying the nodes    for i in range(temp.count,0,-1) :        temp.value[i + 1] = temp.value[i]        temp.child[i + 1] = temp.child[i]         temp.child[1] = temp.child[0]    temp.count+=1    temp.value[1] = m.value[k]     temp = m.child[k - 1]    m.value[k] = temp.value[temp.count]    m.child[k].child[0] = temp.child[temp.count]    temp.count-=1     # Adjusts the values and children# while shifting the value from# parent to left childdef leftshift(m, k):     temp = m.child[k - 1]    temp.count+=1    temp.value[temp.count] = m.value[k]    temp.child[temp.count] = m.child[k].child[0]     temp = m.child[k]    m.value[k] = temp.value[1]    temp.child[0] = temp.child[1]    temp.count-=1     for i in range(1, temp.count+1):        temp.value[i] = temp.value[i + 1]        temp.child[i] = temp.child[i + 1]

## Java

 // Java Code// Adjusts the values and children// while shifting the value from// parent to right childvoid rightshift(Node m, int k){    Node temp = m.child[k];    // Copying the nodes    for (int i = temp.count; i > 0; i--) {        temp.value[i + 1] = temp.value[i];        temp.child[i + 1] = temp.child[i];    }    temp.child[1] = temp.child[0];    temp.count++;    temp.value[1] = m.value[k];     temp = m.child[k - 1];    m.value[k] = temp.value[temp.count];    m.child[k].child[0] = temp.child[temp.count];    temp.count--;} // Adjusts the values and children// while shifting the value from// parent to left childvoid leftshift(Node m, int k){    Node temp = m.child[k - 1];    temp.count++;    temp.value[temp.count] = m.value[k];    temp.child[temp.count] = m.child[k].child[0];     temp = m.child[k];    m.value[k] = temp.value[1];    temp.child[0] = temp.child[1];    temp.count--;     for (int i = 1; i <= temp.count; i++) {        temp.value[i] = temp.value[i + 1];        temp.child[i] = temp.child[i + 1];    }}// This is written by Sundaram

## C#

 void rightshift(Node m, int k){    Node temp = m.child[k];    // Copying the nodes    for (int i = temp.count; i > 0; i--) {        temp.value[i + 1] = temp.value[i];        temp.child[i + 1] = temp.child[i];    }    temp.child[1] = temp.child[0];    temp.count++;    temp.value[1] = m.value[k];     css Copy code temp = m.child[k - 1];    m.value[k] = temp.value[temp.count];    m.child[k].child[0] = temp.child[temp.count];    temp.count--;} // Adjusts the values and children// while shifting the value from// parent to left childvoid leftshift(Node m, int k){    Node temp = m.child[k - 1];    temp.count++;    temp.value[temp.count] = m.value[k];    temp.child[temp.count] = m.child[k].child[0];     css Copy code temp = m.child[k];    m.value[k] = temp.value[1];    temp.child[0] = temp.child[1];    temp.count--;     for (int i = 1; i <= temp.count; i++) {        temp.value[i] = temp.value[i + 1];        temp.child[i] = temp.child[i + 1];    }}

## Javascript

 // Adjusts the values and children// while shifting the value from// parent to right childfunction rightshift(m, k) {  let temp = m.child[k];   // Copying the nodes  for (let i = temp.count; i > 0; i--) {    temp.value[i + 1] = temp.value[i];    temp.child[i + 1] = temp.child[i];  }   temp.child[1] = temp.child[0];  temp.count++;  temp.value[1] = m.value[k];   temp = m.child[k - 1];  m.value[k] = temp.value[temp.count];  m.child[k].child[0] = temp.child[temp.count];  temp.count--;} // Adjusts the values and children// while shifting the value from// parent to left childfunction leftshift(m, k) {  let temp = m.child[k - 1];   temp.count++;  temp.value[temp.count] = m.value[k];  temp.child[temp.count] = m.child[k].child[0];   temp = m.child[k];  m.value[k] = temp.value[1];  temp.child[0] = temp.child[1];  temp.count--;   for (let i = 1; i <= temp.count; i++) {    temp.value[i] = temp.value[i + 1];    temp.child[i] = temp.child[i + 1];  }}

rightshift() and leftshift()

• The function rightshift() receives two parameters
• First is the address of the node from where the value is shifted to its child and second is the position k of the value that is to be shifted
• The function leftshift() are exactly same as that of function rightshift()
• The function merge() receives two parameters. First is the address of the node from which the value is to copied to the child and second is the position of the value

## C++

 // Merges two nodesvoid merge(struct node* m, int k){    int i;    struct node *temp1, *temp2;     temp1 = m->child[k];    temp2 = m->child[k - 1];    temp2->count++;    temp2->value[temp2->count] = m->value[k];    temp2->child[temp2->count] = m->child[0];     for (i = 0; i <= temp1->count; i++) {        temp2->count++;        temp2->value[temp2->count] = temp1->value[i];        temp2->child[temp2->count] = temp1->child[i];    }    for (i = k; i < m->count; i++) {        m->value[i] = m->value[i + 1];        m->child[i] = m->child[i + 1];    }    m->count--;    free(temp1);}

## C

 // Merges two nodesvoid merge(struct node* m, int k){    int i;    struct node *temp1, *temp2;     temp1 = m->child[k];    temp2 = m->child[k - 1];    temp2->count++;    temp2->value[temp2->count] = m->value[k];    temp2->child[temp2->count] = m->child[0];     for (i = 0; i <= temp1->count; i++) {        temp2->count++;        temp2->value[temp2->count] = temp1->value[i];        temp2->child[temp2->count] = temp1->child[i];    }    for (i = k; i < m->count; i++) {        m->value[i] = m->value[i + 1];        m->child[i] = m->child[i + 1];    }    m->count--;    free(temp1);}

## Python3

 # Merges two nodesdef merge(m, k):    temp1 = m.child[k]    temp2 = m.child[k - 1]    temp2.count+=1    temp2.value[temp2.count] = m.value[k]    temp2.child[temp2.count] = m.child[0]     for i in range(temp1.count+1):        temp2.count+=1        temp2.value[temp2.count] = temp1.value[i]        temp2.child[temp2.count] = temp1.child[i]         for i in range(k,m.count):        m.value[i] = m.value[i + 1]        m.child[i] = m.child[i + 1]         m.count-=1

## Java

 void merge(Node m, int k){    int i;    Node temp1, temp2;     temp1 = m.child[k];    temp2 = m.child[k - 1];    temp2.count++;    temp2.value[temp2.count] = m.value[k];    temp2.child[temp2.count] = m.child[0];     for (i = 0; i <= temp1.count; i++) {        temp2.count++;        temp2.value[temp2.count] = temp1.value[i];        temp2.child[temp2.count] = temp1.child[i];    }    for (i = k; i < m.count; i++) {        m.value[i] = m.value[i + 1];        m.child[i] = m.child[i + 1];    }    m.count--;}

## Javascript

 // Merges two nodesfunction merge(m, k) {  let temp1, temp2;   temp1 = m.child[k];  temp2 = m.child[k - 1];  temp2.count++;  temp2.value[temp2.count] = m.value[k];  temp2.child[temp2.count] = m.child[0];   for (let i = 0; i <= temp1.count; i++) {    temp2.count++;    temp2.value[temp2.count] = temp1.value[i];    temp2.child[temp2.count] = temp1.child[i];  }  for (let i = k; i < m.count; i++) {    m.value[i] = m.value[i + 1];    m.child[i] = m.child[i + 1];  }  m.count--;}

## C#

 // Merges two nodesvoid Merge(Node m, int k){    Node temp1 = m.Child[k];    Node temp2 = m.Child[k - 1];     temp2.Count++;    temp2.Value[temp2.Count] = m.Value[k];    temp2.Child[temp2.Count] = m.Child[0];     for (int i = 0; i <= temp1.Count; i++)    {        temp2.Count++;        temp2.Value[temp2.Count] = temp1.Value[i];        temp2.Child[temp2.Count] = temp1.Child[i];    }     for (int i = k; i < m.Count; i++)    {        m.Value[i] = m.Value[i + 1];        m.Child[i] = m.Child[i + 1];    }     m.Count--;    temp1 = null;}

• The function merge() receives two parameters
• First is the address of the node from which the value is to be copied to the child and second is the position of the value
• In this function, two temporary variables temp1 and temp2 are defined to hold the addresses of the two children of the value that is to be copied
• Initially, the value of the node is copied to its child. Then the first child of the node is made the respective child of the node where the value is copied
• Then two for loops are executed, out of which first copies all the values and children of one child to the other child
• The second loop shifts the value and child of the node from where the value is copied
• Then the count of the node from where the node is copied is decremented. Finally, the memory occupied by the second node is released by calling free()

Previous
Next