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 tree
structnode* insert(intval,
structnode* root)
{
inti;
structnode *c, *n;
intflag;
// 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 = (structnode*)malloc(sizeof(structnode));
n->count = 1;
n->value[1] = i;
n->child[0] = root;
n->child[1] = c;
returnn;
}
returnroot;
}
C++
// Inserts a value in the m-Way tree
structnode* insert(intval,
structnode* root)
{
inti;
structnode *c, *n;
intflag;
// 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 = newnode();
n->count = 1;
n->value[1] = i;
n->child[0] = root;
n->child[1] = c;
returnn;
}
returnroot;
}
Java
// Inserts a value in the m-Way tree
publicnode insert(intval, node root)
{
inti;
node c, n;
intflag;
// 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 = newnode();
n.count = 1;
n.value[1] = i;
n.child[0] = root;
n.child[1] = c;
returnn;
}
returnroot;
}
// This code is contributed by Tapesh(tapeshdua420)
Python3
# Inserts a value in the m-Way tree
definsert(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
returnn
returnroot
C#
// Inserts a value in the m-Way tree
publicnode insert(intval, node root)
{
inti;
node c, n;
intflag;
// 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 = newnode();
n.count = 1;
n.value[1] = i;
n.child[0] = root;
n.child[1] = c;
returnn;
}
returnroot;
}
// This code is contributed by Tapesh(tapeshdua420)
Javascript
functioninsert(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 = newnode();
n.count = 1;
n.value[1] = i;
n.child[0] = root;
n.child[1] = c;
returnn;
}
returnroot;
}
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 node
intsetval(intval,
structnode* n,
int* p,
structnode** c)
{
intk;
// if node is null
if(n == NULL) {
*p = val;
*c = NULL;
return1;
}
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);
return0;
}
else{
// Insert by splitting
split(*p, *c, n, k, p, c);
return1;
}
}
return0;
}
}
C
// Sets the value in the node
intsetval(intval,
structnode* n,
int* p,
structnode** c)
{
intk;
// if node is null
if(n == NULL) {
*p = val;
*c = NULL;
return1;
}
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);
return0;
}
else{
// Insert by splitting
split(*p, *c, n, k, p, c);
return1;
}
}
return0;
}
}
Java
// Sets the value in the node
publicstaticintsetval(intval, node n, intp, node c)
{
intk;
// if node is null
if(n == null) {
p = val;
c = null;
return1;
}
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);
return0;
}
else{
// Insert by splitting
split(p, c, n, k, p, c);
return1;
}
}
return0;
}
}
// This code is contributed by Tapesh(tapeshdua420)
Python3
# Sets the value in the node
defsetval(val, n, p, c):
k=0
# if node is None
if(n ==None) :
p =val
c =None
return1
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)
return0
else:
# Insert by splitting
split(p, c, n, k, p, c)
return1
return0
C#
// Sets the value in the node
publicstaticintsetval(intval, node n, intp, node c)
{
intk;
// if node is null
if(n == null) {
p = val;
c = null;
return1;
}
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);
return0;
}
else{
// Insert by splitting
split(p, c, n, k, p, c);
return1;
}
}
return0;
}
}
// This code is contributed by Tapesh(tapeshdua420)
Javascript
functionsetval(val, n, p, c) {
let k;
// if node is null
if(n == null) {
p = val;
c = null;
return1;
}
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);
return0;
}
else{
// Insert by splitting
split(p, c, n, k, p, c);
return1;
}
}
return0;
}
}
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 node
voidfillnode(intval,
structnode* c,
structnode* n,
intk)
{
inti;
// 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 node
voidfillnode(intval,
structnode* c,
structnode* n,
intk)
{
inti;
// 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 node
publicstaticvoidfillnode(intval, node c, node n, intk)
{
inti;
// 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 node
deffillnode(val, c, n, k):
i=0
# Shifting the node by one position
fori inrange(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
staticvoidfillnode(intval, node c, node n, intk)
{
inti;
// 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
functionfillnode(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 node
voidsplit(intval,
structnode* c,
structnode* n,
intk, int* y,
structnode** newnode)
{
inti, mid;
if(k <= MIN)
mid = MIN;
else
mid = MIN + 1;
// Allocating the memory for a new node
*newnode = newnode();
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 node
voidsplit(intval,
structnode* c,
structnode* n,
intk, int* y,
structnode** newnode)
{
inti, mid;
if(k <= MIN)
mid = MIN;
else
mid = MIN + 1;
// Allocating the memory for a new node
*newnode = (structnode*)
malloc(sizeof(structnode));
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 node
publicvoidsplit(intval, node c, node n, intk, inty,
node newnode)
{
inti, mid;
if(k <= MIN)
mid = MIN;
else
mid = MIN + 1;
// Allocating the memory for a new node
newnode = newnode();
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 node
defsplit(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()
fori inrange(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 node
publicstaticvoidsplit(intval, node c, node n, intk, inty, node newnode)
{
inti, mid;
if(k <= MIN)
mid = MIN;
else
mid = MIN + 1;
// Allocating the memory for a new node
newnode = newnode();
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
functionsplit(val, c, n, k, y, newnode) {
let i, mid;
const MIN = 2; // assuming MIN and MAX are constants
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 node
structnode* del(intval,
structnode* root)
{
structnode* 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);
}
}
returnroot;
}
C
// Deletes value from the node
structnode* del(intval,
structnode* root)
{
structnode* 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);
}
}
returnroot;
}
Python3
# Deletes value from the node
defdelete(val, root):
temp=None
if(notdelhelp(val, root)) :
print()
print("value not found.")
else:
if(root.count ==0) :
temp =root
root =root.child[0]
returnroot
Javascript
// Deletes value from the node
functiondel(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;
}
}
returnroot;
}
Java
// Java Code
// Deletes value from the node
publicstaticNode del(intval, 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;
}
}
returnroot;
}
// This code is written by Sundaram.
C#
publicNode Del(intval, 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();
}
}
returnroot;
}
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()
intdelhelp(intval,
structnode* root)
{
inti;
intflag;
if(root == NULL)
return0;
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);
}
returnflag;
}
C
// Helper function for del()
intdelhelp(intval,
structnode* root)
{
inti;
intflag;
if(root == NULL)
return0;
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);
}
returnflag;
}
}
Python3
# Helper function for del()
defdelhelp(val, root):
i,flag=None,None
if(root ==None):
return0
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(notflag):
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)
returnflag
Java
// GFG
// Java Code
// Helper function for del()
publicstaticintdelhelp(intval, Node root)
{
inti;
intflag;
if(root == null)
return0;
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);
}
}
returnflag;
}
}
// This code is contributed by Sundaram
C#
publicintDelHelp(intval, Node root)
{
inti;
intflag;
if(root == null)
return0;
else
{
// Again searches for the node
flag = SearchNode(val, root, outi);
// 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);
}
}
returnflag;
}
}
Javascript
// Helper function for del()
functiondelhelp(val, root) {
let i;
let flag;
if(root == null)
return0;
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);
}
}
returnflag;
}
}
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 values
voidclear(structnode* m, intk)
{
inti;
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 values
voidclear(structnode* m, intk)
{
inti;
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 values
defclear(m, k):
fori inrange(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 values
publicstaticvoidclear(Node m, intk)
{
inti;
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#
publicvoidClear(Node m, intk)
{
for(inti = 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 values
functionclear(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 deleted
voidcopysucc(structnode* m, inti)
{
structnode* 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 deleted
voidcopysucc(structnode* m, inti)
{
structnode* 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 deleted
defcopysucc(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 deleted
publicstaticvoidcopysucc(Node m, inti)
{
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#
publicvoidCopySucc(Node m, inti)
{
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 deleted
functioncopysucc(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 node
voidrestore(structnode* m, inti)
{
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 node
voidrestore(structnode* m, inti)
{
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 node
defrestore(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 node
publicstaticvoidrestore(Node m, inti)
{
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#
publicvoidRestore(Node m, inti)
{
if(i == 0)
{
if(m.Child[1].Count > MIN)
{
LeftShift(m, 1);
}
else
{
Merge(m, 1);
}
}
elseif(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 node
functionrestore(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 child
voidrightshift(structnode* m, intk)
{
inti;
structnode* 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
voidleftshift(structnode* m, intk)
{
inti;
structnode* 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 child
voidrightshift(structnode* m, intk)
{
inti;
structnode* 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
voidleftshift(structnode* m, intk)
{
inti;
structnode* 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 child
defrightshift(m, k):
temp =m.child[k]
# Copying the nodes
fori inrange(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
defleftshift(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
fori inrange(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 child
voidrightshift(Node m, intk)
{
Node temp = m.child[k];
// Copying the nodes
for(inti = 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
voidleftshift(Node m, intk)
{
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(inti = 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#
voidrightshift(Node m, intk)
{
Node temp = m.child[k];
// Copying the nodes
for(inti = 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
voidleftshift(Node m, intk)
{
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(inti = 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 child
functionrightshift(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
functionleftshift(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 nodes
voidmerge(structnode* m, intk)
{
inti;
structnode *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 nodes
voidmerge(structnode* m, intk)
{
inti;
structnode *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 nodes
defmerge(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]
fori inrange(temp1.count+1):
temp2.count+=1
temp2.value[temp2.count] =temp1.value[i]
temp2.child[temp2.count] =temp1.child[i]
fori inrange(k,m.count):
m.value[i] =m.value[i +1]
m.child[i] =m.child[i +1]
m.count-=1
Java
voidmerge(Node m, intk)
{
inti;
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 nodes
functionmerge(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 nodes
voidMerge(Node m, intk)
{
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(inti = 0; i <= temp1.Count; i++)
{
temp2.Count++;
temp2.Value[temp2.Count] = temp1.Value[i];
temp2.Child[temp2.Count] = temp1.Child[i];
}
for(inti = 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()
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article’s discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please Login to comment...