Last active
September 20, 2015 06:21
-
-
Save ahmedahamid/d61ee9486267de6e7802 to your computer and use it in GitHub Desktop.
Tricky pointer basics explained | Inserting an element into a binary search tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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