rbt rbt_insert(rbt r,char *s) { if(r == NULL){/*empty tree*/ r = emalloc(sizeof *r); r->key = emalloc(strlen(s) * sizeof s[0] + 1); r->colour = RED; strcpy(r->key,s); }else if(strcmp(r->key,s)==0){/*do nothing*/ }else if(strcmp(r->key,s) < 0){/*s > b-key */ r->right = rbt_insert(r->right,s); } else if(strcmp(r->key,s) > 0){/*s < b-key*/ r->left = rbt_insert(r->left,s); } r = rbt_fix(r); return r; }
rbt rbt_insert(rbt r, char *s) { if (NULL == r) { r = emalloc(sizeof *r); r->key = emalloc(strlen(s) + 1); r->colour = RED; r->left = NULL; r->right = NULL; strcpy(r->key, s); } else if (strcmp(s, r->key) == 0) { /* do nothing */ } else if (strcmp(s, r->key) < 0) { r->left = rbt_insert(r->left, s); } else { r->right = rbt_insert(r->right, s); } return rbt_fix(r); }