Created
July 15, 2015 06:34
-
-
Save joelburton/275126c3df0191e4a1d0 to your computer and use it in GitHub Desktop.
Binary Tree insertion in C
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
void insertNode(TreeNode **root, COMPARE compare, void* data) { | |
TreeNode *node = (TreeNode*) malloc(sizeof(TreeNode)); | |
node->data = data; | |
node->left = NULL; | |
node->right = NULL; | |
if (*root == NULL) { | |
*root = node; | |
return; | |
} | |
while (1) { | |
if (compare((*root)->data, data) > 0) { | |
if ((*root)->left != NULL) { | |
*root = (*root)->left; | |
} else { | |
(*root)->left = node; | |
break; | |
} | |
} else { | |
if ((*root)->right != NULL) { | |
*root = (*root)->right; | |
} else { | |
(*root)->right = node; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment