Last active
September 20, 2015 06:20
-
-
Save ahmedahamid/f7525863e3f5acdca659 to your computer and use it in GitHub Desktop.
Tricky pointer basics explained | Recursive implementation of insertion into binary search tree
This file contains 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) | |
{ | |
tree_node* new_node = new tree_node(); | |
new_node->data = value; | |
new_node->left = new_node->right = NULL; | |
return new_node; | |
} | |
if (value == n->data) | |
{ | |
return n; | |
} | |
if (value < n->data) | |
{ | |
n->left = insert(n->left, value); | |
} | |
else | |
{ | |
n->right = insert(n->right, value); | |
} | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment