void insert(RBtree *T, data_t data) { Node *p = malloc(sizeof(Node)); p->data = data; p->parent = p->left = p->right = &NIL; /*root*/ if (*T == &NIL) { *T = p; p->color = BLACK; return; } Node *tmp; Node *it = *T; while (it != &NIL) { tmp = it; if (data == it->data) /*No reduplicate data, it coule be changed in concrete situation*/ return; else if (data < it->data) it = it->left; else it = it->right; } p->parent = tmp; if (data < tmp->data) tmp->left = p; else tmp->right = p; p->color = RED; insert_fix(T, p); }
inline void insert(int v){ Node *x = root, *y = null; while (x->s){ x->s++; if (v == x->data){ x->c++; return; } y = x, x = x->ch[v > x->data]; } x = newNode(v); if (y != null) y->ch[v > y->data] = x; else root = x; x->fa = y; x->push_up(); insert_fix(x); }