Wednesday, June 19, 2013

Insertion in B-Tree

Hi All,

Hope you will be finding my blogs useful, atleast for me it will be after sometime. So here is one more post about the insertion in B.Tree.

Here I am assuming we have structure defined to store the information, I will be using it as Node.


Node * insertElement(Node *head, char dir[],int val)
 
{
 
Node *prev;
 
Node  *cur;
 
Node *temp
 
int i;
 
temp->data = val;
 
temp->left= NULL;
 
temp->right = NULL;
 
prev = NULL;
 
cur = head;
 
for(i = 0; i < strlen(dir) && cur != NULL; i++)
 
{
 
prev = cur;
 
if(dir[i] = 'l')
 
cur = cur->left;
 
else
 
cur = cur->right;
 
}
 
/* Check if it is not reachable position */
 
if(cur != NULL ||  i != strlen(dir) )
 
{
 
printf("No Position find");
 
free(temp);
 
return head;
 
}
 
if(dir[i-1] ='l')
 
prev->left = temp;
 
else
 
prev->right = temp;
 
 
 
return head;
 
}

No comments: