Insertion of a node in M-way search tree I am currently studying data structures and i am looking to see if my implementation of the insertion method in an M-way search tree is good and i am looking forward to improving my coding approach.

There are some basic cases in inserting a node in an M-way search tree:-

Basic Case: If there is still space in the node for new key.

Otherwise,

Case 1: If the data is smaller than the first key of the node then we move towards the first child.

Case 2: If the data is greater than the last node then we move towards the last child.

Case 3: According to the property we check accordingly if the value ranges between the values of two keys.

Code:

#include&lt;iostream&gt;
using namespace std;

struct mWay{
    int index=-1;
    static const int m=3;
    int k[m-1];
    mWay *child[m]={NULL};
};

mWay* createNode(int value){
    mWay *node=new mWay;
    (node-&gt;index)++;
    node-&gt;k[node-&gt;index]=value;
    return node;
}

void sorting(mWay *s){      //using bubble sort..
    int temp;
    for(int i=0;i&lt;(s-&gt;index);i++){
        for(int j=0;j&lt;(s-&gt;index);j++){
            if((s-&gt;k[j])&gt;(s-&gt;k[j+1])){
                temp=(s-&gt;k[j]);
                (s-&gt;k[j])=(s-&gt;k[j+1]);
                (s-&gt;k[j+1])=temp;
            }
        }
    }
}

mWay* Insert(int data,mWay *root){
    if(root==NULL){         //creating a new mWay tree...
        root=createNode(data);
        return root;
    }
    else{                   //updating the mWay tree...
        mWay *ptr=root;
        mWay *par=NULL;
        int store;          //we will store the index of next child node in this variable (in the last case)...
        while(ptr!=NULL){
            par=ptr;
            if((ptr-&gt;index)&lt;(ptr-&gt;m)-2){    //checking to see if we can insert more keys in the current node...
                (ptr-&gt;index)++;
                ptr-&gt;k[ptr-&gt;index]=data;
                sorting(ptr);               //sorting keys..
                return root;
            }
            else{       //we have to move to the next node..
                if(data&lt;ptr-&gt;k[0]){     //if the data is smaller than the first key of the node then we move towards the first child...
                    ptr=ptr-&gt;child[0];
                }
                else if(data&gt;ptr-&gt;k[(ptr-&gt;m)-2]){       //if the data is greater than the last node then we move towards the last child...
                    ptr=ptr-&gt;child[(ptr-&gt;m)-1];
                }
                else{       //according to the property we check accordingly if the value ranges between the values of two keys ...
                    for(int i=1;i&lt;(ptr-&gt;m)-1;i++){
                        if(data&gt;(ptr-&gt;k[i-1]) &amp;&amp; data&lt;(ptr-&gt;k[i])){
                            store=i;
                            ptr=ptr-&gt;child[i];
                            break;
                        }
                    }
                }
            }
        }   //end of while loop which means we just have to insert the node now...
        if(data&lt;(par-&gt;k[0])){
            par-&gt;child[0]=createNode(data);
        }
        else if(data&gt;(par-&gt;k[(par-&gt;m)-2])){
            par-&gt;child[(par-&gt;m)-1]=createNode(data);
        }
        else{
            par-&gt;child[store]=createNode(data);
        }
        return root;
    }
}
This looks more like C than C++.
The only features of C++ I see used are #include &lt;iostream&gt;, using namespace std; and new.
Since this is flagged c++, I will review it as such, but if it is intended to use with C, a review for that language might give better insights.

Don't reinvent the wheel
The C++ standard library provides many features that would help. Need a sorting algorithm? Use std::sort. Need to find something in a container? Use std::find, std::lower_bound, std::upper_bound or some other algorithm that suits your needs!
Naming
Many variables have very short and cryptic names. This makes the code hard to read.
Example: What does mWay::k stand for?
Issues

Elements of &lt;iostream&gt; aren't used anywhere.

using namespace std; is discouraged, especially in headers. If someone else includes that header, it might cause ambiguities in their code!


Design
C++ has classes. Why not use them to define the tree, instead of a loose set of free standing functions?
For example, a rough outline could look like this:
#include &lt;array&gt;
#include &lt;memory&gt;
#include &lt;algorithm&gt;

template&lt;typename ValueType, size_t Arity&gt;
class nary_search_tree {
    static_assert(Arity &gt;= 2u, &quot;Tree needs an arity greater than or equal to 2!&quot;);

    class node {
        size_t size = 0;
        std::array&lt;ValueType, Arity-1&gt; values;
        std::array&lt;std::unique_ptr&lt;node&gt;, Arity&gt; children;

        void insert_in_order(ValueType value) {
            auto insert_index = find_child_index(value);
            
            if(insert_index == size) {
                values[insert_index] = value;
                ++size;
                return;
            }
            
            std::copy_backwards(&amp;values[insert_index], &amp;values[size-1], &amp;values[size]);
            values[insert_index] = value;
            ++size;
        }

        void find_child_index(ValueType value) const {
            for(auto i = 0u; i &lt; size; ++i) {
                if(values[i] &gt; value) return i;
            }
            
            return size;
        }

    public:
        void insert(ValueType value) {
            if(index &lt; values.size()) {
                insert_in_order(value);
                return;
            }

            auto index = find_child_index(value);
            if(!children[index]) {
                children[index] = std::make_unique&lt;node&gt;();
            }

            children[index]-&gt;insert(value);
        }
    };

    std::unique_ptr&lt;node&gt; root = nullptr;

public:
    void insert(ValueType value) {
        if(!root) root = std::make_unique&lt;node&gt;();
        
        root-&gt;insert(value);
    }
};

Makes reasoning much easier, doesn't it? And as a bonus, it's much more generic, too!
Note: This code requires C++11 or later to work.

Of course, this is just the beginning... This code could be improved, e.g. by using const ValueType&amp; to prevent copies and by using overloads for ValueType&amp;&amp; to support movable types. Also, other search tree functionality is missing, like looking up an element