1

I am making a recursive function to insert value in a btree. Before moving to the next node, I save the address of that node(pds_parent) so that I always have a pointer pointing to the parent.

But pds_parent gets initialized every time(node *pds_parent;), so I am not access the parent of a node, How I can access the parent of a node while going into recursion i.e accessing the child nodes and going out of recursion i.e moving back to parent.

void insertion(node *pds, int item){
    node *pds_parent;
    if(pds[0]->limit==0)
    {
        pds[1]->value=item;
        pds[0]->limit++;
        return 1;
    }
    int loc=b_search(pds,item,1,limit)
    if(pds[0]->is_leaf)
    {
        if(pds[0]->limit==2)
        {
            if(loc==0)
            {
                int value=pds[1]->value;
            }
            else if(loc==1)
            {
                int value=item
            }
            else
            {

                int value=pds[2]->value;
            }
            splitting(pds_parent,value,pds);
        }
        else
        {

            pds[(pds[0]->limit+1)]->value=item;
            if(loc==limit)
            {
               pds[(pds[0]->limit+1)].nextIndex=-1;
               pds[loc].nextIndex=limit+1;
            }
            else
            {
               pds[(pds[0]->limit+1)].nextIndex=loc+1;
               pds[loc].nextIndex=limit+1;
            }
            pds[0]->limit++;
            return 1;
        }
    }
    else
    {
        pds_parent=pds;
        insertion(pds[loc]->c,int item);
    }
}
H.S.
  • 11,654
  • 2
  • 15
  • 32
satyam_sareen
  • 97
  • 1
  • 6
  • 5
    Pass it as parameter?. – kiran Biradar Oct 19 '18 at 11:15
  • 2
    (not about the main question) All your definitions of `value` will be invisible for `splitting(pds_parent,value,pds);` Move `value` definition above `if(loc==0)` and leave just assignments in `if` branches. – ReAl Oct 19 '18 at 11:24

1 Answers1

2

If you want a variable to be shared across function calls, you should use static keyword. You can read more about it here .

First, initialize it while declaring:

static node *pds_parent = NULL;

This means that the first value of the pointer variable will be NULL. Before every recursive call, set it with the value of the current node being visited. Also, only use it if it's not NULL.

EDIT: I shall also add that I would rather change the function signature to void insertion(node *pds, int item, node *parent) and call it with a NULL the first time. Of course, having to use NULL is not pretty but you can wrap it. This is what kiran Biradar suggested, I assume.

nm_tp
  • 463
  • 3
  • 15
  • When I pass parent pointer as a parameter as you said(node *parent), Then is there a need to use static variable for parent – satyam_sareen Oct 19 '18 at 16:27
  • I completely agree. That's why I said that I would rather use that approach. However, from your question, it seemed to me that a static variable would also be suitable. – nm_tp Oct 22 '18 at 07:32