Created
April 21, 2016 23:41
-
-
Save abatilo/88fc70da11d4eadc01dd0400e4565b46 to your computer and use it in GitHub Desktop.
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
node *insert(node *root, int value) { | |
node *current = root; | |
node *prev; | |
if (root == NULL) { | |
root = (node *)malloc(sizeof(node)); | |
root->data = value; | |
root->left = NULL; | |
root->right = NULL; | |
return root; | |
} | |
while (current != NULL) { | |
if (value <= current->data) { | |
prev = current; | |
current = current->left; | |
} else { | |
prev = current; | |
current = current->right; | |
} | |
} | |
node *next = (node *)malloc(sizeof(node)); | |
next->data = value; | |
next->left = NULL; | |
next->right = NULL; | |
if (value <= prev->data) { | |
prev->left = next; | |
} else { | |
prev->right = next; | |
} | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment