Skip to content

Instantly share code, notes, and snippets.

@ahmedahamid
Last active September 20, 2015 06:21
Show Gist options
  • Save ahmedahamid/d61ee9486267de6e7802 to your computer and use it in GitHub Desktop.
Save ahmedahamid/d61ee9486267de6e7802 to your computer and use it in GitHub Desktop.
Tricky pointer basics explained | Inserting an element into a binary search tree
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
tree_node** x = &n;
while ((*x) != NULL)
{
if (value == (*x)->data)
{
return *x;
}
if (value < (*x)->data)
{
(*x) = (*x)->left;
}
else
{
(*x) = (*x)->right;
}
}
*x = new tree_node();
(*x)->data = value;
(*x)->right = (*x)->left = NULL;
return (*x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment