Open In App

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: 
 






// Inserts a value in the m-Way tree
struct 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;
}




// Inserts a value in the m-Way tree
struct 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;
}




// Inserts a value in the m-Way tree
public 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)




# Inserts a value in the m-Way tree
def 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




// Inserts a value in the m-Way tree
public 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)




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




// Sets the value in the node
int 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;
    }
}




// Sets the value in the node
int 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;
    }
}




// Sets the value in the node
public 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)




# Sets the value in the node
def 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




// Sets the value in the node
public 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)




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






// Adjusts the value of the node
void 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++;
}




// Adjusts the value of the node
void 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++;
}




// Adjusts the value of the node
public 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)




# Adjusts the value of the node
def 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




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




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




// Splits the node
void 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--;
}




// Splits the node
void 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--;
}




// Splits the node
public 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)




# Splits the node
def 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# Code - Splits the node
public 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)




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

 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 
 

Example: 
 




// Deletes value from the node
struct 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;
}




// Deletes value from the node
struct 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;
}




# Deletes value from the node
def 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




// Deletes value from the node
function 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 Code
// Deletes value from the node
public 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.




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




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




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




# 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




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




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




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




// Removes the value from the
// node and adjusts the values
void 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--;
}




// Removes the value from the
// node and adjusts the values
void 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--;
}




# Removes the value from the
# node and adjusts the values
def 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




// GFG Java Code
// Removes the value from the
// node and adjusts the values
public 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.




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




// Removes the value from the
// node and adjusts the values
function 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(): 
 




// Copies the successor of the
// value that is to be deleted
void 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];
}




// Copies the successor of the
// value that is to be deleted
void 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];
}




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




// Copies the successor of the
// value that is to be deleted
public 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.




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




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

copysucc()
 




// Adjusts the node
void 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);
            }
        }
    }
}




// Adjusts the node
void 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);
            }
        }
    }
}




# Adjusts the node
def 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)




// Adjusts the node
public 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.




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




// Adjusts the node
function 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(): 
 




// Adjusts the values and children
// while shifting the value from
// parent to right child
void 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 child
void 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];
    }
}




// Adjusts the values and children
// while shifting the value from
// parent to right child
void 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 child
void 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];
    }
}




# Adjusts the values and children
# while shifting the value from
# parent to right child
def 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 child
def 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 Code
// Adjusts the values and children
// while shifting the value from
// parent to right child
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];
 
    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 child
void 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




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 child
void 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];
    }
}




// Adjusts the values and children
// while shifting the value from
// parent to right child
function 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 child
function 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() 
 




// Merges two nodes
void 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);
}




// Merges two nodes
void 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);
}




# Merges two nodes
def 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




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




// Merges two nodes
function 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--;
}




// Merges two nodes
void 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;
}


Article Tags :